How to send emails with PHPMailer

You can implement SendPulse's transactional emails with SMTP settings into your project using PHPMailer. PHPMailer is compatible with many PHP libraries and allows you to create a personalized email with an attachment and send it to your subscribers.

In the article, we will look at how to install PHPMailer, where to find the SMTP parameters in your SendPulse account, and how to set up a file to send an email.

Install PHPMailer

You can install PHPMailer via Composer or download the file and install it in your project.

If you are using Composer, add the following line to your composer.json file:

"phpmailer/phpmailer": "^6.5"

Or run the following command:

composer require phpmailer/phpmailer

If you are not using Composer, download the PHPMailer files, and add them via additional classes:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

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

Check out the installation details and a code example in the official PHPMailer documentation.

Copy Your SMTP Login Credentials

Go to the SMTP Settings > General tab in your SendPulse account. Copy the authorization values ​in SMTP: server address, port, login, and password.

You can access the SMTP settings after your profile gets activated. The “Your profile is being moderated” message will disappear from the page, and you will receive an email notifying you that your account has been approved and activated.

Configure the Object to Use SMTP

If you used Composer to install PHPMailer, add the autoload.php file:

require 'path/to/composer/vendor/autoload.php';

If you installed PHPMailer manually, initialize it like this:

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

Create a PHPMailer class and a new object:

<?php
use PHPMailer\PHPMailer\PHPMailer;

Create a new PHPMailer object:

$mail = new PHPMailer(true);

Next, add the SMTP parameters, the data about your sender and recipients, and your email content.

Add SMTP Settings

In the “Server settings” section, you need to add the data from your SMTP settings to your SendPulse account.

//Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
    $mail->isSMTP();                                            //Send using SMTP
    $mail->Host = 'smtp-pulse.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 = 'ssl';            //Enable implicit TLS encryption
    $mail->Port = 465;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

Where:

Host Server’s address
SMTPAuth Indicates whether authentication is enabled (in our case, yes, we use it, and we need to add the true value)
Username Your SendPulse email login
Password Password to your SendPulse account
SMTPSecure Encryption type (corresponds to the port)
Port The port you will use to connect to SMTP. You can choose 2525, 465, and 587

Add the Sender and Recipients

Specify the sender and recipients in the Recipients section.

//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->addCC('cc@example.com');
    $mail->addBCC('bcc@example.com');

Where:

setFrom Email sender's name and address.

The sender address must be verified in your SMTP account.

addAddress Primary recipient’s email address and name.

You can send as many emails per hour as determined by your pricing plan.

addCC Email address and name of the secondary recipient in the copy. The recipient will see other recipients.
addBCC Secondary recipient's email address and name in BCC. The recipient will not see other recipients.

Add Your Email Content

Specify the email parameters in the Content section.

To personalize the message in the parameter value, you can use your project variables.

 //Content
    $mail->isHTML(true);                                  //Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Encoding = 'base64';

    $mail->Body = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
   $mail->msgHTML(file_get_contents('contents.html'), __DIR__);

Where:

Subject Subject of your email
Body Body of your email in HTML
AltBody Text version of your email
Encoding Encodes the content of the email in the selected data format. You can use the base64 and quoted-printable values.
msgHTML Converts an HTML email version to a text version

Additionally, you can attach files to the message. Specify the file path and file name in the addAttachment parameter

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

Set the Send Attributes

At the end of the document, the send function is specified. In echo command, you can specify which message to display if the sendout is successful or when receiving a send error:

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

You can check an example of a different file configuration in the official PHPMailer documentation: Simple example and Example: SMTP.

If you're having trouble using it, you can also refer to the Troubleshooting PHPMailer Problems and view SMTP Error Codes.

Then, you will need to run a file according to your project settings on each event in order to send a transactional email.

    Rate this article about "How to send emails with PHPMailer"

    User Rating: 4 / 5

    Previous

    How to send emails via SMTP with WordPress plugins

    Popular in Our Blog

    Try SendPulse SMTP service for free