Web Development

Send Mail via SMTP using PHPMailer

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:

[cc lang=”php”]
# 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!”;
}
[/cc]

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

You can download PHPMailer from here.

Shares:
  • […] I have posted about the send mail using SMTP with PHPMailer. Before this I have posted about the send mail using SMTP with core PHP. For sending the mails […]

    Reply
  • PHPmailer
    September 8, 2011 at 1:45 am

    […]  Send mail via SMTP using phpmailer (0 visite) […]

    Reply
  • Raul
    November 14, 2011 at 3:43 am

    …or you could try SMTP4PHP ;)

    Reply
  • Ricardo Martins
    Ricardo Martins
    February 28, 2012 at 4:33 pm

    Hello,

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

    Thanks

    Reply
    • Avinash
      February 28, 2012 at 10:32 pm

      ok will look into it…

      Reply
      • Ricardo Martins
        Ricardo Martins
        February 28, 2012 at 10:34 pm

        Ok thanks, and post the solution =D

        Reply
  • Ricardo Martins
    Ricardo Martins
    March 13, 2012 at 5:54 pm

    no solution?

    Reply
    • Avinash
      March 13, 2012 at 10:21 pm

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

      Reply
  • kiran gadhvi
    kiran gadhvi
    October 3, 2012 at 12:28 pm

    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 ..

    Reply
    • Avinash
      October 5, 2012 at 9:12 am

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

      Reply
  • kiran gadhvi
    kiran gadhvi
    October 3, 2012 at 12:31 pm

    [cc lang=”php”]
    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!”;
    }
    [/cc]
    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

    Reply
    • Avinash
      October 5, 2012 at 9:21 am

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

      [cc lang=”php”]
      if(!$mail->Send())
      {
      echo “Mailer Error: ” . $mail->ErrorInfo;
      }
      else
      {
      echo “Message sent Successfully!”;
      }
      [/cc]

      Reply
  • Kamlesh Khatti
    Kamlesh Khatti
    April 14, 2013 at 4:39 pm

    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/

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *