Solved

PHP E-mail

Posted on 2011-02-14
13
294 Views
Last Modified: 2012-05-11
dear experts ...

i will be thankful if any expert would give me a small example on how to send an email from a php page to the outlook email account ....

i wanna know if  i have to change something in the php.ini or ini_set () ???
and what about the smpt ?

i would like to note that .. the email account i have in the outlook is  for the company i am working on ....
i.e ( myname@mycompany.com )

list me all the things i have to configure
hope to find solution as soon as possible !
0
Comment
Question by:mokatell
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
13 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 34891610
This is my generic PHP email demo.  Save it as 'Email.php', put your own email address in it and run it.  It is self contained.  It is expecting a SMTP server to be on your machine.
<?php
# some settings of POST vars
if (!isset($_POST['send']))  $send = ''; else $send = $_POST['send'];
if (!isset($_POST['toText'])) $toText = ''; else $toText = $_POST['toText'];
if (!isset($_POST['ccText'])) $ccText = ''; else $ccText = $_POST['ccText'];
if (!isset($_POST['subjectText'])) $subjectText = ''; else $subjectText = $_POST['subjectText'];
if (!isset($_POST['msgText'])) $msgText = ''; else $msgText = $_POST['msgText'];
if (!isset($_POST['ccText'])) $ccTexth = ''; else $ccText = $_POST['ccText'];
if (!isset($_POST['bccText'])) $bccText = ''; else $bccText = $_POST['bccText'];
if (!isset($_POST['nameText'])) $nameText = ''; else $nameText = $_POST['nameText'];
if (!isset($_POST['fromText'])) $fromText = ''; else $fromText = $_POST['fromText'];

