Multiple ways to send email in Laravel 5

Tags

, , ,


Today I converted a Laravel 5.2 application to use Sparkpost so we could track delivery of email to clients. Prior to this the software application was coded to use Swift Mail which worked great, and that was implemented when Mailchimp wanted to force everyone to a paid plan. Prior to MailChimp, the app was using the PHP mail() method. So its seen it fair share of email delivery implementations!

Sending email in Laravel is dead easy and below I am going to show you 3 ways to do it.

Method 1

First example is using the basic PHP mail() function. This works in any PHP application.

$subject = "Test Email";
$body = "<html><body><h1>Test email</h1></body></html>";
$to = "test@test.com";
$headers = "From: me@test.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to, $subject, $body, $headers);

As you can see the $headers variable has 3 parts and all parameters are sent in the mail() function. Very basic and uses the local SMTP server to send.

Method 2

Next, using swift_mailer in a Laravel Jobs class. I’ve simplified the class down from a working example, made the variables local to the handle() function just to reduce clutter and emptied out the constructor for this example.

<?php namespace App\Jobs;
use App\Jobs\Job;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;

use Swift_Mailer;
use Swift_Message;
use Swift_SmtpTransport;

class SendTestEmailJob extends Job implements SelfHandling, ShouldQueue
{
use InteractsWithQueue, SerializesModels;

  public function __construct()
  {
  }

  public function handle();
  {
    $transport = Swift_SmtpTransport::newInstance('localhost', 25);
    $mailer = Swift_Mailer::newInstance($transport);
    $subject = "Test Email";
    $body = "<html><body><h1>Test email</h1></body></html>";
    $to = "test@test.com";
    $from = "me@test.com";
    $message = Swift_Message::newInstance($subject);
    $message->setFrom( $from );
    $message->setBody( $body, 'text/html' );
    $message->setTo( $to );
    $mailer->send( $message );
  }
}

Method 3

The third example uses the Sparkpost API to send, to implement this you need to add your API key to your .env file. Then from the base directory of your Laravel project where the composer.json file is, run the composer commands below to load in the Sparkpost packages:

composer require guzzlehttp/guzzle
composer require php-http/guzzle6-adapter
composer require sparkpost/sparkpost

The Job Class looks similar but the use statements to bring in the Swift Mailer packages are gone and replaced with our new packages and the mail transport call is replaced.

<?php namespace App\Jobs;
use App\Jobs\Job;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;

use SparkPost\SparkPost;
use GuzzleHttp\Client;
use Ivory\HttpAdapter\Guzzle6HttpAdapter;

class SendTestEmailJob extends Job implements SelfHandling, ShouldQueue
{
use InteractsWithQueue, SerializesModels;

  public function __construct()
  {
  }

  public function handle()
  {
    $from = "me@test.com";
    $subject = "Test Email";
    $body = "<html><body><h1>Test email</h1></body></html>";
    $to = "test@test.com";
    $key = getenv('SPARKPOST_API_KEY');
    $httpAdapter = new Guzzle6HttpAdapter(new Client());
    $sparky = new SparkPost($httpAdapter, ['key'=>$key]);
    $sparky->transmission->send([
      'from'=>$from,
      'html'=>$body,
      'subject'=>$subject,
      'recipients'=>['address'=>['email'=>$to]]
      ]
    ]);
  }
}

To send to multiple recipients use:

    'recipients'=>[
           ['address'=>['email'=>$to]],
           ['address'=>['email'=>$you]]
         ]

Thats it till Sparkpost starts charging and we need to move to yet another mail service!

-oOo-