Home » Support » Help Articles » Send Email with PHPMailer and SMTP

Send Email with PHPMailer and SMTP

Back to Help Articles Home


Websites and web applications built on PHP language mostly send transactional emails. PHP has a built-in function called mail() to send an email from the web server itself. However, there are some drawbacks to use this function. So, it is recommended to use an external mail server to handle the transactional emails.

PHPMailer is a third party library that can be used to send transactional emails by SMTP.

To use SMTP in your PHP code, follow the steps below.

  1. Download PHP Mailer.
  2. Add the files 'class.phpmailer.php' and 'class.smtp.php' to your server.
  3. Include the library and instantiate a PHPMailer object.
    require_once('class.phpmailer.php');
    include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
    $mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
  4. Next, set the object to use SMTP.
    $mail->IsSMTP(); // telling the class or object to use SMTP
  5. Next, add configuration settings to point the object to the server.
    $mail->SMTPDebug  = 1;   // enables SMTP debug information (for testing)
    $mail->SMTPAuth   = true; // enable SMTP authentication
    $mail->Host       = "smtp.mandrillapp.com"; // sets the SMTP server
    $mail->Port       = 25;  // set the SMTP port for the GMAIL server
    $mail->Username   = "xxxx.#####@abc.com"; // SMTP account username
    $mail->Password   = "************";
  6. Next, set the particulars of the email you want to send.
    $mail->AddReplyTo($email, stripslashes($name));
    $mail->AddAddress($sReciverEmail, $sReciverName);
    $mail->SetFrom($email, stripslashes($name));
    $mail->Subject = 'Subject of Email';
    $mail->MsgHTML($sEmailBody);
    if($sUploadFile) $mail->AddAttachment($sUploadFile); 
  7. Finally, call the send() method. If the method returns true, then email is sent successfully else then there is a problem.
    if($mail->Send()) {
      print "Email sent successfully";
    } else {
      echo "Email Error: " . $mail->ErrorInfo;
    }

The complete code to send email using PHPMailer and SMTP is below.

require_once('class.phpmailer.php');
include("class.smtp.php");
			
$mail = new PHPMailer(true);
			
$mail->IsSMTP();
			
try {
  $mail->SMTPDebug  = 1;
  $mail->SMTPAuth   = true;
  $mail->Host       = "smtp.mandrillapp.com";
  $mail->Port       = 25;
  $mail->Username   = "xxxx.#####@abc.com";
  $mail->Password   = "************";
  $mail->AddReplyTo($email, stripslashes($name)); 
  $mail->AddAddress($sReciverEmail, $sReciverName);
  $mail->SetFrom($email, stripslashes($name));
  $mail->Subject = 'Subject of Email';
  $mail->MsgHTML($sEmailBody);
  if($sUploadFile) $mail->AddAttachment($sUploadFile); 
  $mail->Send();
} catch (phpmailerException $e) {
  echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
  echo $e->getMessage(); //Boring error messages from anything else!
}


Updated on Aug 31, 2016

The techReview is an online magazine by Batoi and publishes articles on current trends in technologies across different industry verticals and areas of research. The objective of the online magazine to provide an insight into cutting-edge technologies in their evolution from labs to market.

Visit techReview


English - IN (USD)
New Users? Signup.     Existing Users? Login.