Send Email using SMTP

Pre-requisites: Basic Understanding HTML, PHP and OOP.


In this project:

  • Send an email using SMTP.
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;    //Enable verbose debug output
    $mail->isSMTP();                          //Send using SMTP
    $mail->Host       = 'smtp.example.com';   //Set the SMTP server to send through
    $mail->SMTPAuth   = true;                 //Enable SMTP authentication
    $mail->Username   = '[email protected]';   //SMTP username
    $mail->Password   = 'pwd';             //SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
    $mail->Port       = 465; 
//TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

    //Recipients
    $mail->setFrom('[email protected]', 'Mailer');
    $mail->addAddress('[email protected]', 'Smith');     //Add a recipient
    $mail->addAddress('[email protected]');               //Name is optional
    $mail->addReplyTo('[email protected]', 'Message');
    $mail->addCC('[email protected]');
    $mail->addBCC('[email protected]');

    //Attachments
    $mail->addAttachment('/var/tmp/file.tar.gz');         //Add attachments
    $mail->addAttachment('/tmp/image.jpeg', 'new.jpeg');    //Optional name

    //Content
    $mail->isHTML(true);                                  //Set email format to HTML
    $mail->Subject = 'Mail subject';
    $mail->Body    = 'HTML message body';
    $mail->AltBody = 'Message body in plain text';
    $mail->send();
    echo 'Message is sent';
} catch (Exception $e) {
    echo "Message is not sent. Error: {$mail->ErrorInfo}";
}
?>

  

Code Explanation of mail.php

use PHPMailerPHPMailerPHPMailer;

      use PHPMailerPHPMailerSMTP;

      use PHPMailerPHPMailerException;
  • Import above mentioned PHPMailer classes into the global namespace.
  • These must be at the top of your script, not inside a function.
require 'vendor/autoload.php';
  • Load Composer's autoloader.
$mail = new PHPMailer(true);
  • $mail object is created for the predefined PHPMailer Class.
  • Passing true as an argument will enable the exceptions.
$mail->Host       = 'smtp.example.com';  

      $mail->SMTPAuth   = true;                 

      $mail->Username   = '[email protected]';   

      $mail->Password   = 'pwd';    

        

  • Set the SMTP server.
  • Enable SMTP authentication.
  • SMTP username and password.
$mail->setFrom('[email protected]', 'Mailer');

      $mail->addAddress('[email protected]', 'Smith');     //Add a recipient

      $mail->addAddress('[email protected]');               //Name is optional

      $mail->addReplyTo('[email protected]', 'Message');

      $mail->addCC('[email protected]');

      $mail->addBCC('[email protected]');
  • Add the recipient addresses using above mentioned options.
  • We can easily add more than one recipient for our mailer.
$mail->Subject = 'Mail subject';

      $mail->Body    = 'HTML message body';

      $mail->AltBody = 'Message body in plain text';

      $mail->send();
  • Mail Subject and Body is defined according the mail requirement using above methods.
  • Send() method is used to send the mailer via SMTP.

 


Summary

In this project, we have learned to send an email using SMTP.

Predefined classes are imported and an instance of PHPMailer is created and is used to define the mail parameters and finally then mail is sent using send() method.

Help Us to Improve our content

Let's Talk
Go back to Previous Course