A guide to PHP Mailer

PHPMailer compose mail tutorial. Send email with PHP and composer packages instead of the inbuilt mail commands.

The PHPMailer class is the best solution for adding email capabilities to your PHP application. Many PHP frameworks are compatible with it. PHPMailer is a sophisticated tool for creating HTML emails with documents and sending them to numerous recipients through SMTP or a local web host.

How We Define PHPMailer?

The PHPMailer package is the industry standard for sending emails using PHP. In addition to mail(), Sendmail, Gmail, as well as direct dispatching to SMTP servers it offers a number of other methods for delivering email. In addition, there are a number of other options:

  • TLS but also SSL support for SMTP with authentication/MIME encryption in SMTP
  • Normal text plus multiple fs attachments are also included in this document. capability for inserted pictures
  • Sending a Message through Electronic Mail

PHPMailer: Installation Guide

Up to PHPMailer version 5, the "PHPMailerAutoload.php" file provided by the software was sufficient to generate a PHPMailer session in your script. If you want to use the latest version of PHPMailer, which was released in August 2017, you'll use Composer, dependency management for PHP, to install it. Add the following line into your composer.json file once you have installed Composer:

"phpmailer/phpmailer": "~6.1"

Guide on How to Send a Message:

So that we don't overload our own or our customers' inboxes with spam, we'll utilize a dummy SMTP server called MailSlurp. It will be easy to swap the SMTP parameters in our examples using your actual servers after you've checked that everything actually works but that the personal emails appear correct. To use it, Open your Inbox as well as paste the following parameters into your PHPMailer script from the SMTP settings menu.

Multiple-recipient emailing

In the last section, we looked at how to send the same email to several recipients, including those on the recipient's CC as well as BCC lists. It's rather uncommon for transactional emails to need sending a customized message to a group of recipients. It's possible to utilize PHPMailer for these kinds of scenarios, too!

<?php
use PHPMailer\PHPMailer\PHPMailer;
require 'path/to/composer/vendor/autoload.php';
$mail = new PHPMailer;
$body = file_get_contents('contents.html');
$mail->isSMTP();
$mail->Host = 'smtp.mailslurp.com';
$mail->SMTPAuth = true;
$mail->Username = '1a2b3c4d5e6f7g'; //generated by MailSlurp
$mail->Password = '1a2b3c4d5e6f7g'; //generated by MailSlurp
$mail->SMTPSecure = 'tls';
$mail->Port = 2525;
$mail->SMTPKeepAlive = true; // add it to keep SMTP connection open after each email sent

$mail->setFrom('list@example.com', 'List manager');
$mail->Subject = "New MailSlurp mailing list";

$users = [
  ['email' => 'max@example.com', 'name' => 'Max'],
  ['email' => 'box@example.com', 'name' => 'Bob']
];

foreach ($users as $user) {
  $mail->addAddress($user['email'], $user['name']);

  $mail->Body = "<h2>Hello, {$user['name']}!</h2> <p>How are you?</p>";
  $mail->AltBody = "Hello, {$user['name']}! \n How are you?";

  try {
      $mail->send();
      echo "Message sent to: ({$user['email']}) {$mail->ErrorInfo}\n";
  } catch (Exception $e) {
      echo "Mailer Error ({$user['email']}) {$mail->ErrorInfo}\n";
  }

  $mail->clearAddresses();
}

$mail->smtpClose();

Debugging

The SMTPDebug command may be used to find out what's wrong if you're having issues delivering emails using an SMTP server. The following commands will enable SMTP debugging in your script and raise the debug level to:

$mail->SMTPDebug = 2;
  • Emails sent from the client will be shown at level 1.
  • It is advised that you choose the level 2 option, which enables both client and server messages.
  • Client, server, as well as pconnection, are all included at the third level, which may be helpful in determining whether or not STARTTLS has failed.
  • Level 4 information is at the lowest possible level.

If you can't connect at all, try level 3 or level 4. Debugging is disabled when the level is set to 0, which is the default in production.

What else is important for you to learn about PHPMailer

In this lesson, we went through some of the most popular applications of PHPMailer. These applications include writing HTML emails with photos and attachments, sending those emails over SMTP or localhost to a variety of recipients, and going over the available options for debugging.