使用动态内容模板发送自动电子邮件

时间:2019-03-21 11:07:13

标签: mailchimp mandrill

好吧,我想在每个星期五使用MailChimp / Mandrill发送自动电子邮件。

电子邮件模板:

Hi {{firstName}},

You weekly weekend pass is {{code}}.

在这里,我的电子邮件内容是动态生成的。因此,在发送自动电子邮件之前,应该有某种机制可以在我获取firstName和代码的地方调用rest api。然后,电子邮件将发送给其他用户。

{
   [{
      firstName: 'test',
      code: 'Xewetwerx'
   },
   {
      firstName: 'test2',
      code: 'Xewetwrtd'
   }]
}

我知道我们可以在后端服务器中编写代码,并在每个星期五使用cronjob调用API(每个星期五发送一封电子邮件),但是我不想使用任何cronjob。

是否可以使用MailChimp或mandrill进行API调用并发送自动邮件?

结果: 2封电子邮件应使用不同的代码发送。

1 个答案:

答案 0 :(得分:0)

Mandrill 通过 sent_at 参数实现 scheduled emails

可以 send an email using a template 并指定日期和时间应以 YYYY-MM-DD HH:MM:SS 格式的 UTC 时间戳发送消息。如果您指定过去的时间,消息将立即发送。请注意,对于 sent_at 功能,您需要一个付费(非免费)帐户

如果您选择 PHP 作为后端的编程语言, 您可以使用 official PHP API library

示例:(代码未经测试,可能包含错误)

require_once '/path/to/MailchimpTransactional/vendor/autoload.php';

$mailchimp = new MailchimpTransactional\ApiClient();
$mailchimp->setApiKey('YOUR_API_KEY');
$msg = array(
    'subject' => 'My subject',
    'from_email' => 'ellery@example.com',
    'to' => array(array('email' => 'recipient1@example.com', 'name' => 'Recipient 1'),array('email' => 'recipient2@example.com', 'name' => 'Recipient 2')),
    'merge_vars' => array(array(
        'rcpt' => 'recipient1@example.com',
        'vars' =>
        array(
            array(
                'name' => 'firstName',
                'content' => 'test'),
            array(
                'name' => 'code',
                'content' => 'Xewetwerx')
    )),
    
    array(
        'rcpt' => 'recipient2@example.com',
        'vars' =>
        array(
            array(
                'name' => 'firstName',
                'content' => 'test2'),
            array(
                'name' => 'code',
                'content' => 'Xewetwrtd')
    ))));



$response = $mailchimp->messages->sendTemplate([
    "template_name" => "template_name",
    "template_content" => [[]],
    "message" => $msg,
    "send_at" => "2021-06-23 09:05:00" ,
]);
print_r($response);
相关问题