Link to home
Start Free TrialLog in
Avatar of mokatell
mokatell

asked on

PHP E-mail

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 !
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

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

Avatar of mokatell
mokatell

ASKER

ok fine .. if i wanna use the hotmail server ,
what can i do in this case ?
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?
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/

Fatal error: Call to a member function Send() on a non-object


this is the error i am getting !!
please post your code.
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

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?

i have the one which belongs to my company ...

mail.xxxxxx.com .. what do u i have to know from the smtp server name ?
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 ?
ASKER CERTIFIED SOLUTION
Avatar of psimation
psimation
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 ?
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.