Last active
November 5, 2024 03:06
-
-
Save tuanpht/83d021434086878d30ef8e4516cc11b5 to your computer and use it in GitHub Desktop.
PHP/Laravel AWS SES multiple emails multiple recipients
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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', | |
]), | |
], | |
], | |
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@component('mail::message') | |
Hello @{{ name }}, | |
We want to say: @{{ message }}. | |
@endcomponent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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