如何使用 Mandrill API 向多个收件人发送电子邮件?

How can I send an email to multiple recipients using the Mandrill API?

我也发现了类似的问题,但还是不太清楚。 如何使用 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;

这就是山魈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);
} 
// 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);
} 

一种简单的方法是遍历 $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);
} 

}

您还可以使用 template.However 通过 Mandrill 发送新消息,确保您了解它的限制

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

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

For SMTP messages, you may send to up to 1,000 recipients at a time. If you’re sending to more recipients, simultaneous and subsequent connections are permitted.

For the API, there’s no recipient limitation, but the JSON provided per API call must be less than 10MB. We strongly recommend smaller recipient batches for easier troubleshooting.