From 32e0503dcff1aa0ae8068d901efe3caf75ebcc41 Mon Sep 17 00:00:00 2001 From: gaalferov Date: Thu, 31 Oct 2024 16:38:11 +0100 Subject: [PATCH 1/4] add more template examples --- src/Bridge/Laravel/README.md | 62 ++++++++++++++++++++++++++++++++++-- src/Bridge/Symfony/README.md | 54 +++++++++++++++++++++++++++++-- 2 files changed, 112 insertions(+), 4 deletions(-) diff --git a/src/Bridge/Laravel/README.md b/src/Bridge/Laravel/README.md index 0d645d0..11f72b1 100644 --- a/src/Bridge/Laravel/README.md +++ b/src/Bridge/Laravel/README.md @@ -80,7 +80,7 @@ Firstly you need to generate `Mailable` class. More info [here](https://laravel. ```bash php artisan make:mail WelcomeMail ``` -After that you can configure you Email as you which. Below will be example. +After that, you can configure your Email as you which. Below will be an example. ```php # app/Mail/WelcomeMail.php send(new WelcomeMail("Jon")); + // Also, you can use specific mailer if your default mailer is not "mailtrap" but you want to use it for welcome mails // Mail::mailer('mailtrap')->to('testreceiver@gmail.com')->send(new WelcomeMail("Jon")); })->purpose('Send welcome mail'); @@ -226,6 +227,63 @@ After that just call this CLI command, and it will send your email php artisan send-welcome-mail ``` +### Send Template Email +To send 'Template Email', you should use the native library and its methods, +as mail transport validation does not allow you to send emails without ‘html’ or ‘text’. + +Add CLI command +```php +# app/routes/console.php +from(new Address('example@YOUR-DOMAIN-HERE.com', 'Mailtrap Test')) // <--- you should use your domain here that you installed in the mailtrap.io admin area (otherwise you will get 401) + ->replyTo(new Address('reply@YOUR-DOMAIN-HERE.com')) + ->to(new Address('example@gmail.com', 'Jon')) + // when using a template, you should not set a subject, text, HTML, category + // otherwise there will be a validation error from the API side + ->templateUuid('bfa432fd-0000-0000-0000-8493da283a69') + ->templateVariables([ + 'user_name' => 'Jon Bush', + 'next_step_link' => 'https://mailtrap.io/', + 'get_started_link' => 'https://mailtrap.io/', + 'onboarding_video_link' => 'some_video_link', + 'company' => [ + 'name' => 'Best Company', + 'address' => 'Its Address', + ], + 'products' => [ + [ + 'name' => 'Product 1', + 'price' => 100, + ], + [ + 'name' => 'Product 2', + 'price' => 200, + ], + ], + 'isBool' => true, + 'int' => 123 + ]) + ; + + MailtrapClient::initSendingEmails( + apiKey: env('MAILTRAP_API_KEY') // your API token from here https://mailtrap.io/api-tokens + )->send($email); +})->purpose('Send Template Mail'); +``` + +After that just call this CLI command, and it will send your template email +```bash +php artisan send-template-mail +``` + ## Compatibility The Mailtrap library is fully compatible with **Laravel 9.x and above**. > Laravel did one of the largest changes in Laravel 9.x is the transition from SwiftMailer, which is no longer maintained as of December 2021, to Symfony Mailer. diff --git a/src/Bridge/Symfony/README.md b/src/Bridge/Symfony/README.md index 1a5c54a..2b95f55 100644 --- a/src/Bridge/Symfony/README.md +++ b/src/Bridge/Symfony/README.md @@ -58,10 +58,12 @@ php bin/console mailer:test to@example.com ```php from('from@xample.com') @@ -102,6 +104,54 @@ final class SomeController extends AbstractController return JsonResponse::create(['messageId' => $response->getMessageId()]); } + + /** + * WARNING! To send ‘Template Email, you should use the native library and its methods, + * as mail transport validation does not allow you to send emails without ‘html’ or ‘text’ + * + * @Route(name="send-template-email", path="/send-template-email", methods={"GET"}) + * + * @return JsonResponse + */ + public function sendTemplateEmail(): JsonResponse + { + $email = (new MailtrapEmail()) + ->from(new Address('example@YOUR-DOMAIN-HERE.com', 'Mailtrap Test')) // <--- you should use your domain here that you installed in the mailtrap.io admin area (otherwise you will get 401) + ->replyTo(new Address('reply@YOUR-DOMAIN-HERE.com')) + ->to(new Address('example@gmail.com', 'Jon')) + // when using a template, you should not set a subject, text, HTML, category + // otherwise there will be a validation error from the API side + ->templateUuid('bfa432fd-0000-0000-0000-8493da283a69') + ->templateVariables([ + 'user_name' => 'Jon Bush', + 'next_step_link' => 'https://mailtrap.io/', + 'get_started_link' => 'https://mailtrap.io/', + 'onboarding_video_link' => 'some_video_link', + 'company' => [ + 'name' => 'Best Company', + 'address' => 'Its Address', + ], + 'products' => [ + [ + 'name' => 'Product 1', + 'price' => 100, + ], + [ + 'name' => 'Product 2', + 'price' => 200, + ], + ], + 'isBool' => true, + 'int' => 123 + ]) + ; + + $response = MailtrapClient::initSendingEmails( + apiKey: env('MAILTRAP_API_KEY') // your API token from here https://mailtrap.io/api-tokens + )->send($email); + + return JsonResponse::create(ResponseHelper::toArray($response)); + } } ``` From 64eb385362ae2fa865334e5bd080988c1ed0dc09 Mon Sep 17 00:00:00 2001 From: gaalferov Date: Thu, 31 Oct 2024 17:20:37 +0100 Subject: [PATCH 2/4] bump version --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 354ada3..b6bfc8d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## [2.0.3] - 2024-11-01 + +- Add more template examples in Laravel/Symfony docs + ## [2.0.2] - 2024-10-04 - Remove an expected message from the `testUnsupportedSchemeException` method ([reason](https://github.com/symfony/mailer/commit/a098a3fe7f42a30235b862162090900cbf787ff6)) From 6663f04ebb9c5587d584337a5a5d286b0685c4a7 Mon Sep 17 00:00:00 2001 From: gaalferov Date: Thu, 31 Oct 2024 17:28:40 +0100 Subject: [PATCH 3/4] add ResponseHelper --- src/Bridge/Symfony/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Bridge/Symfony/README.md b/src/Bridge/Symfony/README.md index 2b95f55..5ade74e 100644 --- a/src/Bridge/Symfony/README.md +++ b/src/Bridge/Symfony/README.md @@ -60,6 +60,7 @@ php bin/console mailer:test to@example.com use Mailtrap\MailtrapClient; use Mailtrap\Mime\MailtrapEmail; +use Mailtrap\Helper\ResponseHelper; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Mailer\Transport\TransportInterface; use Symfony\Component\HttpFoundation\JsonResponse; From aa0cb1ab0840d51f1ba7ec9c2f52650c0420cd68 Mon Sep 17 00:00:00 2001 From: gaalferov Date: Thu, 31 Oct 2024 17:38:55 +0100 Subject: [PATCH 4/4] fix phrases --- src/Bridge/Laravel/README.md | 4 ++-- src/Bridge/Symfony/README.md | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Bridge/Laravel/README.md b/src/Bridge/Laravel/README.md index 11f72b1..7a2f4d1 100644 --- a/src/Bridge/Laravel/README.md +++ b/src/Bridge/Laravel/README.md @@ -80,7 +80,7 @@ Firstly you need to generate `Mailable` class. More info [here](https://laravel. ```bash php artisan make:mail WelcomeMail ``` -After that, you can configure your Email as you which. Below will be an example. +After that, you can configure your Email as you wish. Below will be an example. ```php # app/Mail/WelcomeMail.php