sunaoka/laravel-ses-template-driver

Amazon SES template mail driver for Laravel.

v3.3.1 2023-04-06 02:09 UTC

README

Latest License PHP Laravel Test codecov

A Mail Driver with support for Using templates to send personalized emails with the Amazon SES API.

Version Compatibility

Laravel Amazon SES template mail driver
5.7.x 1.x
5.8.x 1.x
6.x 1.x
7.x 2.x
8.x 2.x
9.x 3.x
10.x 3.x

Installation

Laravel 5.7.x, 5.8.x, 6.x

composer require sunaoka/laravel-ses-template-driver:'^1.0'

Laravel 7.x, 8.x

composer require sunaoka/laravel-ses-template-driver:'^2.0'

Laravel 9.x, 10.x

composer require sunaoka/laravel-ses-template-driver

Next, set the following in config/mail.php and config/services.php.

config/mail.php

Laravel 5.7.x, 5.8.x, 6.x

'driver' => 'ses.template',

Laravel 7.x, 8.x, 9.x, 10.x

'default' => 'sestemplate',

'mailers' => [
    'sestemplate' => [
        'transport' => 'sestemplate',
    ],
],

config/services.php

'ses' => [
    'key'    => 'your-ses-key',
    'secret' => 'your-ses-secret',
    'region' => 'ses-region',  // e.g. us-east-1
],

If you need to include additional options when executing the SES SendTemplatedEmail request, you may define an options array within your ses configuration:

'ses' => [
    'key'     => 'your-ses-key',
    'secret'  => 'your-ses-secret',
    'region'  => 'ses-region',  // e.g. us-east-1
    'options' => [
        'ConfigurationSetName' => 'MyConfigurationSet',
        'Tags' => [
            [
                'Name'  => 'foo',
                'Value' => 'bar',
            ],
        ],
    ],
],

Basic usage

use Sunaoka\LaravelSesTemplateDriver\Mail\SesTemplate;

class Foo
{
    public function sendmail()
    {
        $templateName = 'MyTemplate';
        $templateData = [
            'name'           => 'Alejandro',
            'favoriteanimal' => 'alligator',
        ];

        \Mail::to('alejandro.rosalez@example.com')
            ->cc('cc@example.com')
            ->bcc('bcc@example.com')
            ->send(new SesTemplate($templateName, $templateData));
    }
}

Options

Set Reply-to header

use Sunaoka\LaravelSesTemplateDriver\Mail\SesTemplate;

class Foo
{
    public function sendmail()
    {
        $templateName = 'MyTemplate';
        $templateData = [
            'name'           => 'Alejandro',
            'favoriteanimal' => 'alligator',
        ];
        $options = [
            'from' => [
                'address' => 'alejandro.rosalez@example.com', // required
                'name'    => 'Alejandro Rosalez',             // optional
            ],
            'reply_to' => [
                'address' => 'alejandro.rosalez@example.com', // required
                'name'    => 'Alejandro Rosalez',             // optional
            ],
        ];

        \Mail::to('alejandro.rosalez@example.com')
            ->cc('cc@example.com')
            ->bcc('bcc@example.com')
            ->send(new SesTemplate($templateName, $templateData, $options));
    }
}

To send a templated email to a single destination

{
  "Template": {
    "TemplateName": "MyTemplate",
    "SubjectPart": "Greetings, {{name}}!",
    "HtmlPart": "<h1>Hello {{name}},</h1><p>Your favorite animal is {{favoriteanimal}}.</p>",
    "TextPart": "Dear {{name}},\r\nYour favorite animal is {{favoriteanimal}}."
  }
}

Not supported, to send a templated email to multiple destinations.

Artisan Console Commands

Lists the email templates present in your Amazon SES account in the current AWS Region.

Options

php artisan ses-template:list-templates --help
Description:
  Lists the email templates present in your Amazon SES account in the current AWS Region

Usage:
  ses-template:list-templates [options]

Options:
      --name            Sort by the name of the template [default]
      --time            Sort by the time and date the template was created
      --asc             Sort by ascending order [default]
      --desc            Sort by descending order
      --json            The output is formatted as a JSON string

Output Text format

php artisan ses-template:list-templates
+----+-------------+---------------------------+
| No | Name        | Created At                |
+----+-------------+---------------------------+
| 0  | MyTemplate  | 2020-11-24T15:01:21+00:00 |
| 1  | MyTemplate2 | 2020-11-24T15:01:25+00:00 |
+----+-------------+---------------------------+

Enter a number to display the template object:
> 0

TemplateName:
MyTemplate

SubjectPart:
Greetings, {{name}}!

TextPart:
Dear {{name}},\r\nYour favorite animal is {{favoriteanimal}}.

HtmlPart:
<h1>Hello {{name}},</h1><p>Your favorite animal is {{favoriteanimal}}.</p>

Output JSON format

php artisan ses-template:list-templates --json
{
  "TemplatesMetadata": [
    {
      "Name": "MyTemplate",
      "CreatedTimestamp": "2020-11-24T15:01:21+00:00"
    },
    {
      "Name": "MyTemplate2",
      "CreatedTimestamp": "2020-11-24T15:01:25+00:00"
    }
  ]
}

Displays the template object for the template you specify

Options

php artisan ses-template:get-template --help
Description:
  Displays the template object for the template you specify

Usage:
  ses-template:get-template [options] [--] <TemplateName>

Arguments:
  TemplateName          The name of the template to retrieve

Options:
      --json            The output is formatted as a JSON string

Output Text format

php artisan ses-template:get-template MyTemplate
TemplateName:
MyTemplate

SubjectPart:
Greetings, {{name}}!

TextPart:
Dear {{name}},\r\nYour favorite animal is {{favoriteanimal}}.

HtmlPart:
<h1>Hello {{name}},</h1><p>Your favorite animal is {{favoriteanimal}}.</p>

Output JSON format

php artisan ses-template:get-template MyTemplate --json
{
  "Template": {
    "TemplateName": "MyTemplate",
    "SubjectPart": "Greetings, {{name}}!",
    "HtmlPart": "<h1>Hello {{name}},</h1><p>Your favorite animal is {{favoriteanimal}}.</p>",
    "TextPart": "Dear {{name}},\r\nYour favorite animal is {{favoriteanimal}}."
  }
}

Reference