Learn how to send SMTP emails with PHP using a mail library.

  • Table of contents

Use PHPMailer to send emails with SMTP and MailSlurp mailservers. Configure IMAP and SMTP access in PHP using phpmailer and composer.

PHP is a great language for sending email with SMTP. PHP provides many functions for sending emails directly or by connecting to an SMTP server. In this post we will discuss the SMTP standard and different ways to send email in PHP using SMTP.

php smtp

What is the SMTP protocol?

SMTP or simple mail transfer protocol is a standard way to exchange emails between servers and code. If you wish to send email in PHP using SMTP you must first determine the server you wish to connect to. This could be the SMTP server that hosts the recipients email account or your own SMTP server that will send on your behalf.

Most mail providers run an SMTP server on ports 25, 2525, 465 or 578. MailSlurp is a free email provider with an SMTP server available on the following ports:

Protocol Host Port TLS Description
SMTP mx.mailslurp.com 2525 false SMTP server
SMTP mx.mailslurp.com 2525 false SMTP server
SMTP mailslurp.mx 587 true SMTP server
IMAP mailslurp.click 1143 false IMAP server

PHP mail sending methods

As PHP is an old and venerable language there are many ways to send emails. The oldest method is the mail() function. The mail function has been available since PHP4 but it comes with some caveats. Firstly, it requires configuration using the php.ini system files. This can be difficult to set up. Modern PHP applications typically use a higher level library like PHPMailer.

Due to the limitations of the built-in mail function most developers recommend using the open source PHPMailer library. You can install the library using composer composer require phpmailer/phpmailer. Then include the libaries in your php code using vendor autoload to send email using SMTP in PHP.

<?php
require_once(__DIR__ . '/vendor/autoload.php');

use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

Connect to SMTP servers using PHPMailer

To send SMTP emails in PHP import PHPMailer and configure an instance to use the port and hostname for your SMTP server.

$mail = new PHPMailer(true);
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->isSMTP();
$mail->SMTPSecure = '';
$mail->SMTPAuth   = true;
$mail->Host       = 'mx.host.com';
$mail->Username   = 'username';
$mail->Password   = 'your password';
$mail->Port       = 2525;

If you use MailSlurp SMTP servers you can use the getImapSmtpAccess method to obtain your username and password:

$smtpAccess = $inboxController->getImapSmtpAccess($inbox1->getId());

Sending SMTP emails using PHP

To actually send an email, first connect to the server and then build the message in parts.

$smtpAccess = $inboxController->getImapSmtpAccess($inbox1->getId());

$mail = new PHPMailer(true);
try {
    // User smtp access to configure PhpMailer for MailSlurp
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;
    $mail->isSMTP();
    $mail->SMTPAuth   = true;
    $mail->Host       = $smtpAccess->getSmtpServerHost();
    $mail->Username   = $smtpAccess->getSmtpUsername();
    $mail->Password   = $smtpAccess->getSmtpPassword();
    $mail->SMTPSecure = '';
    $mail->Port       = $smtpAccess->getSmtpServerPort();

    // test connection (secure disabled)
    $connected = $mail->smtpConnect();
    $this->assertTrue($connected);

    // write email from inbox1 to inbox2
    $mail->setFrom($inbox1->getEmailAddress());
    $mail->addAddress($inbox2->getEmailAddress());
    $mail->isHTML(true);
    $mail->Subject = 'Hello inbox2';
    $mail->Body    = 'This is an <b>HTML</b> message';

    // send the email
    $sent = $mail->send();

    $this->assertTrue($sent);
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
    throw new Exception($mail->ErrorInfo);
}

Testing email sending with PhpUnit

When sending emails from your PHP application consider testing email end-to-end using MailSlurp. MailSlurp is a free service for catching emails in disposable accounts. You can connect to servers using SMTP and then verify that emails were received:

<?php
require_once(__DIR__ . '/vendor/autoload.php');

use MailSlurp\ApiException;
use PHPMailer\PHPMailer\SMTP;
use PHPUnit\Framework\TestCase;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

/**
 * Example tests for MailSlurp (using PHP 7.1)
 */
class MailSlurp_SDK_Test extends TestCase
{

    public $config;

