首页 文章

使用mandrill发送多封电子邮件

提问于
浏览
0

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

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

我也知道,为了能够运行一次Mandrill API并发送多封电子邮件,必须为每个电子邮件地址重复Mandrill API中的“to”数组 . 我想要做的是获得某种循环,为Mandrill API中的每个电子邮件重复“to”数组,从而运行整个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 回答

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

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

    preserve_recipients:是否将所有收件人公开到每个电子邮件的“收件人” Headers 中

相关问题