Send Emails with PHPMailer and SMTP

One of the most popular PHP library that developers use to send emails in web applications.

Batoi Research Group May 1, 2021 Facebook Twitter LinkedIn Pinterest

While writing code in PHP, there are frequent occasions when we need to send emails to the users from within the web application or website. These are called transactional emails as these are usually triggered when some event occurs; i.e., when a new user is added, a new record is deleted, etc.

It is to be noted that the PHP language has a built-in function called mail() to send emails through the local mail server. But it is not capable of availing the architecture that is required for the reliability (of receiving emails and in the Inbox) and security (against spamming and vectors). For such purposes, we need to use external (specialized) mail services like Mailgun and Sendgrid, etc. We can use SMTP or ReST API to access such services online to send 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.

Download PHP Mailer. Store the files class.phpmailer.php and class.smtp.php in a folder within your application. You may use composer to automate the update of files (useful to manage dependency) or may just keep these files manually. Create include within your main PHP script, 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

Next, set the object to use SMTP.

$mail->IsSMTP(); // telling the class or object to use SMTP

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   = "************";

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); 

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!
}

Need our assistance? We are available.

Learn More About Our Platform?
Schedule a Demo
An Existing Customer?
Get Support
Want Managed Service?
Request for a Quote
Report an Error