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 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 opensource library for sending emails via 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.

<?php
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

    $mail->isSMTP();
    // Set mailer to use SMTP

    $mail->Host       = 'smtp1.demo.com; smtp2.demo.com';
    // Specify main and backup SMTP servers ex: 'smtp.gmail.com' 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

    //Recipients
    $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
    $mail->addAttachment('/var/tmp/new_file.tar.gz');
    // Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new_img.jpg');
    // Optional name

    // Content
    $mail->isHTML(true);
    // Set email format to HTML

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

    $mail->send();
    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

Conclusion

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 *