A guide to PHP Mailer

A guide to PHP Mailer

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!

use PHPMailer\PHPMailer\PHPMailer;
require 'path/to/composer/vendor/autoload.php';
$mail = new PHPMailer;
$body = file_get_contents('contents.html');
$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 {
      echo "Message sent to: ({$user['email']}) {$mail->ErrorInfo}\n";
  } catch (Exception $e) {
      echo "Mailer Error ({$user['email']}) {$mail->ErrorInfo}\n";




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.

Related content

Fake SMTP servers for safe testing that prevent any outbound emails from reaching real users

Test emails safely with no risk of spamming users with real emails

Java email library for SMTP and creating email addresses

MailSlurp Java SDK for sending and receive email and attachments on the JVM.

SMTP email application libraries

How to send email in CSharp, PHP, Ruby, Java, Python, and NodeJS using the SMTP protocol and MailSlurp mailservers.

1111 SMTP Connections failing

1111 SMTP Connections failing

How to Send an Email in Python

Send emails using SMTP mailer in Python. Detailed tutorial for sending mail in Python.

How to send emails in Javascript (NodeJS)

JS SMTP email sending guide using HTML and NodeJS.

PHPMailer tutorial (Sending email with PHP)

PHPMailer tutorial (Sending email with PHP)

How to send an email using Powershell (Windows and cross-platform)

Use Send-MailMessage in Windows Powershell to send emails using an SMTP server or MailSlurp's free email API.

How to send email in R

RLang tutorial for sending emails over SMTP.

SMTP Commands and Response Codes List

A full list of commands and response status codes for SMTP mail servers when sending emails.

Test SMTP servers with Telnet

How to use Telnet to test the SMTP servers?

What Is Amazon Simple Storage Service (SES) - And How Can I Use It?

Amazon SES is an email service among virtual servers, AI apps, and libraries. Ideally, you can integrate your email system with your program hosted on Amazon.

Grab all details about an SMTP Port

What are common SMTP mail server ports and how do you find them?

Which SMTP Port Should I Use?

Picking the right port for SMTP mailservers

Avoiding email spam filters

How to send emails that don't trigger a spam warning.

BCC and CC in SMTP emails

What is cc and bcc in email? What is the difference between cc and bcc?

BCC For Email: What It Means And When To Use It

What is BCC email field (blind carbon copy) and how does it differ to CC?

What Is The Best SMTP Port To Use?

MailServers use TCP ports to transmit emails. Picking the right port between 25 vs 2555, 465 vs 587 in SMTP is easy with this guide.

How to configure Mail desktop clients with MailSlurp

How to setup MailSlurp with Outlook, Gmail, Thunderbird, Mail app and more.

Send email with CSharp using SMTP Client and MailSlurp

Create a custom SMTP client and access MailSlurp inboxes from CSharp/DotNET.

DMARC – All Facts that you should know

DMARC – All Facts that you should know

Email size limits

Email size limits

The Fundamentals Of Email Subdomains

How to manage email subdomain custom mailboxes using SMTP/IMAP mailserver settings.

How to send emails in VBA

Send email in Visual basic

A guide to PHP Mailer

A guide to PHP Mailer

SMTP IMAP Settings

How to access MailSlurp email accounts using IMAP and SMTP servers

SMTP Relay for outbound smtp servers

How to use SMTP relay to address spam and security issues that affect outbound mail servers.

SMTP vs HTTP MailSlurp inboxes

The difference between HTTP and SMTP inboxes and domains in MailSlurp.

What are SMTP ports

What are SMTP ports

CSharp send SMTP email

How to use CSharp SMTP client to send email with MailSlurp mail server

Send SMTP email with Java

How to use Java SMTP client to send email with MailSlurp mail server on the JDK

Send emails in NodeJS using SMTP

How to use Javascript SMTP client (or Nodemailer) to send email with MailSlurp mail server

Send SMTP emails with PHP

Use PHPMailer to send emails with SMTP and MailSlurp

Send SMTP email with Python (using SMTPLib)

How to send emails with Python's built-in SMTP client and MailSlurp mail servers.

Ruby send SMTP email

How to use Ruby SMTP client to send email with the MailSlurp mail servers

Ready to dive in?Start building email applications today.