我通过一个CRON作业使用PHP和Mailgun,它向订阅的电子邮件地址发送电子邮件,计划每天运行,尽管可能不会每天都有电子邮件排队。
下面是我用来添加退订简介的代码:
$email = array(
'subject' => $row['subject'],
'body' => $row['body'] . "\r\n\r\n\r\n\r\n\r\n" . "No longer wish to receive emails from us? You may unsubscribe below. Beware, in unsubscribing, you will miss out on any information we share via email going forward."
);
$row['subject']
是带有HTML标记的字符串。
function send_email($from, $to, $subject, $body) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'api:removed for privacy');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v3/removed for privacy/messages');
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'from' => $from,
'to' => $to,
'subject' => $subject,
'html' => $body
));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
function mail_merge($text, $agency, $sender, $receiver, $efs) {
// Replace any mail merger fields from the body
$text = str_replace('[agency.name]', $agency['name'], $text);
$text = str_replace('[agency.address]', $agency['address_formatted'], $text);
$text = str_replace('[agency.phone]', $agency['phone'], $text);
$text = str_replace('[agency.fax]', $agency['fax'], $text);
$text = str_replace('[agency.email]', $agency['email'], $text);
$text = str_replace('[agency.signature]', $agency['signature'], $text);
$text = str_replace('[agency.referral]', $agency['referral'], $text);
$text = str_replace('[sender.firstname]', $sender['first_name'], $text);
$text = str_replace('[sender.lastname]', $sender['last_name'], $text);
$text = str_replace('[sender.email]', $sender['email'], $text);
$text = str_replace('[sender.signature]', $sender['signature'], $text);
$text = str_replace('[receiver.firstname]', $receiver['first_name'], $text);
$text = str_replace('[receiver.lastname]', $receiver['last_name'], $text);
$text = str_replace('[receiver.email]', $receiver['email'], $text);
$text = str_replace('[receiver.phone]', $receiver['phone'], $text);
$text = str_replace('[efs.firstname]', $efs['first_name'], $text);
$text = str_replace('[efs.lastname]', $efs['last_name'], $text);
$text = str_replace('[efs.email]', $efs['email'], $text);
$text = str_replace('[efs.phone]', $efs['phone'], $text);
return $text;
}
正如我所提到的,我有这样的情况
一些
正在发送的测试电子邮件中,不包括附加的新行和optout blub。这是在cURL请求通过Mailgun的API发送电子邮件时可能引起的,还是我在代码中无意中做了一些事情而没有捕捉到?