【问题标题】:How can I send an email to multiple recipients using the Mandrill API?如何使用 Mandrill API 向多个收件人发送电子邮件?
【发布时间】:2016-03-14 01:54:39
【问题描述】:

我发现了类似的问题,但我仍然不清楚。 如何使用 Mandrill API 向多个收件人发送电子邮件?

接收者的数量可能会根据数据库中存储的信息而有所不同:

$query = "SELECT emails FROM emails_table";
$data = mysql_query($query);
$n = 0;
while ($row = mysql_fetch_assoc($data))
{
$email[$n] = $row['emails'];
$n++;
}

因此,电子邮件将存储在这样的变量中。例如

$email[0] = email_0@example.com;
$email[1] = email_1@example.com;
$email[2] = email_2@example.com;

这是 Mandrill API:

require("/mandrill_mail/src/Mandrill.php");

try {
    $mandrill = new Mandrill('kWre_48F1lnJs3_39YM434z');//API KEY
    $message = array(
        'html' => 'message',
        'subject' => 'subject',
        'from_email' => 'my_mail@my_domain.com',
        'from_name' => 'My_Domain',
        'to' => array(
            array(
                'email' => $email[0], //How can I add the other emails considering that the number of recipients will vary depending on the data in the db?
                'name' => 'Recipient Name',
                'type' => 'to'*/
            )
        ),
        'headers' => array('Reply-To' => 'my_mail@my_domain.com'),
        'important' => false,
        'track_opens' => null,
        'track_clicks' => null,
        'auto_text' => null,
        'auto_html' => null,
        'inline_css' => null,
        'url_strip_qs' => null,
        'preserve_recipients' => false,
        'view_content_link' => null,
        'bcc_address' => $mail_bc,
        'tracking_domain' => null,
        'signing_domain' => null,
        'return_path_domain' => null,
        'merge' => true,
        'merge_language' => 'mailchimp',

    );
    $async = false;
    $ip_pool = 'Main Pool';

    $result = $mandrill->messages->send($message, $async, $ip_pool);
} 

【问题讨论】:

  • 您想如何发送电子邮件?作为多个单独的电子邮件给这些收件人?还是作为一封电子邮件有许多收件人?
  • 更快的方法。我认为这是一封有很多收件人的电子邮件。
  • @BenRowe 如果是`多个单独的电子邮件给多个收件人`,我们需要做什么?
  • 您需要逐个遍历每个收件人。

标签: php mandrill


【解决方案1】:
// build the 'to' array
$query = "SELECT emails FROM emails_table";
$data = mysql_query($query);
$emails = array();
while ($row = mysql_fetch_assoc($data)) {
    $emails[] = array(
        'email' => $row['emails'], 
        'type' => 'to'
    );
}

然后

require("/mandrill_mail/src/Mandrill.php");

try {
    $mandrill = new Mandrill('kWre_48F1lnJs3_39YM434z');//API KEY
    $message = array(
        'html' => 'message',
        'subject' => 'subject',
        'from_email' => 'my_mail@my_domain.com',
        'from_name' => 'My_Domain',
        'to' => $emails,
        'headers' => array('Reply-To' => 'my_mail@my_domain.com'),
        'important' => false,
        'track_opens' => null,
        'track_clicks' => null,
        'auto_text' => null,
        'auto_html' => null,
        'inline_css' => null,
        'url_strip_qs' => null,
        'preserve_recipients' => false,
        'view_content_link' => null,
        'bcc_address' => $mail_bc,
        'tracking_domain' => null,
        'signing_domain' => null,
        'return_path_domain' => null,
        'merge' => true,
        'merge_language' => 'mailchimp',

    );
    $async = false;
    $ip_pool = 'Main Pool';

    $result = $mandrill->messages->send($message, $async, $ip_pool);
} 

【讨论】:

  • 你确定这个方法吗? api文档说它需要'to'字段中的单个收件人信息
  • 是的,我确定。该文档是指在to 字段的email 部分中有多个电子邮件地址。
【解决方案2】:

一种简单的方法是遍历 $emails 数组并动态地将电子邮件发送到每个电子邮件地址。

foreach($emails as $email){

try {
    $mandrill = new Mandrill('kWre_48F1lnJs3_39YM434z');//API KEY
    $message = array(
        'html' => 'message',
        'subject' => 'subject',
        'from_email' => 'my_mail@my_domain.com',
        'from_name' => 'My_Domain',
        'to' => array(
            array(
                'email' => $email, 
                'name' => 'Recipient Name',
                'type' => 'to'*/
            )
        ),
        'headers' => array('Reply-To' => 'my_mail@my_domain.com'),
        'important' => false,
        'track_opens' => null,
        'track_clicks' => null,
        'auto_text' => null,
        'auto_html' => null,
        'inline_css' => null,
        'url_strip_qs' => null,
        'preserve_recipients' => false,
        'view_content_link' => null,
        'bcc_address' => $mail_bc,
        'tracking_domain' => null,
        'signing_domain' => null,
        'return_path_domain' => null,
        'merge' => true,
        'merge_language' => 'mailchimp',

    );
    $async = false;
    $ip_pool = 'Main Pool';

    $result = $mandrill->messages->send($message, $async, $ip_pool);
} 

}

您也可以使用模板通过 Mandrill 发送新消息。但是请确保您了解它的限制

对于 SMTP 邮件,您一次最多可以发送给 1,000 个收件人。如果您要发送给更多收件人,则允许同时连接和后续连接。

对于 API,没有收件人限制,但每次 API 调用提供的 JSON 必须小于 10MB。我们强烈建议使用较小的收件人批次,以便更轻松地进行故障排除。

对于 SMTP 邮件,您一次最多可以发送给 1,000 个收件人。 如果您要发送给更多收件人,同时和后续 允许连接。

对于 API,没有收件人限制,但提供的 JSON 每个 API 调用必须小于 10MB。我们强烈建议较小 收件人批次,以便更轻松地进行故障排除。

【讨论】:

    猜你喜欢
    • 2019-03-01
    • 2012-05-18
    • 2011-04-18
    • 1970-01-01
    • 1970-01-01
    • 2019-04-22
    • 2014-06-22
    相关资源
    最近更新 更多