1. Products
  2.   Email
  3.   PHP
  4.   PHPMailer 

PHPMailer 

 
 

Open Source PHP API for Email Processing

PHP library that allows to attach and send multiple files, compose and send email messages to multiple users via To, CC, BCC, and Reply-to addresses.

PHPMailer is an open source PHP email sending library that enables software developers to create applications for building and sending email messages with minimum effort and cost. It also provides support for making an SMTP connection with authentication as well as sending email messages using Gmail servers.

The library has provided the capability to send emails to multiple users via To, CC, BCC, and Reply-to addresses. It also allows users to encode email messages using UTF-8 content and 8bit, base64, binary, and quoted-printable encodings. It also enables users to attach and send multiple files with ease. 

The library has provided multiple methods for sending email messages. The library provides several important features for email management such as adding attachments to an email message, automatic email validation, error handling in over 50 languages, S/MIME and DKIM signing support, Integrated SMTP support, protection from header injection attacks, and many more.

Previous Next

Getting Started with PHPMailer

The recommended way to install PHPMailer is via Composer, please use the following command for easy installation.

Install PHPMailer API via Composer 

composer require phpmailer/phpmailer 

Send Email Message via PHP API

Sending email messages in PHP is a very common approach nowadays adopted by software developers. The PHPMailer library gives software developers the power to send email messages inside their own applications with a couple of PHP commands. The library also supports sending plain text emails to non-HTML email clients. You can also send emails from a local web server with ease. Here is a simple example that shows how software developers can compose and send email message via inside PHP applications.

How to Send a Simple Email Message via SMTP Protocol inside PHP Applications? 

//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
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   = 'user@example.com';                     //SMTP username
    $mail->Password   = 'secret';                               //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('from@example.com', 'Mailer');
    $mail->addAddress('joe@example.net', 'Joe User');     //Add a recipient
    $mail->addAddress('ellen@example.com');               //Name is optional
    $mail->addReplyTo('info@example.com', 'Information');
    $mail->addCC('cc@example.com');
    $mail->addBCC('bcc@example.com');

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

    //Content
    $mail->isHTML(true);                                  //Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body in bold!';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

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

Send Email Message to a List via PHP

The open source library PHPMailer is usually used to send an email message to mailing lists. The library is very reliable and can send emails a messages to a whole list of recipients proficiently. After setting up a PHPMailer instance using SMTP, they can connect to a MySQL database to retrieve a list of recipients. You can set your custom email messages as well as one particular one for all recipients. You can also select some specific users from the list for sending emails.

Add Multiple Attachment to Email

It is often required to share multiple files among your team members or with the customers.  The PHPMailer is a very powerful library that gives software developers the capability to send emails with multiple attachments. It uses a very simple form that accepts a file upload and emails it. On the other hand, it is a little more complex form that allows uploading multiple files at once and sends all of them as attachments to an email. Here is an example that shows how software developers can add attachments to their email messages using PHP commands.

How to Attach Files to an Email Message & Send it inside PHP Applications? 

//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
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   = 'user@example.com';                     //SMTP username
    $mail->Password   = 'secret';                               //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('from@example.com', 'Mailer');
    $mail->addAddress('joe@example.net', 'Joe User');     //Add a recipient
    $mail->addAddress('ellen@example.com');               //Name is optional
    $mail->addReplyTo('info@example.com', 'Information');
    $mail->addCC('cc@example.com');
    $mail->addBCC('bcc@example.com');

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

    //Content
    $mail->isHTML(true);                                  //Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body in bold!';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

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

Sending Email via Gmail

The open source library PHPMailer gives software developers the capability to send their email messages via Google’s Gmail service. To send emails via Gmail servers you need some more settings than normal SMTP settings. It uses id & password authentication. You can use the IMAP commands to save messages to a folder. You can also get a list of available folders or labels using IMAP commands. The following example shows, How software developers can send an email message via Gmail inside PHP applications.

How to Send Email Message via Gmail using PHP API?

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
require '../vendor/autoload.php';

//Create a new PHPMailer instance
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->SMTPAuth = true;
$mail->Username = 'username@gmail.com';
//Password to use for SMTP authentication
$mail->Password = 'yourpassword';
$mail->setFrom('from@example.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('replyto@example.com', 'First Last');
$mail->addAddress('whoto@example.com', 'John Doe');
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->msgHTML(file_get_contents('contents.html'), __DIR__);
$mail->AltBody = 'This is a plain-text message body';
$mail->addAttachment('images/phpmailer_mini.png');
if (!$mail->send()) {
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message sent!';
    //Section 2: IMAP
    //Uncomment these to save your message in the 'Sent Mail' folder.
    #if (save_mail($mail)) {
    #    echo "Message saved!";
    #}
}
 English