取消订阅的Blub被任意删除

时间:2019-05-31 02:18:44

标签: php mailgun

我正在通过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标记的字符串。

这是我用于A)在发送电子邮件之前执行邮件合并和B)实际上发送电子邮件的代码。

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;
}

正如我所提到的,在某些情况下,发送的测试电子邮件中有 some 个不包含附加的新行和optout Blub。这是由于执行cURL请求以通过Mailgun的API发送电子邮件而引起的,还是我在代码中无意中做了却没有捕获到的?

1 个答案:

答案 0 :(得分:0)

虽然我始终无法弄清为什么随意删除了Blub,但是却可以通过从MailGun的域控制面板中定制退订模板来解决问题:

Domains > [Domain Name] > Tracking Settings > Customize unsubscribe templates