没有时,Mailgun批量电子邮件发送不起作用。收件人超过2000

时间:2016-01-25 07:38:31

标签: mailgun

此功能我用于通过Mailgun Api发送批量邮件,当没有时它可以正常工作。收件人的数量少于1000但收件人数超过2000时失败。

我正在做的是,我正在获取收件人列表,然后将它们分成5串(5个收件人,1批邮件)。批量邮件包含各种内容占位符(%recipient.name%),当从Mailgun发送批量邮件时,它将被原始数据替换,我也在批量邮件中添加一些自定义数据以跟踪Mailgun日志中的邮件

/**
 * Used to send batch mails
 * @param text $subject content mail subject
 * @param text $text content mail in text body
 * @param array $from sender details
 * @param array $recipients content all recipients details
 * @param text $htmlBody content mail html content
 * @param int $type content mail type
 * @param int $broadcastId batch mail id
 * @param int $broadcastType type of broadcast email
 * @param int $isUser content user or contact
 * @return boolean
*/
public function sendBatchMail($subject, $text, $from = '',          $recipients = array(), $htmlBody = '', $type = '', $broadcastId = '',
        $broadcastType = '', $isUser = '')
{

    //get the broadcast mailgun info
    $this->_mgClient = apiKey;
    $this->_domain = domain;

    // divide batch mail in chunk of 5(sending 5 emails in 1 batch mail)
    $recipientsArr = array_chunk($recipients, 5);

    try {
        foreach ($recipientsArr as $allRecipient) {
            $batchMsg = $this->_mgClient->BatchMessage($this->_domain);

            // Define the subject.
            $batchMsg->setSubject($subject);

            // Define the body of the message.
            $batchMsg->setTextBody($text);

            $batchMsg->setHtmlBody($htmlBody);

            // filter from and set
            $from = $this->_getBatchDetail($from);

                $batchMsg->setFromAddress(
                    $from['email'], $from['detail']
                );

            // loop through $allRecipient and add recipient
            if (is_array($allRecipient)) {
                $customData = array();
                foreach ($allRecipient as $eachRecipient) {
                    // filter recipient and set
                    $to = $this->_getBatchDetail($eachRecipient);
                    if ($to) {
                        $data['accountId']
                            = !empty($eachRecipient['accountId'])
                            ? $eachRecipient['accountId'] : '';
                        $data['recipientId']
                            = !empty($eachRecipient['id'])
                            ? $eachRecipient['id'] : '';
                        $data['subject'] = $subject;
                        $data['sendDate'] = date("Y-m-d H:i:s");
                        $data['mailType'] = $type;
                        $data['mailId'] = $broadcastId;
                        $data['broadcastType'] = $broadcastType;
                        $data['recipientType'] = $isUser;
                        $data['email'] = !empty($eachRecipient['email'])
                            ? $eachRecipient['email']
                            : '';
                        $customData[] = $data;
                        $batchMsg->addToRecipient(
                            $to['email'],
                            $to['detail']
                        );
                    }
                }
                $batchMsg->addCustomData('custom-data', $customData);
            }

            // Call finalize() to send any remaining recipients still in the buffer.
            $batchMsg->finalize();
        }
        // finalize doesn't return anything. so if dont through any error, its a success
        return true;
    } catch (Exception $ex) {
        error_log($ex->getMessage());
        return false;
    }

}

1 个答案:

答案 0 :(得分:0)

根据他们的文档(https://documentation.mailgun.com/user_manual.html#batch-sending),批量发送的最大允许收件人数为1000,因此请将您的批次保留在此之下。

相关问题