Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example of usage with laravel Mailable class #82

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 149 additions & 6 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

## 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:

Mailjet\LaravelMailjet\Providers\CampaignDraftServiceProvider::class

You can manage [/campaigndraft](https://dev.mailjet.com/email-api/v3/campaigndraft) resources using the `CampaignDraftContract` class.


### Code sample

Expand Down Expand Up @@ -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' => "<html><body><p>Hello {{var:name}}</p></body></html>",
'Text-part' => "Hello {{var:name}}"
];
$templateManager->createDetailContent($ID, $contentData);

// List all templates based on multiple filters
$filters['OwnerType']='apikey';
$filters['EditMode']=1;
Expand Down Expand Up @@ -215,4 +215,147 @@ 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.

### InvoicePaid class
```php
<?php

namespace App\Mail;

use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Mail\Mailables\Headers;

class InvoicePaid extends Mailable
{
/**
* Create a new message instance.
*/
public function __construct(
public string $invoiceNumber
) {
//
}

/**
* 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 content definition.
*/
public function content(): Content
{
return new Content(
view: 'mail.invoice-paid',
text: 'mail.invoice-paid-text'
);
}

/**
* Get the message headers.
*/
public function headers(): Headers
{
return new Headers(
text: [
'X-MJ-TemplateLanguage' => 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',
]
);
}
}
```

### Views
Use `@` to escape blade directive.
In `views/mail.invoice-paid.blade.php`:
```php
<p>An invoice has been paid: @{{var:invoiceNumber}}</p>
```

In `views/mail.invoice-paid-text.blade.php`:
```php
An invoice has been paid: @{{var:invoiceNumber}}
```

### Example with mailjet template
```php
<?php

namespace App\Mail;

use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Mail\Mailables\Headers;

class InvoicePaid extends Mailable
{
/**
* Create a new message instance.
*/
public function __construct(
public string $invoiceNumber
) {
// Set empty HTML since a tempalte is used
$this->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,
]
);
}
}
```