Skip to content

Instantly share code, notes, and snippets.

@tuanpht
Last active May 30, 2022 03:49
Show Gist options
  • Save tuanpht/83d021434086878d30ef8e4516cc11b5 to your computer and use it in GitHub Desktop.
Save tuanpht/83d021434086878d30ef8e4516cc11b5 to your computer and use it in GitHub Desktop.
PHP/Laravel AWS SES multiple emails multiple recipients
<?php
use Aws\Ses\SesClient;
use Illuminate\Mail\Markdown;
// Create SES client
$ses = new SesClient([
'credentials' => [
'key' => config('services.ses.key'),
'secret' => config('services.ses.secret'),
],
'region' => config('services.ses.region'),
'version' => 'latest',
]);
// Render email content from markdown...
$markdown = new Markdown(view(), config('mail.markdown'));
$htmlContent = $markdown->render('hello_template');
$textContent = $markdown->renderText('hello_template');
// Create template
$ses->createTemplate([
'Template' => [
'TemplateName' => 'hello_template',
'HtmlPart' => $htmlContent,
'TextPart' => $textContent,
'SubjectPart' => 'Hello {{ name }}',
],
]);
// Update if necessary
/* $ses->updateTemplate([
'Template' => [
'TemplateName' => 'hello_template',
'HtmlPart' => $htmlContent,
'SubjectPart' => 'Hello {{ name }}',
],
]); */
// Test content before send
/* echo $ses->testRenderTemplate([
'TemplateName' => 'hello_template',
'TemplateData' => json_encode([
'name' => 'Mr A',
'message' => 'Thank you!',
]),
]); */
$ses->sendBulkTemplatedEmail([
'Source' => sprintf('%s <%s>', config('mail.from.name'), config('mail.from.address')),
'Template' => 'hello_template',
'DefaultTemplateData' => json_encode([
'name' => 'DEFAULT',
'placeholder' => 'DEFAULT',
]),
'Destinations' => [
[
'Destination' => [
'ToAddresses' => ['abc@domain.com'],
],
'ReplacementTemplateData' => json_encode([
'name' => 'ABC',
'message' => 'Bonjua',
]),
],
[
'Destination' => [
'ToAddresses' => ['def@domain.com'],
],
'ReplacementTemplateData' => json_encode([
'name' => 'DEF',
'message' => 'Merci',
]),
],
],
]);
@component('mail::message')
Hello @{{ name }},
We want to say: @{{ message }}.
@endcomponent
Hello {{ name }},
We want to say: {{ message }}.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment