Sending Email with PHP

To obtain your SMTP credentials you can create a free outbound account with CloudMailin.

Sending mail with PHP

The default PHP mail function doesn't actually have SMTP support for sending email. While it will allow sending to a local system it expects a different local configuration to perform the SMTP relay. However, it's actually very simple to use a third-party solution to send email over SMTP with PHP.

A few poplular libraries are PHPMailer, Pear Mail or SwiftMailer. Here we'll cover sending an email with SwiftMailer.

Using SwiftMailer in PHP to send Email

In this example we'll use composer to install SwiftMailer:

composer require "swiftmailer/swiftmailer:^6.0"
composer install

With SwiftMailer installed we'll include SwiftMailer in our PHP app and then create an SMTP transport to send our mail.


$hostname = 'hostname from account';
$username = 'username from account';
$password = 'password from account';

$transport = (new Swift_SmtpTransport($hostname, 587, 'tls'))

$mailer = new Swift_Mailer($transport);

When the transport is created we'll need to set 'tls' so that the connection calls STARTTLS and establishes a TLS encrypted connection before we perform authentication. Then we'll set the username and password so that the AUTH command is called to authenticate the SMTP session.

Creating an sending the email

Now we have a transport we can create and send the mail message:

$message = (new Swift_Message())
  ->setSubject('Hello from PHP SwiftMailer')
  ->setTo(['' => 'User Name']);

$headers = ($message->getHeaders())
  -> addTextHeader('X-CloudMTA-Class', 'standard');

  '<h1>hello from php</h1>'.
$message->addPart('hello from PHP', 'text/plain');

Any additional headers can be set using addTextHeader to the headers of the email message. You'll notice here we're setting the additional X-CloudMTA-Class header. More details around this can be found in the Setting SMTP queue priority and Tagging documentation.

Sending mail with Laravel

Laravel creates a config/mail.php file with the settings used to create emails over SMTP. All of these settings can be modified by settings your .env file. Since Laravel inherits it's mailer from SwiftMailer we can use similar settings to above in our ENV to set this.

// .env
MAIL_HOST=host from account
MAIL_USERNAME=username from account
MAIL_PASSWORD=password from account

In addition we also recommend creating a default from address in the config you can use something like the following for this:

// config/mail.php
'from' => ['address' => '', 'name' => 'App Name'],

For more information about actually creating your email in Laravel take a look at the make:mail command.

Queueing a message can then be done using something like the following:

    ->queue(new OrderShipped($order));

There are lots of options to use Queues and to delay sending email messages in laravel. Take a look at the Queueing Mail documentation for more information.


We should now have received a message in our inbox. Using CloudMailin we can also track the status of each of our emails and see the delivery details. Any tags you've added as a x-cloudmta-tags header can be used to filter the email messages and search.

If you need any SMTP credentials you can create an account for free and get your SMTP credentials on the outbound account page.

Make sure you changed the from address to match your verified domain and set a recipient

Outbound Dashboard Showing Message

The SMTP transaction will reply with the message-id of the message to use for bounce tracking if required.

250 Thanks! Queued as: f78b876d-dd97-4e10-9a61-067f98044cc7

Of course if there are any questions feel free to contact us and we'll do our best to help you get setup sending email.