使用mandrill发送多封电子邮件

时间:2015-11-25 15:29:41

标签: php email mandrill

我有一个订阅者数据库,我想向他发送相同的电子邮件。我使用Mandrill能够发送电子邮件。网站管理员必须输入电子邮件的主题,消息和附件,然后将其提交给所有订阅者。

我尝试过为数据库中的每封电子邮件运行Mandrill API的while循环。它可以工作,但是在发送大约5封电子邮件后,服务器会耗尽并崩溃。

我也知道能够运行一次Mandrill API并发送多封电子邮件,"到"必须为每个电子邮件地址重复Mandrill API中的数组。我想要做的是获得某种循环,重复"到#34; Mandrill API中每封电子邮件的数组,从而运行整个API并发送所有电子邮件。以下是我用来发送电子邮件的Mandrill API。

你们可以帮帮我吗?

由于

        while($row = mysqli_fetch_assoc($result1))
        {
            $ID = $row['ID'];
            $name = $row['name'];
            $surname = $row['surname'];
            $email = $row['email'];

            try
            {   
                $mandrill = new Mandrill('My Key');

                $message = array(
                    'html' => $message,
                    'subject' => $subject,
                    'from_email' => 'email@gmail.com',
                    'from_name' => 'Silvan Theuma',
                    'to' => array(
                        array(
                            'email' => $email,
                            'name' => $name,
                            'type' => 'to'
                        )
                    ),/*This is what I want to repeat for every email*/
                    'attachments' => array(
                        array(
                            'type' => $mimeType,
                            'name' => $attachmentName,
                            'content' => $file_encoded
                        )
                    ),                     
                );
            $async = false;
            $ip_pool = 'Main Pool';
            $result = $mandrill->messages->send($message, $async, $ip_pool);
            }

            catch(Mandrill_Error $e) 
            {
                // Mandrill errors are thrown as exceptions
                echo 'A mandrill error occurred: ' . get_class($e) . ' - ' . $e->getMessage();
                // A mandrill error occurred: Mandrill_Unknown_Subaccount - No subaccount exists with the id 'customer-123'
                throw $e;
            }
       }

1 个答案:

答案 0 :(得分:2)

while循环

中构建收件人数组
while($row = mysqli_fetch_assoc($result1)){
    $recipients[] = array(
        'email' => $row['email'],
        'name' => $row['name'] . ' ' . $row['surname'],
        'type' => 'to'
    );
}

并将其传递给mandrill构造

try{   
    $mandrill = new Mandrill('My Key');

    $message = array(
        'html' => $message,
        'subject' => $subject,
        'from_email' => 'email@gmail.com',
        'from_name' => 'Silvan Theuma',
        'to' => $recipients, // here
        'preserve_recipients' => false,
        'attachments' => array(
            array(
                'type' => $mimeType,
                'name' => $attachmentName,
                'content' => $file_encoded
            )
        ),                     
    );

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

catch(Mandrill_Error $e){
    // Mandrill errors are thrown as exceptions
    echo 'A mandrill error occurred: ' . get_class($e) . ' - ' . $e->getMessage();
    // A mandrill error occurred: Mandrill_Unknown_Subaccount - No subaccount exists with the id 'customer-123'
    throw $e;
}

并确保在preserve_recipients数组中将$message添加为false。

  

preserve_recipients:是否将所有收件人公开到" To"每封电子邮件的标题