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

时间:2015-12-08 23:10:31

标签: php mandrill

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

收件人数量可能会根据db:

中存储的信息而有所不同
$query = "SELECT emails FROM emails_table";
$data = mysql_query($query);
$n = 0;
while ($row = mysql_fetch_assoc($data))
{
$email[$n] = $row['emails'];
$n++;
}

因此,电子邮件将存储在这样的变量中。 E.g。

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

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

一种简单的方法是循环遍历$ 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。我们强烈建议小一些   收件人批次以便于故障排除。

相关问题