Wednesday, August 25, 2010

Authenticated Mail using PHP

Sending mail in PHP is a generally a piece of cake every basic book or tutorial tell you a very simple PHP mail function to send email, although it send mail to whom ever you intended to send it, but there is a little problem in it that mail which is send by the php function below generally not go into the "inbox" of the intended receiver instead it goes in "junk" folder and so your receiver may be unaware of your message.

<?php
   mail(to,subject,message,headers,parameters);
?>

The solution to the above mention problem is to send an authenticated mail (if you want to learn more about what is authenticated mail wikipedia explains it nicely). When you Google the solution you will find a PEAR library that do that job, what if you don't have PEAR installed, i find a solution which really works for me, you want two thing for this
  1. SMTP Server
  2. PHP Mailer Class form http://phpmailer.sourceforge.net

i used Google SMTP server to send authenticated mail using PHP, the code i use is as follows

<?php
if(isset($_POST["submit"])){

 date_default_timezone_set('America/Toronto');

 require_once('class.phpmailer.php');

 $body=$_POST["msg"];
 $address = $_POST["to"];
 
 $mail             = new PHPMailer();
 $mail->IsSMTP(); 
 $mail->SMTPDebug  = 2;                     
 $mail->SMTPAuth   = true;                  
 $mail->SMTPSecure = "ssl";                 
 $mail->Host       = "smtp.gmail.com";      
 $mail->Port       = 465;                   
 $mail->Username   = "youremail@gmail.com"; 
 $mail->Password   = "SECRETPASSWORD";      
 $mail->SetFrom('youremail@gmail.com', 'DISPLAY_NAME');
 $mail->AddReplyTo("youremail@gmail.com","DISPLAY_NAME");
 $mail->Subject    = $_POST["subject"];
 $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!";
 $mail->MsgHTML($body);
 $mail->AddAddress($address,$_POST["to"]);

 if(!$mail->Send()) {
   echo "Mailer Error: " . $mail->ErrorInfo;
 }
 else {
   echo "Message sent!";
 }
}
?>

HTML form for this is

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Authenticated Mail Demo</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
<table width="60%" height="229" border="0">
  <tr>
    <td align="right" valign="top">From : </td>
    <td>
      <input name="from" type="text" id="from" size="55" value="youremail@gmail.com" disabled="disabled" />    </td>
  </tr>
  <tr>
    <td align="right" valign="top">To : </td>
    <td><label>
      <input name="to" type="text" id="to" size="55" />
    </label></td>
  </tr>
  <tr>
    <td align="right" valign="top">Subject : </td>
    <td><input name="subject" type="text" id="subject" size="55" /></td>
  </tr>
  <tr>
    <td align="right" valign="top">Message : </td>
    <td><textarea name="msg" id="msg" cols="55" rows="5"></textarea></td>
  </tr>
  <tr>
    <td colspan="2" align="center" valign="middle"><input type="submit" name="submit" id="submit" value="Send Email" /></td>
    </tr>
</table>
</form>
</body>
</html>

you can download all source files and dependencies from below

authenticated email using php source code
Download Authenticated Email PHP Source Code

enjoy sending authenticated email and your customer never miss a mail from you :)

2 comments:

Anonymous said...

source files and dependencies not available for download...

Originative on June 22, 2012 at 1:17 PM said...

that source was located on a free hosting site and was removed... but all the code is available in the post you can regenerate it by following the instructions in this tutorial.

Post a Comment

 

Blog Info

A Pakistani Website by Originative Systems

Total Pageviews

Tutorial Jinni Copyright © 2015 WoodMag is Modified by Originative Systems