Laravel SMTP Integration Guide

Guide to integrate Mailazy with your Laravel application


Prerequisites

You need to complete these given prerequisites, you can skip the step if you have already completed.

  1. Sign up for a Mailazy account.
  2. Complete Domain Authentication.
  3. Generate the Mailazy Access Key

Integrate Mailazy with Laravel

Laravel provides a clean API over the popular SwiftMailer library with drivers for SMTP, PHP's mail, sendmail and more. For this example, we'll be sending an email with Mailazy using the SMTP Driver. For more information, check out the docs for Laravel's Mail interface.

Laravel 5.5 LTS uses Mailable classes. Mailables in Laravel abstracts building emails with a mailable class. Mailables are responsible for collating data and passing them to views.

Before you begin

Check your .env file and configure these variables:

MAIL_MAILER=smtp
# MAIL_DRIVER=smtp # for laravel < 7
MAIL_HOST=smtp.mailazy.com
MAIL_PORT=587
MAIL_USERNAME=mailazy_apikey
MAIL_PASSWORD=mailazy_secret
MAIL_ENCRYPTION=tls
MAIL_FROM_NAME="John Smith"
MAIL_FROM_ADDRESS=from@example.com

Set the MAIL_USERNAME field to "mailazy_apikey" to inform Mailazy that you're using an API key.

The MAIL_FROM_NAME field requires double quotes because there is a space in the string.

Creating a Mailable

Categories and Unique Arguments will be stored as a “Not PII” field and may be used for counting or other operations as Mailazy runs its systems. These fields generally cannot be redacted or removed. You should take care not to place PII in this field. Mailazy does not treat this data as PII, and its value may be visible to Mailazy employees, stored long-term, and may continue to be stored after you’ve left Mailazy’s platform.

Next you need to create a Mailable class, Laravel's CLI tool called Artisan makes that a simple feat. Open CLI, go to the project directory and type:

php artisan make:mail TestEmail

This command will create a new file under app/Mail/TestEmail.php and it should look something like this:

<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class TestEmail extends Mailable
{
  use Queueable, SerializesModels;
  public  $data;
  public  function __construct($data)
  {
    $this->data  =  $data;
  }
  public  function build()
  {
    $address  = 'janeexampexample@example.com';
    $subject  = 'This is a demo!';
    $name  = 'Jane Doe';
	
	$mail = $this->view('emails.test')
      ->from($address, $name)
      ->cc($address, $name)
      ->bcc($address, $name)
      ->replyTo($address, $name)
      ->subject($subject)
      ->with([ 'test_message' =>  $this->data['message'] ]);
	  
	  if(!empty($this->data["attachments"])){
            foreach($this->data["attachments"] as $k => $v){
                $mail = $mail->attach($v["path"], [
                    'as' => $v["as"],
                    'mime' => $v["mime"],
                ]);
            }
        }
	return $mail;
  }
}

In Laravel Views are used as 'templates' when sending an email. Let's create a file under app/resources/views/emails/test.blade.php and insert this code:

<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8" />
</head>
<body>
<h2>Test Email</h2>
<p>{{ $test_message }}</p>
</body>
</html>

Sending an email

Now that we have our Mailable Class created, all we need to do is run this code:

<?php
use App\Mail\TestEmail;
$data  = ['message' => 'This is a test!'];
Mail::to('john@example.com')->send(new TestEmail($data));

Sending an email with attachments

Now that we have our Mailable Class created with attachments, all we need to do is run this code:

<?php
use App\Mail\TestEmail;
$data  = ['message' => 'This is a test!',
			"attachments" => [
				[
					"path" => public_path('uploads/invoice.pdf'),
					"as" => "Purchase Invoice.pdf",
					"mime" => "application/pdf",
				],
			],
		];
Mail::to('john@example.com')->send(new TestEmail($data));