From 3c5815802498ffeb3e8094d82a306b61d1cace12 Mon Sep 17 00:00:00 2001 From: Abderraouf Mahdi Date: Wed, 18 Dec 2024 20:53:56 +0100 Subject: [PATCH 1/2] Example of usage with laravel mailable --- docs/usage.md | 95 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 90 insertions(+), 5 deletions(-) diff --git a/docs/usage.md b/docs/usage.md index 9ac8f5a..c3cec38 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -2,7 +2,7 @@ ## Prepare & Send a Campaign Draft -In order to create a draft campaign, perform a `POST` on the `/campaigndraft` endpoint. +In order to create a draft campaign, perform a `POST` on the `/campaigndraft` endpoint. Required fields are `Locale`, `Sender`, `SenderEmail`, `Subject` and `ContactsListID`. In the providers array inside `app.php` add: @@ -10,7 +10,7 @@ In the providers array inside `app.php` add: Mailjet\LaravelMailjet\Providers\CampaignDraftServiceProvider::class You can manage [/campaigndraft](https://dev.mailjet.com/email-api/v3/campaigndraft) resources using the `CampaignDraftContract` class. - + ### Code sample @@ -71,16 +71,16 @@ public function templateExample(TemplateContract $templateManager) { $optionalProp['EditMode'] = 1; $optionalProp['Purposes'] = ['transactional']; $template = new Template("Laravel Template Example", $optionalProp); - + $ID = $templateManager->create($template)[0]['ID']; - + // Set template content $contentData = [ 'Html-part' => "

Hello {{var:name}}

", 'Text-part' => "Hello {{var:name}}" ]; $templateManager->createDetailContent($ID, $contentData); - + // List all templates based on multiple filters $filters['OwnerType']='apikey'; $filters['EditMode']=1; @@ -215,4 +215,89 @@ Example: $response = $mailjet->post(Resources::$Email, ['body' => $body]); +``` + +## Usage with Laravel Mailable class +In order to use this package with a laravel `Mailable` class, first generate a `Mailable` class: +```bash +php artisan make:mail InvoicePaid +``` +Then define mailjet `X-MJ-` or `X-Mailjet-` properties inside the `headers` method, these headers will be add to the message body before sending. + +### Code Example +```php +

Invoice paid: {{var:invoiceNumber}}

true, // must be set to true to use variables + // Full list + 'X-MJ-Vars' => json_encode([ + 'invoiceNumber' => $this->invoiceNumber + ]), + 'X-MJ-TemplateID' => 12532, + 'X-MJ-TemplateErrorReporting' => json_encode([...]), + 'X-MJ-TemplateErrorDeliver' => true, + 'X-MJ-CustomID' => 'string', + 'X-MJ-EventPayload' => 'string', + 'X-Mailjet-Campaign' => 'string', + 'X-Mailjet-DeduplicateCampaign' => true, + 'X-Mailjet-Prio' => 5, + 'X-Mailjet-TrackClick' => 'string', + 'X-Mailjet-TrackOpen' => 'string', + ] + ); + } +} ``` \ No newline at end of file From 2538bad179672022a0bd8875d3a053e04bb8fd24 Mon Sep 17 00:00:00 2001 From: Abderraouf Mahdi Date: Wed, 18 Dec 2024 21:36:47 +0100 Subject: [PATCH 2/2] Enhance example with more details --- docs/usage.md | 80 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 69 insertions(+), 11 deletions(-) diff --git a/docs/usage.md b/docs/usage.md index c3cec38..73dac17 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -224,7 +224,7 @@ php artisan make:mail InvoicePaid ``` Then define mailjet `X-MJ-` or `X-Mailjet-` properties inside the `headers` method, these headers will be add to the message body before sending. -### Code Example +### InvoicePaid class ```php

Invoice paid: {{var:invoiceNumber}}

An invoice has been paid: @{{var:invoiceNumber}}

+``` + +In `views/mail.invoice-paid-text.blade.php`: +```php +An invoice has been paid: @{{var:invoiceNumber}} +``` + +### Example with mailjet template +```php +html(''); + } + + /** + * Get the message envelope. + */ + public function envelope(): Envelope + { + return new Envelope( + // Variables can be use in subject too + subject: 'Invoice Paid {{var:invoiceNumber}}', + ); + } + + /** + * Get the message headers. + */ + public function headers(): Headers + { + return new Headers( + text: [ + 'X-MJ-TemplateLanguage' => true, + 'X-MJ-Vars' => json_encode([ + 'invoiceNumber' => $this->invoiceNumber + ]), + 'X-MJ-TemplateID' => 12532, + ] + ); + } +} +```