How to Send Emails From PHPMailer Using SMTP

There are lots of methods to send an email in core PHP. There are two most commonly used methods, which are PHP mail function and PHP SMTP mailer. Before starting let’s get some basic information about PHPMailer and PHP mail function. Even more, we will see why PHPMailer is better than the PHP mail function.

What Is the PHP Mail Function?

PHP mail function is very basic and easy to use mail configuration for sending emails from PHP websites. PHP mail function sends emails directly from the server that is not very professional and after some time that emails are getting spammed.

What Is the PHPMailer?

PHPmailer is an opensource library for sending emails via SMTP server with original email accounts. PHPmailer is compatible with PHP 5.5 and later. PHPmailer also provides additional features like Send emails with multiple To, CC, BCC and Reply-to addresses.

PHPMailer provides better security due to SSL and HTTPS support. In addition, it has fewer chances to be spam in a user inbox.

Steps to Setup PHPMailer With SMTP

Step 1: Download PHPMailer

PHPMailer is developed by an opensource community. If you want to use a phpmailer without the command line, it is available free at Github.

Step 2: Create a PHP file to Setup PHPMailer

Create a PHP file in your current working directory and copy the following PHP code in it.

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

// This path to PHPMailer should be correct

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
    //Server settings

    $mail->SMTPDebug = 2; 
    // Enable debug output

    // Set mailer to use SMTP

    $mail->Host       = ';';
    // Specify main and backup SMTP servers ex: '' for gmail

    $mail->SMTPAuth   = true;
    // Enable SMTP authentication
    $mail->Username   = '[email protected]';
    // SMTP username/ Email address

    $mail->Password   = 'password';
    // SMTP password for your Email

    $mail->SMTPSecure = 'tls';
    // Enable TLS encryption, `ssl` also accepted

    $mail->Port       = 587;
    // TCP port to connect to

    $mail->setFrom('[email protected]', 'Admin');

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

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

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

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

    // Attachments
    // Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new_img.jpg');
    // Optional name

    // Content
    // Set email format to HTML

    $mail->Subject = 'Test Subject';
    $mail->Body    = 'This is test Email';

    echo 'Email has been sent';
} catch (Exception $e) {
    echo "Email could not be sent. Mailer Error: {$mail->ErrorInfo}";



I hope you understand the complete process of sending an email using SMTP configurations in PHP. Exploring is one of the best practice to be master in programming. So explore PHPMailer and if you find something important write it in  comments.

Enjoy Coding 🙂

Leave a Reply

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

Read previous post:
php multiple file uploader using Ajax
PHP Multiple File Uploader Using Ajax

Multiple file upload in PHP using ajax script is easy to implement. Here is the simplest method to upload multiple...