Send Mail via SMTP using PHPMailer

  • Avinash
  • 13
  • Aug 31, 2011
  • Web Development

PHP has several method to send the mail and sending the mail using SMTP settings is one of them.
Before some days I have posted one article which shows how to send the mail using SMTP. In this post I will going to explain Send Mail using PHPMailer with SMTP settings.

Earlier post was about send mail using SMTP with PEAR::Mail package.

Let’s see how you can send mail using SMPT with PHPMailer:

# Include PHP Mailer Class require_once("class.phpmailer.php"); # Create object of PHPMailer $mail = new PHPMailer(); // Inform class to use SMTP $mail->IsSMTP(); // Enable this for Testing $mail->SMTPDebug = 2; // Enable SMTP Authentication $mail->SMTPAuth = true; // Host of the SMTP Server $mail->Host = "host.smtpserver.com"; // Port of the SMTP Server $mail->Port = 25; // SMTP User Name $mail->Username = "[email protected]"; // SMTP User Password $mail->Password = "user_pass"; // Set From Email Address $mail->SetFrom("[email protected]", "From Name"); // Add Subject $mail->Subject = "PHPMailer SMTP Testing"; // Add the body for mail $body = "This is the mail body"; $mail->MsgHTML($body); // Add To Address $to "[email protected]"; $mail->AddAddress($to, "SMTP Test"); // Finally Send the Mail if(!$mail->Send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message sent Successfully!"; }
# Include PHP Mailer Class
require_once("class.phpmailer.php");

# Create object of PHPMailer
$mail = new PHPMailer();

// Inform class to use SMTP
$mail->IsSMTP();

// Enable this for Testing
$mail->SMTPDebug  = 2;

// Enable SMTP Authentication
$mail->SMTPAuth   = true;

// Host of the SMTP Server
$mail->Host = "host.smtpserver.com";

// Port of the SMTP Server
$mail->Port = 25;

// SMTP User Name
$mail->Username   = "[email protected]";

// SMTP User Password
$mail->Password = "user_pass";

// Set From Email Address
$mail->SetFrom("[email protected]", "From Name");

// Add Subject
$mail->Subject    = "PHPMailer SMTP Testing";

// Add the body for mail
$body = "This is the mail body";
$mail->MsgHTML($body);

// Add To Address
$to "[email protected]";
$mail->AddAddress($to, "SMTP Test");


// Finally Send the Mail
if(!$mail->Send())
{
  echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
  echo "Message sent Successfully!";
}

As I have used the both PEAR::Mail and PHPMailer, and I found that PHPMailer is very easy to learn and use.

You can download PHPMailer from here.

Related Posts

Written by Avinash

Avinash Zala is leading various projects which deals with the various technology involved with the web. A combination of perfect technical and management skills. Avinash would like to chat with you and convert your imagination into the working system. You can get in touch with him on Facebook and Twitter.

View all posts by:

  • Pingback: PHPMailer with PHP Send Mail | Expert PHP Developer

  • Pingback: PHPmailer

  • https://sourceforge.net/projects/smtp4php/ Raul

    …or you could try SMTP4PHP ;)

  • Ricardo Martins

    Hello,

    your’s emails when are send to hotmail, they go to the spam?

    Thanks

    • http://www.xpertdeveloper.com Avinash

      ok will look into it…

      • Ricardo Martins

        Ok thanks, and post the solution =D

  • Ricardo Martins

    no solution?

    • http://www.xpertdeveloper.com Avinash

      Passing from very busy schedule, not found time for the same.

  • kiran gadhvi

    is it possible with youe code to set unsubscribe link in the mail code .and when reciever click on that link then he gets unsubcribed.i have problem with your code when i used them for unsubscribe link.plz help me ..

    • http://www.xpertdeveloper.com Avinash

      It’s totally dependent on your project to how you manage your subscription. Above code is just to send the email..

  • kiran gadhvi
    ini_set('max_execution_time', 300); /*echo $_SESSION["Fromemail"]; echo ""; echo $_SESSION["pwd"]; echo "";*/ $date=date("Y-m-d"); include_once("class.php"); $c = new connection(); $c->connect(); include('PHPMailer_v5.1/class.phpmailer.php'); $mail = new PHPMailer(); $mail->IsSMTP(); // telling the class to use SMTP $mail->SMTPAuth = true; // enable SMTP authentication $mail->SMTPSecure = "ssl"; // sets the prefix to the servier $mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server $mail->Port = 465; // set the SMTP port for the GMAIL server $mail->Username = $_SESSION["Fromemail"]; // GMAIL username $mail->Password = $_SESSION["pwd"]; // GMAIL password for($i=0; $iSetFrom($_SESSION["Fromemail"]); $mail->AddReplyTo($_SESSION["Fromemail"]); $mail->Subject = "Do not Miss it,You have great Offer at Pricepointshop"; $mail->AltBody = "Mail contents"; $mail->MsgHTML($_SESSION["Messege"]."If you would like to stop receiving these emails please Click Here Unsubscribe"); $to = $_REQUEST["checkbox"][$i]; $mail->AddAddress($to); } if(!$mail->Send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message sent Successfully!"; }
    ini_set('max_execution_time', 300);
        /*echo $_SESSION["Fromemail"];
        echo "";
        echo $_SESSION["pwd"];
        echo "";*/

        $date=date("Y-m-d");
        include_once("class.php");
        $c = new connection();
        $c->connect();
        include('PHPMailer_v5.1/class.phpmailer.php');
     
        $mail = new PHPMailer();
        $mail->IsSMTP(); // telling the class to use SMTP
        $mail->SMTPAuth = true; // enable SMTP authentication
        $mail->SMTPSecure = "ssl"; // sets the prefix to the servier
        $mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
        $mail->Port = 465; // set the SMTP port for the GMAIL server
       
        $mail->Username = $_SESSION["Fromemail"]; // GMAIL username
        $mail->Password = $_SESSION["pwd"]; // GMAIL password
       
                for($i=0; $iSetFrom($_SESSION["Fromemail"]);
                        $mail->AddReplyTo($_SESSION["Fromemail"]);
                        $mail->Subject = "Do not Miss it,You have great Offer at Pricepointshop";
                        $mail->AltBody = "Mail contents";
                        $mail->MsgHTML($_SESSION["Messege"]."If you would like to stop receiving these emails please Click Here <a href='http://a2zoffer.com/adminoffice/kimpextest/kiran/email_unsbscribe/unsubscribe.php?url=".$_REQUEST["checkbox"][$i]."' rel="nofollow">Unsubscribe</a>");
                        $to = $_REQUEST["checkbox"][$i];
                        $mail->AddAddress($to);
                       
                       
                }  

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

    this is my code when reciever clicks on unsuscribe click then the last person to whom mail sent wil get unsuscribe .this happens with all recivers.your help will be appreciated.thank u

    • http://www.xpertdeveloper.com Avinash

      At first look you Mail sending code (below), needs to be inside the for Loop.

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

    Send mail by using PHPMailer via smtp, basic with authentication and attachments
    Many of times php mail function does not send mail from our website to customer that time
    we use an other option, SMTP (Simple Mail Transfer Protocol) for sending mail. It is very easy to configure and integrate with our site.
    http://codelibrary.googleplus.co.in/send-mail-by-using-phpmailer-via-smtp-basic-with-authentication-and-attachments/