# Examples ## Prepare & Send a Campaign Draft 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 ```php setOptionalProperties($optionalProp); $campaignDraftManager->create($campaignDraft); } ``` Now we've created a draft campaign, we can set its most important property: the content, which can be either plain text or HTML (respectively represented by the `Text-part` and the `Html-part` resource's properties). ```php $content = ['Html-part' => "Hello world!", 'Text-part' => "Hello world!"]; $campaignDraft->setContent($content); $campaignDraftManager->createDetailContent($campaignDraft->getId(), $campaignDraft->getContent()); ``` Once the draft campaign draft is all set, it can now be sent via the `CampaignDraftContract`. ``` php /* Send the campaigndraft instance */ $campaignDraftManager->sendCampaign($campaignDraft->getId()); ``` ## Storing & Sending a Template The `/template` resource allows to store your template on the Mailjet system. To create a template you only need to provide a name. You can than reuse the template in your messages by referencing the ID returned when created. In the providers array inside `app.php` add: Mailjet\LaravelMailjet\Providers\TemplateServiceProvider::class You can manage [/template](https://dev.mailjet.com/email-api/v3/template) resources through the `TemplateContract`. ### Create Template Code Sample ```php 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; $result=$templateManager->getAll($filters); } ``` To send the template you must set the `Mj-TemplateID` property to the template ID to send in your Send API payload. In addition to that, you must set the `Mj-TemplateLanguage` property in the Send API payload to true in order to have the Mailjet templating language interpreted. ### Send Template Code Sample ``` php "pilot@mailjet.com", 'FromName' => "Mailjet Pilot", 'Subject' => "Your email flight plan!", 'MJ-TemplateID' => $ID, 'MJ-TemplateLanguage' => true, 'Recipients' => [['Email' => "passenger@mailjet.com"]] ]; $response = $mj->post(Resources::$Email, ['body' => $body]); $response->success() && var_dump($response->getData()); ``` ## Manage Campaigns In the providers array inside `app.php` add: Mailjet\LaravelMailjet\Providers\CampaignServiceProvider::class You can manage the [/campaign](https://dev.mailjet.com/email-api/v3/campaign) resources through the `CampaignContract`. ### Code Sample ```php getAllCampaigns($filters); } ``` ## Update a contact email address In the providers array inside app.php add: Mailjet\LaravelMailjet\Providers\ContactsListServiceProvider::class ```php updateEmail($listId, $contact,$oldEmail); } ``` ## Retrieve Mailjet Client instance to perform custom requests You can retrieve the `MailjetClient` instance, as defined in the PHP [wrapper]((https://github.com/mailjet/mailjet-apiv3-php)), using the method `getClient()`. It enables you to perform custom requests to Mailjet API. * In the providers array ```php 'providers' => [ ... Mailjet\LaravelMailjet\MailjetServiceProvider::class, Mailjet\LaravelMailjet\MailjetMailServiceProvider::class, ... ] ``` * In the aliases array ```php 'aliases' => [ ... 'Mailjet' => Mailjet\LaravelMailjet\Facades\Mailjet::class, ... ] ``` To use it, you need to import Mailjet Facade in your file use Mailjet\LaravelMailjet\Facades\Mailjet; Example: ``` php get(Resources::$Contact); /* Read the response */ if ($response->success()) var_dump($response->getData()); else var_dump($response->getStatus()); ... // Send transactional emails (note: prefer using SwiftMailer to send transactionnal emails) $body = [ 'FromEmail' => "pilot@mailjet.com", 'FromName' => "Mailjet Pilot", 'Subject' => "Your email flight plan!", 'Text-part' => "Dear passenger, welcome to Mailjet! May the delivery force be with you!", 'Html-part' => "