Reputation: 402
Using laravel I'm trying to send batch emails with the Mailgun api.
$recipients = new Collection();
$subscription_addresses = new Collection();
foreach($request['subscriptions'] as $subscription){
$recipients->put($subscription['email'], (object)array('id' => $subscription['id']));
$subscription_addresses->push($subscription['email']);
}
$recipient_chunks = $recipients->chunk(500);
$subscription_address_chunks = $subscription_addresses->chunk(500);
for($i = 0; $i < count($recipient_chunks); $i++){
$mgClient = Mailgun::create(env('MAILGUN_SECRET'), 'https://' . env('MAILGUN_ENDPOINT'));
$domain = env('MAILGUN_DOMAIN');
$params = array(
'from' => env('MAIL_FROM_NAME') . ' <' . env('MAIL_FROM_ADDRESS') . '>',
'to' => $subscription_address_chunks[$i]->toArray(),
'subject' => $request['newsletterForm']['subject'],
'html' => view('emails.newsletter', $request['newsletterForm'])->render(),
'recipient-variables' => json_encode($recipient_chunks[$i], JSON_FORCE_OBJECT)
);
# Make the call to the client.
$mgClient->messages()->send($domain, $params);
}
The chunking per 500 is because Mailgun queues only 500 mails per request according their docs.
Anyway, while testing with 3 recipients, everything works fine on localhost.
However, while sending on a live domain, I get an error Mailgun's servers are currently unreachable.
Anybody who knows this issue? Not much to find on stackoverflow or in the mailgun docs.
The MX records and mailgun configuration should be ok since I'm able to send single mails on the live site with
Mail::send('emails.confirmation', $request->all(), function($message) use ($request){
$message->from(env('MAIL_FROM_ADDRESS'), env('MAIL_FROM_NAME'));
$message->to($request['userInfo']['email'], $request['userInfo']['firstname'] .' '. $request['userInfo']['lastname'])->subject('Uw bestelling bij Hof te Berchemveld');
});
Upvotes: 0
Views: 1518
Reputation: 11
I was facing the same issue & found out that my domain & sender mail domain was different. Now after resolving that issue it worked fine for me.
Upvotes: 1