【发布时间】:2016-01-25 07:38:30
【问题描述】:
我用来通过Mailgun Api发送批量邮件的这个功能,它在没有时工作。收件人的数量小于 1000,但收件人大于 2000 时失败。
我正在做的是,我正在获取收件人列表,然后将它们分成 5 组(1 批邮件中的 5 位收件人)。批量邮件包含各种内容占位符(%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;
}
}
【问题讨论】:
标签: mailgun