' . $safeTitle . '
' . $safeExcerpt . '
Read the story
';
$payload = [
'name' => 'New article: ' . $title,
'subject' => $newsletterTitle,
'lists' => [$listId],
'from_email' => 'MVLog ',
'type' => 'regular',
'content_type' => 'html',
'body' => $body,
'template_id' => $templateId,
'messenger' => 'email',
'status' => 'draft',
'data' => [
'newsletter_title' => $newsletterTitle,
],
'headers' => [
['Reply-To' => 'mvlog@bubulescu.org']
],
];
$json = json_encode($payload);
if (function_exists('curl_init')) {
$ch = curl_init($base . '/api/campaigns');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERPWD => $user . ':' . $pass,
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $json,
]);
$response = curl_exec($ch);
if ($response === false) {
$error = curl_error($ch);
curl_close($ch);
throw new Exception('Listmonk API request failed: ' . $error);
}
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
} else {
$context = stream_context_create([
'http' => [
'method' => 'POST',
'header' => "Content-Type: application/json
Authorization: Basic " . base64_encode($user . ':' . $pass) . "
",
'content' => $json,
'timeout' => 10,
'ignore_errors' => true,
],
]);
$response = @file_get_contents($base . '/api/campaigns', false, $context);
$code = 0;
if (isset($http_response_header[0]) && preg_match('/\s(\d{3})\s/', $http_response_header[0], $m)) {
$code = (int)$m[1];
}
if ($response === false) {
throw new Exception('Listmonk API request failed.');
}
}
$decoded = json_decode($response, true);
if ($code < 200 || $code >= 300) {
throw new Exception('Listmonk campaign create failed. HTTP ' . $code . ': ' . $response);
}
return $decoded;
}