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 the PHP mail function and the 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 fundamental 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, emails are getting spammed.

What Is the PHP Mailer?

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

PHP Mailer provides better security due to SSL and HTTPS support. Also, it has fewer chances to be spam in a user inbox.

Steps to Setup PHP Mailer With SMTP

Step 1: Download PHP Mailer

An open-source community develops PHPMailer. 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 PHP Mailer

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}";


Read Also: What Is File_get_contents in PHP


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

Enjoy Coding πŸ™‚

Leave a Reply

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