Reputation: 1
this function i am using to send batch mail through Mailgun Api, it works when no. of recipient is less then 1000 but it fails when recipient is more then 2000.
What i am doing is, i am getting list of recipient and then dividing them in bunch of 5(5 recipient in 1 batch mail). Batch mail contains various content place holder(%recipient.name%) which get replaced with original data while sending batch mail from Mailgun, Also i am adding some custom data to batch mail to track mails in Mailgun log
.
/**
* 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;
}
}
Upvotes: 0
Views: 1329
Reputation: 11
According to their documentation (https://documentation.mailgun.com/user_manual.html#batch-sending) the maximum number of allowed recipients for Batch Sending is 1000, so keep your batches under that.
Upvotes: 1