I'd like to explain my problem. I use mailjet api v3 to send emails and every X days, I have to create a campaign that will send an email to all the emails on the list (saved on mailjet).
The creation of the draft campaign goes well, I retrieve the data from my email template (transactional) at mailjet, but no way to inject the necessary variables and therefore the emails are empty of content.
I've attached the code and the result I received.
public function handle(array $programs): void
{
$templateContent = GetTemplateContent::run(ID TEMPLATE, $programs);
$response = UpdateAndSendCampaign::run(CreateCampaign::run(), $templateContent);
if (!$response->success()) {
throw new \RuntimeException($response->getReasonPhrase());
}
}
GetTemplateContent
public function handle(int $templateID, array $variables = []): array
{
$response = $this->mailjet->get(Resources::$TemplateDetailcontent, [
'id' => $templateID,
'Vars' => [
'programmes' => [
[
'title' => 'Programme 1',
'link' => 'https://www.google.com',
'address' => 'Paris',
'price' => '10',
'image' => 'https://miro.medium.com/v2/resize:fit:1024/0*pl3wnRCOkivJWOxG.png'
],
[
'title' => 'Programme 2',
'link' => 'https://www.google.com',
'address' => 'Paris',
'price' => '10',
'image' => 'https://miro.medium.com/v2/resize:fit:1024/0*pl3wnRCOkivJWOxG.png'
]
]
]
]);
return [
'Html-part' => $response->getData()[0]['Html-part'],
'Text-part' => $response->getData()[0]['Text-part'],
'MJMLContent' => $response->getData()[0]['MJMLContent']
];
}
CreateCampaign
public function handle(): int
{
$body = [
'Locale' => "fr_FR",
'Title' => "[TEST] Nouveaux programmes de la semaine du " . now()->translatedFormat('l j F Y') . " !",
'Subject' => "[TEST] Nouveaux programmes de la semaine du " . now()->translatedFormat('l j F Y') . " !",
'Sender' => config('app.name'),
'SenderEmail' => config('contact.email'),
"ContactsListID" => config('services.mailjet.lists.newsletter'),
"EditMode" => "mjml",
];
$response = $this->mailjet->post(Resources::$Campaigndraft, ['body' => $body]);
return $response->getData()[0]['ID'];
}
UpdateAndSendCampaign
public function handle(int $campaignID, array $templateContent): Response
{
$this->mailjet->post(Resources::$CampaigndraftDetailcontent, [
'id' => $campaignID,
'body' => [
'Html-part' => $templateContent['Html-part'],
'Text-part' => $templateContent['Text-part'],
'MJMLContent' => $templateContent['MJMLContent'],
]
]);
return $this->mailjet->post(Resources::$CampaigndraftSend, ['id' => $campaignID]);
}
MAIL RESULT enter image description here
Thank you in advance
I'd like some help to find a way of passing the variables to my template so that my campaign works properly.