129 lines
No EOL
4 KiB
PHP
129 lines
No EOL
4 KiB
PHP
<?php
|
|
|
|
// $title - title
|
|
// $articleUrl - permalink
|
|
// $excerpt - teaser
|
|
// $articleId - articleId
|
|
// $newsletterTitle - randomly chosen from header_titles.php
|
|
|
|
function listmonkCreateCampaign(
|
|
string $articleId,
|
|
string $articleUrl,
|
|
string $title,
|
|
string $excerpt,
|
|
string $newsletterTitle = 'Mistakes, Views & Luck'
|
|
): array {
|
|
$base = 'http://127.0.0.1:9000';
|
|
|
|
$user = 'mvlog';
|
|
$pass = '1VLUsqSyIQ4xboEd2tmQvgCjWJJk0F3Q';
|
|
|
|
$listId = 3; // MVLog Readers
|
|
$templateId = 5; // Your Listmonk template ID
|
|
|
|
$ogImageUrl = 'https://bubulescu.org/out-dir/' . rawurlencode($articleId) . '_og.jpg';
|
|
|
|
$safeTitle = htmlspecialchars($title, ENT_QUOTES, 'UTF-8');
|
|
$safeExcerpt = nl2br(htmlspecialchars($excerpt, ENT_QUOTES, 'UTF-8'));
|
|
$safeArticleUrl = htmlspecialchars($articleUrl, ENT_QUOTES, 'UTF-8');
|
|
$safeOgImageUrl = htmlspecialchars($ogImageUrl, ENT_QUOTES, 'UTF-8');
|
|
|
|
$body = '
|
|
<a href="' . $safeArticleUrl . '">
|
|
<img
|
|
src="' . $safeOgImageUrl . '"
|
|
alt="' . $safeTitle . '"
|
|
style="display:block;width:100%;max-width:632px;height:auto;border-radius:18px;border:0;margin:0 auto 24px auto;"
|
|
>
|
|
</a>
|
|
|
|
<div style="padding:0 24px 8px 24px;text-align:center;">
|
|
<h2 style="margin:0 0 12px 0;color:#ffffff;font-size:26px;line-height:1.25;">
|
|
' . $safeTitle . '
|
|
</h2>
|
|
|
|
<p style="margin:0;color:#eeeeee;font-size:16px;line-height:1.6;">
|
|
' . $safeExcerpt . '
|
|
</p>
|
|
</div>
|
|
|
|
<p style="text-align:center;margin-top:28px;">
|
|
<a href="' . $safeArticleUrl . '"
|
|
style="display:inline-block;background:#d9822b;color:#111;text-decoration:none;font-weight:bold;padding:13px 22px;border-radius:999px;">
|
|
Read the story
|
|
</a>
|
|
</p>
|
|
';
|
|
|
|
$payload = [
|
|
'name' => 'New article: ' . $title,
|
|
'subject' => $newsletterTitle,
|
|
'lists' => [$listId],
|
|
'from_email' => 'MVLog <newsletter@bubulescu.org>',
|
|
'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;
|
|
} |