    /**
     * Setup mailslurp config for later use
     */
    public function setUp(): void
    {
        // get api key from environment variables (use your own key here)
        $API_KEY = getenv('API_KEY');
        $this->assertNotEmpty($API_KEY, "API Key must not be empty");

        // create configuration with api key
        $this->config = MailSlurp\Configuration::getDefaultConfiguration()->setApiKey('x-api-key', $API_KEY);
    }

    /**
     * @throws ApiException
     * @throws Exception
     */
    public function test_canSendSMTPEmail_usingPhpMailer() {
        // create an inbox
        $inboxController = new MailSlurp\Apis\InboxControllerApi(null, $this->config);
        $inbox1 = $inboxController->createInboxWithOptions(new \MailSlurp\Models\CreateInboxDto([ "inbox_type" => "SMTP_INBOX"]));
        $this->assertStringContainsString("@mailslurp.mx", $inbox1->getEmailAddress());

        $inbox2 = $inboxController->createInbox();
        $smtpAccess = $inboxController->getImapSmtpAccess($inbox1->getId());

        $mail = new PHPMailer(true);
        try {
            // User smtp access to configure PhpMailer for MailSlurp
            $mail->SMTPDebug = SMTP::DEBUG_SERVER;
            $mail->isSMTP();
            $mail->SMTPAuth   = true;
            $mail->Host       = $smtpAccess->getSmtpServerHost();
            $mail->Username   = $smtpAccess->getSmtpUsername();
            $mail->Password   = $smtpAccess->getSmtpPassword();
            $mail->SMTPSecure = '';
            $mail->Port       = $smtpAccess->getSmtpServerPort();

            // test connection (secure disabled)
            $connected = $mail->smtpConnect();
            $this->assertTrue($connected);

            // write email from inbox1 to inbox2
            $mail->setFrom($inbox1->getEmailAddress());
            $mail->addAddress($inbox2->getEmailAddress());
            $mail->isHTML(true);
            $mail->Subject = 'Hello inbox2';
            $mail->Body    = 'This is an <b>HTML</b> message';

            // send the email
            $sent = $mail->send();

            $this->assertTrue($sent);
        } catch (Exception $e) {
            echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
            throw new Exception($mail->ErrorInfo);
        }

        // wait for email to arrive in inbox2
        $waitForController = new MailSlurp\Apis\WaitForControllerApi(null, $this->config);
        $email = $waitForController->waitForLatestEmail($inbox2->getId(), 30000);
        $this->assertStringContainsString("Hello inbox2", $email->getSubject());
        $this->assertStringContainsString("<b>HTML</b>", $email->getBody());
    }
}

Why PHP is a great choice

PHP is a server-side scripting language that has been widely used for web development since its creation in 1994. One of its most popular use cases is sending emails. PHP has a number of features and advantages that make it an excellent choice for sending emails.

Firstly, PHP has built-in email functions that allow developers to easily send emails without the need for third-party libraries. The mail() function is a built-in PHP function that can be used to send emails from a PHP script. This function is simple to use and does not require any additional setup or configuration. The mail() function supports basic email functionality, such as sending text and HTML emails, as well as attaching files to emails.

Secondly, PHP has extensive support for email protocols, such as SMTP and IMAP. The PHPMailer library is a popular open-source library that provides a more advanced and feature-rich alternative to the built-in mail() function. PHPMailer supports sending emails via SMTP and includes support for sending attachments, using encryption, and other advanced features. PHPMailer also supports sending emails using third-party email services, such as Gmail and MailSlurp.

Thirdly, PHP has a large and active community of developers who contribute to the development and improvement of PHP email libraries. This community provides extensive documentation, examples, and support for PHP email libraries. Additionally, many third-party email services provide libraries and documentation for integrating their services with PHP.

Fourthly, PHP can be easily integrated with other web technologies and frameworks. For example, popular PHP frameworks such as Laravel and Symfony provide built-in support for sending emails. These frameworks make it easy to send emails using PHP and provide additional features, such as email templates and email queues!

Further reading

In conclusion, PHP is a powerful and flexible language that is well-suited for sending emails. Its built-in email functions, support for email protocols, active developer community, and ease of integration with other web technologies make it a popular choice for developers who need to send emails from their web applications.

There are many other ways to send and receive email in PHP using SMTP. See the PHP developer docs for more information.