The PHPMailer plain text class is the best choice for including email functionality in your PHP application. It works with the majority of PHP frameworks (Laravel or Symfony are based on the SwiftMailer library, though, but it is still possible to use PHPMailer as well.) Using SMTP or a local web server, PHPMailer offers the robust capability for creating HTML emails with attachments and sending them to numerous recipients.

What is PHPMailer?

The traditional email-sending library from PHP is called PHPMailer. Direct dispatch to SMTP servers, mail(), Sendmail, and other methods of delivering email are also supported. It also lists several sophisticated features:

  • authentication for SMTP.
  • MIME safe encryption.
  • TLS and SSL protocol support.
  • combined with plain text and HTML.
  • several binaries, textual, and fs attachments.
  • support for embedded images.

PHPMailer and SMTP Email Sending

You must first install PHPMailer and set up your SMTP settings before you can send emails using SMTP and PHPMailer.

Installing PHPMailer: Steps

Up until version 5, all that was required to start a PHPMailer instance was to include the "PHPMailerAutoload.php" file in your script. Since PHPMailer 6.0 was released in August 2017, you must install it, ideally via Composer, a PHP dependency management (this method is advised by the developers of PHPMailer on Github). Add this line to your composer after installing the Composer.json file :

Or

You can manually add PHPMailer if, for instance, you're working in a testing environment and don't want to install Composer. Copy the contents of the PHPMailer plain text folder to one of the included path directories listed in your PHP settings, then manually load each class file:

Alternative: Download the PHPMailer source code files.

You can manage issues and troubleshoot them with the addition of the Exception class. Like other programming languages, PHP operates similarly. Therefore, without it, if there is a problem with your email sending code, all you will get is a notice that says, "Exception class is not found," without any information on how to troubleshoot it. Debugging will be covered separately in this post.

Configuring SMTP

How can I use PHPMailer to send test messages?

We will utilize MailSlurp, a fake SMTP server, to test PHP mail capabilities so as not to overburden our inboxes or, worse still, the inboxes of our clients. Once you make sure that everything works properly and your email messages look right, you will be able to quickly swap the SMTP settings in our samples with your real servers.

Create a free account with MailSlurp if you haven't already, then go to your Inbox and copy the values from the SMTP settings tab into your PHPMailer script

  • server host ()

  • username ( (example, generated by MailSlurp))

  • password ( (example, generated by MailSlurp))

and:

  • port (, or 465, or 2525)

SMTP vs POP3

POP-before-SMTP for authentication is the last aspect of SMTP to be discussed. Although SMTP authentication has nearly completely supplanted this mechanism, it is still occasionally applicable. The transparency of the user sending an email is its key benefit. To see an example of using POP before SMTP with PHPMailer plain text, visit this Github page.

Making use of PHPMailer to send HTML email via SMTP

Most likely, you'll build your email notice using HTML. So let's go over some actual uses for HTML properties and functions.

Message in HTML basics

Include the autoload.php file produced by Composer if you used it for installation:

If you manually installed PHPMailer, initialize it as follows:

Afterward, develop the PHPMailer class:

Indicate the PHPMailer headers:

If you want to add many addresses, create a separate command for each one:

Set a subject:

Then, using the isHTML(true) property, set the email format to HTML:

The desired material can now be entered:

Reading and sending

You can send your message in a loop to numerous recipients using PHPMailer. You can read a list from the table, in particular. In a later section of this essay, we'll go over some of these examples again, but for now, let's focus on the core idea.

You can create beautiful HTML emails with PHPMailer plain text as well as send emails that have attachments and custom formatting. For your convenience, PHPMailer plain text can convert referenced images to embedded images, read an HTML message body from an external file, and convert HTML into a simple plain-text alternative body. In this manner, you can update your HTML template independently and avoid overburdening your message-sending code with HTML. Add the following properties to incorporate a different HTML file:

Finally, list the characteristics of email sending:

You know that your code has been executed properly when you see the message "Message has been sent." Visit your MailSlurp inbox to view the delivery status and other details; your messages will arrive there in a matter of seconds.

PHP Mailer with MailSlurp

With MailSlurp, you can check your HTML code, and raw data, test your email client and ensure that neither your message nor your sending domain will be flagged as spam.

All test emails should be recorded and examined in one location.

Attachments in HTML emails

