Hello, I’m integrating with our backend system to create the email campaign in our self hosted Mautic instance. Before posting here, I’ve checked out the API docs (for REST): Mautic Developer Documentation but it turns out it is for pre-6 version, where you don’t need an event for creating a campaign. I found no other documentation etc. I switched to curl along the way, so here is how I create (successfully) the email itself:
$emailData = [
‘name’ => $campaignNameFinal,
‘subject’ => $emailSubject,
‘fromName’ => ‘ABCD’,
‘fromAddress’ => ‘test@test.com’,
‘isPublished’ => true,
‘emailType’ => ‘list’,
‘customHtml’ => $html_content,
‘plainText’ => strip_tags($html_content),
‘lists’ => [$segmentId]
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $mauticBaseUrl.‘/api/emails/new’);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer '.$accessToken,
‘Content-Type: application/json’
]);
curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($emailData));
$emailResponse = curl_exec($ch);
curl_close($ch);
$emailResult = json_decode($emailResponse,true);
$emailId = $emailResult[‘email’][‘id’] ?? null;
So now after I have the $emailId I need to link it to a campaign I create. The only way that I was able to create a campaign in v6 is this:
$campaignData = [
‘isPublished’ => true,
‘name’ => $campaignNameFinal,
‘description’ => ‘Auto-generated campaign with email’,
‘lists’ => [$segmentId],
‘events’ => [
[
‘id’ => ‘send_email’,
‘name’ => ‘Send Newsletter’,
‘type’ => ‘email.send’,
‘eventType’ => ‘action’,
‘order’ => 1,
‘props’ => [
‘canvasSettings’ => [‘droppedX’=>100,‘droppedY’=>50],
‘email’ => $emailId
]
]
]
];
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$mauticBaseUrl.‘/api/campaigns/new’);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_HTTPHEADER,[
'Authorization: Bearer '.$accessToken,
‘Content-Type: application/json’
]);
curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode($campaignData));
$campaignResponse = curl_exec($ch);
curl_close($ch);
$campaignResult = json_decode($campaignResponse,true);
It comes back (no error), the campaign is created, but it is not linked to the email and the send event is also not linked (even though it shows in builder), so I have to manually select the segment (even though the email HAS the segment) and connect in builder the segment → send event.
Any ideas are welcome, much appreciated in trying to successfully save the campaign so that my email creator user doesn’t have to log in to mautic to set up the campaign manually.
Many thanks