if ($send == "") {
    $title="Test Email Page";
    $announce="---";
}
else {
	if($fromText === "") die("No name!");
  $toText="youremail@yourdomain.com";
	$title="Test Email Page";
  $announce="Your Message has been Sent!";
	$header = "From: ".$fromText."\r\n";
//	$header .= "Cc: ".$ccText."\n";
	$header .= "Reply-To : ".$fromText."\r\n";
	$header .= "Return-Path : ".$fromText."\r\n";
	$header .= "X-Mailer: PHP\r\n";
	$header .= "MIME-Version: 1.0\r\n";
	$header .= "Content-Type: text/plain; charset=iso-8859-1\r\n";
//	ini_set(sendmail_from,$fromText);  
	mail($toText, $subjectText, $msgText, $header, '-f'.$fromText);
//	ini_restore(sendmail_from);
}
?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title><?php echo($title)?></title>
<style type="text/css">
<!-- 
A:link { color: #999999; }
A:visited { color: #999999; }
A:hover {color: #0099ff;}
-->
</style>
<script type="text/javascript">
<!--
function check()
{
var at=document.getElementById("fromText").value.indexOf("@");
var eml=document.getElementById("fromText").value;
var nam=document.getElementById("nameText").value;
var alerttxt="";
var submitOK="true";

if (eml.length < 5 || at == -1)
    {
    alerttxt=alerttxt+"Please enter a valid e-mail address!\r\n";
    submitOK="false"
    //return false;
    }
if (nam.length < 3)
    {
    alerttxt=alerttxt+"Please enter your name.\r\n";
    submitOK="false"
    //return false;
    }
if (submitOK=="false")
    {
    alert(alerttxt);
    return false;
    }

}

// -->
</script>
</head>

<body bgcolor="#ddeedd">
<div align="center">
<table border="0" cellpadding="0" cellspacing="0" summary="" width="580">
<tr><td align="center">

<?php
if ($send != "") {
   	echo ("To: ".$toText."<br>\r\nSubject: ".$subjectText."<br>\r\n".$msgText."<br>\r\n".$header);
		}
?>

<p><b><font color="#000000" size="5">Test Email</font></b></p>
<font size="4" color="#000000">

<!--   <form method="POST" action="<?php echo($PHP_SELF)?>"> -->
  <form method="POST" action="Email.php" onsubmit="return check();">
    <p><font size="3"><b>Name: <input type="text" name="nameText" id="nameText" size="46"></b></font></p>
    <p><font size="3"><b>Email: <input type="text" name="fromText" id="fromText" size="46"></b></font></p>
    <input type="hidden" name="subjectText" value="Web Mail">
    <p><font face="Arial" size="3"><b>Message Text:</b></font></p>
    <p><font face="Arial" size="3"><b><textarea rows="6" name="msgText" cols="60"></textarea></b></font></p>
    <p><font size="3"><b><input type="submit" value="Send" name="send" style="font-family: Arial; font-size: 12pt; font-weight: bold"></b></font></p>
    <input type="hidden" name="state" value="1">
  </form>
  <b><font face="Arial" size="4" color="#e00000"><?php echo($announce)?></font></b><br><br>

</font>
</td></tr>
</table> 
</div>


</body>
</html>

Open in new window

0
 

Author Comment

by:mokatell
ID: 34891693
ok fine .. if i wanna use the hotmail server ,
what can i do in this case ?
0
 
LVL 6

Expert Comment

by:MatthewP
ID: 34892017
Are you trying to get php to send an email via your outlook account so that the email is sat there in your outbox, is that what you mean?
0
Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

 
LVL 17

Expert Comment

by:psimation
ID: 34894163
Rather use PHPMailer class to do your mailing. no need to change anything in your php.ini. all you need is a proper smtp server ( typically the one from your ISP or hosting provider). It will give you much more functionality than the built-in php mail() function.

Example of how to use it here: phpmailer.worxware.com
Download the class here: sourceforge.net/projects/phpmailer/

0
 

Author Comment

by:mokatell
ID: 34897838
Fatal error: Call to a member function Send() on a non-object


this is the error i am getting !!
0
 
LVL 17

Expert Comment

by:psimation
ID: 34898484
please post your code.
0
 

Author Comment

by:mokatell
ID: 34898660
now i am getting this error

( SMTP Error: Could not authenticate. Message Not Sent
Mailer Error: SMTP Error: Could not authenticate. )

 ... i want this to be fixed .. on yahoo , or hotmail ..

here is the code


<html>
<head>
<title>PHPMailer</title>
</head>
<body>
<?php
 if (!array_key_exists('Submitted',$_POST))
 {
?>
  <form method="post" action="PHPMailer.php">
  <input type="hidden" name="Submitted" value="true"/><br/>
  Mail Server: <input type="text" name="Host" size="25"/><br/>
  If authentication is required:<br/>
  Username: <input type="text" name="Username" size="25"/><br/>
  Password: <input type="password" name="Password" size="10"/>
  <hr/>
  To: <input type="text" name="To" size="25"/><br/>
  From Email: <input type="text" name="From" size="25"/><br/>
  From Name: <input type="text" name="FromName" size="25"/><br/>
  Subject: <input type="text" name="Subject" size="25"/><br/>
  <textarea name="Message" cols="50" rows="10"></textarea><br/>
  Using HTML: <input type="checkbox" name="HTML"/>
  <input type="submit" value="Send Email"/>
  </form>
<?php
 }
 else
 {
  require("class.phpmailer.php");
  $To = $_POST['To'];
  $From = $_POST['From'];
  $FromName = $_POST['FromName'];
  $Subject = $_POST['Subject'];
  $Message = $_POST['Message'];
  $Host = $_POST['Host'];
  
  if (array_key_exists('HTML',$_POST))
  {
   $HTML = true;
   $Mail->Username=$_POST['Username'];
   $Mail->Password=$_POST['Password'];
  }
  else
  {
   $HTML = false;
  }
  
  $Mail = new phpmailer();
     
  $Mail->IsSMTP(); // send via SMTP
  $Mail->Host = $Host; //SMTP server
  
  if (array_key_exists('Username',$_POST))
  {
   $Mail->SMTPAuth=true;
  }
  else
  {
   $Mail->SMTPAuth=false;
  } 
  
  $Mail->From = $From;
  $Mail->FromName = $FromName;
  $Mail->AddAddress($To);
  $Mail->AddReplyTo($From);
  
  $Mail->WordWrap = 50; // set word wrap
  $Mail->IsHTML($HTML);
  
  $Mail->Subject  = $Subject;
  $Mail->Body = $Message;
  
  if($Mail->Send())
  {
   echo "Message Sent";
  }
  else
  {
    echo "Message Not Sent<br/>";
    echo "Mailer Error: " . $Mail->ErrorInfo;
  }
 }
?>
</body>
</html>

Open in new window

0
 
LVL 17

Expert Comment

by:psimation
ID: 34899423
So the error is clearly regarding failed authentication with your Yahoo or Hotmail account.

Also, please ensure that you are allowed to use your Yahoo or Hotmail account as smtp. I know Gmail does have an option for you to send via their smtp with proper authentication. Unfortunately, I don;t have a Yahoo or Hotmail account so I dont know if they allow this.

Do you have any other smtp account that you can use for now to at least ensure that your code is fine and all you require is to sort out Yahoo or Hotmail to get smtp?

0
 

Author Comment

by:mokatell
ID: 34899845
i have the one which belongs to my company ...

mail.xxxxxx.com .. what do u i have to know from the smtp server name ?
0
 

Author Comment

by:mokatell
ID: 34899855
oh i wanna add something .. that in the company we are connecting the smtp with the outlook !! any benefits i can get out of this ?
0
 
LVL 17

Accepted Solution

by:
psimation earned 500 total points
ID: 34903660
In your code example, to use your company smtp, simply provide the "mail.xxxxxxxx.com" as the HOST variable and then your e-mail account's username and password in the $_POST['Username'] and $_POST['Password'] variables, or type them into the form fields. That will then authenticate against your companies smtp server and allow the script to send the e-mail using that smtp server.

I'm not sure I understand your second question/comment? Your original question states that you want to be able to send yourself an e-mail from a PHP page to your outlook account.

The two are really mutually exclusive. You obviously already have a working "Outlook" where you can receive e-mail on your  myname@mycompany.com address.

With the PHPMailer class, you can send an e-mail to ANY address that is valid. The only requirement as you discovered is to provide the script with a valid SMTP account in order to actually send the e-mail. The PHPMailer class does not have it's own "e-mail sender server" built into it, therefore, it needs an smtp account.

Now, using the same smtp server as what your Outlook e-mail account is using is neither right, nor wrong. Remember, the smtp server of your company is based on a separate server from your PC/Workstation, so it really doesn't matter. Outlook on your workstation also goes and fetches the mail from your mail server using a username and password. The mail will still be sent and received. a "possible" benefit of using your existing smtp server to send mail to yourself via the PHP page is that the mail would probably get to you faster, since the smtp server delivers the mail to itself.

Please note, I have simplified my explanation a bit - in theory, you send e-mail via an smtp server, and you receive it via a POP or IMAP server. Typically though, in smaller organizations, there is one server doing both sending and receiving. So your Outlook may be configured  to receive from a different server than used for sending. But for your purposes and pertaining to the PHPMAiler class, you are really only concerned with the smtp server.
0
 

Author Comment

by:mokatell
ID: 34923923
hello experts ...
well .. i did try what u have suggested using Gmail , everything worked perfectly !
now regarding my outlook account in which i am using my company's domain .. i have to check if i have the permission to use this domain outside outlook , RIGHT ?
as conclusion if the sample i made using gmail worked , this means i dont have any problem and the thing now depends on the ISP .. right ?
0
 
LVL 17

Expert Comment

by:psimation
ID: 34924047
HI Mokatell

Yes ; if the code works now that you provide Gmail smtp details, then you are only limited by what you can/are allowed to do with your work smtp server.

In general, you should not have issues using your company smtp details, BUT, some companies will have stricter setups that will possibly check the origin of the smtp request against an IP address for instance. In such a case, your company smtp details will not work unless the PHP page is hosted on the same PC you use for Outlook.

0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
In this article I discuss my selections of the Top Four free Outlook OST File Viewers available. Open, view and read even damaged OST files by using these tools. They all provide a clear preview of all data such as emails, notes, tasks, calendars, e…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question