Two possibilities exist for including files in your message:

  • Include a file from your filesystem, just like you would in an email. Your files should be saved in the same directory as the script in this situation.

  • Additionally, you can inline a file within your message; this works for photos.

  • Add a string attached to the message, which will attach the data contained in the variable. You don't actually need to store it as a file because it may be pulled from a database, for instance.

Just enter a file's path to attach it. Additionally, but optionally, supply a filename; the script will use it instead:

Repeat the command to add an additional file:

Use the command to add an attachment from a string. The filename and the content should be passed:

This is an illustration of adding data from a MySQL database that is stored as a BLOB (Binary Large OBject).

You can also use a remote URL, like the one below:

Here, we employ CID attachments to embed images:

Complete message example

Let's explore an example of a complete message that includes an internal HTML body and an inlined image.

You know that your code is running properly when you see the message "Message has been sent" as a result. Visit your MailSlurp inbox to view the delivery status and other details; your messages will arrive there in a matter of seconds.

Testing sending

With MailSlurp, you can check your HTML code, and raw data, test your email client and ensure that neither your message nor your sending domain will be flagged as spam.

You can switch to standard email sending using any desired SMTP server once you have confirmed that the PHPMailer sending result meets your expectations.

Distributing emails to numerous people

In the section above, we looked at how to send the same email to several recipients while also using the CC and BCC functions. For transactional emails, it's common to need to send an updated message to a list of recipients. What then? These usage cases are also supported by PHPMailer's features!

Gmail usage instructions for PHPMailer

One of the most widely used SMTP servers that enable free email sending is Gmail. However, some fiddling with the login settings is necessary.

Encryption

The standard Gmail SMTP credentials are as follows:

Or

Utilize your Gmail address and password to access your Gmail account if you don't use two-step verification for your account.

In your account settings, you should permit less secure applications. Please be aware that this setting may not take effect for a few hours. You can occasionally need to adjust anything on the unlock CAPTCHA page.

In the event that something goes wrong, you will get the error "5.7.14." Try again after logging in using your web browser, or "Username and Password not accepted." Instructions on how to remedy it ought to be included.

Verification in Two Steps

You must generate an App password if 2-step verification is set for your Gmail account and you want to utilize it with PHPMailer. The 16-digit passcode should be entered as the value for $mail->Password. Following these Google guidelines will make it simple to generate one.

XOAuth2

Despite being the most challenging, OAuth2 is still the suggested approach. Thank goodness for oauth2-google. It is a package to facilitate the PHP League's OAuth 2.0 Client's use of Google OAuth 2.0. It supports PHP versions 7.0 through 7.3. You must first obtain a Google client ID and client secret in order to utilize it. To set things up, adhere to this Google manual. Once finished, use composer to install the package, then add the authorization information as follows:

Without using an SMTP server, send an email to PHPMailer.

The most common and useful transport option with PHPMailer is SMTP. But as we just explained, it also supports various approaches.

Use the normal Sendmail software for localhost:

Debugging PHP Mailer

The SMTPDebug command will assist you in investigating any issues you have when sending emails using an SMTP server and determining what needs to be resolved.

Enable SMTP debugging and modify your script's debug level as follows:

Debug levels

  • Level 1 (client) displays messages that the client has sent.

  • Level 2 is the suggested setting; it means client and server and will add server messages.

  • Level 3 (client, server, and connection) adds details to the first data and may be helpful in identifying STARTTLS errors.

  • Low-level information is level 4.

If level 2 doesn't work at all, try level 3 or level 4. Debugging can be turned off by setting level 0; this setting is often used for production.

More PHP Mailer examples

Let's go over a few examples to gain a better grasp of how debugging in PHPMailer functions.

1. An incorrect SMTP hostname

In this illustration, we typed mailslurp.com instead of mx.mailslurp.com as the hostname, which is incorrect, and we immediately got a notice saying that there was a problem with the server's initial communication.

Example 2. False identification

The SMTP, its hostname, and port are now correct, but a login and password combination was not identified, illustrating where the mistake occurs. As a result, you should double-check and adjust your credentials.

When you need to delve deeper into debugging and troubleshooting, look to the aforementioned in-depth articles on GitHub.

Additional information on PHPMailer

The most typical PHPMailer use cases have been covered in this article, including producing HTML emails with graphics and attachments, delivering them through SMTP or localhost to various recipient types, and providing debugging options.

Check the "examples" subdirectory in the PHPMailer docs on Github if you require more detailed examples.