简体   繁体   中英

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

I have found similar questions, but still not clear to me. How can I send an email to multiple recipients using the Mandrill API?

The quantity of recipients may vary according to the information stored on the db:

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

So, emails will be stored in variables like this. Eg

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

And this is the 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);
} 
// 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'
    );
}

Then

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

One easy way is to loop through $emails array and dynamically send the email to each email address.

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

}

You can also send a new message through Mandrill using a template.However make sure you know about it`s limits

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM