Hi everyone, I’m having trouble sending out invitation emails from my Laravel app using Mailgun. I’ve set up a form to collect the recipient’s email and some other details. The form submits to a controller method that should handle the email sending, but it’s not working as expected.
Here’s a simplified version of my code:
public function sendInvite(Request $request)
{
$inviteData = [
'recipient' => $request->input('email'),
'house_id' => $request->input('house_id'),
'role' => $request->input('role_id')
];
Mail::send('invite_template', $inviteData, function ($message) use ($inviteData) {
$message->to($inviteData['recipient'])->subject('Join Our App');
});
return redirect()->home();
}
I’ve double-checked my Mailgun settings in the .env file and config files. The domain and API key seem correct. Am I missing something obvious? Has anyone run into similar issues with Laravel and Mailgun?
hey, have u tried using a queue for sending emails? sometimes it helps with delivery issues. also, check ur spam folder - mailgun emails can end up there. if nothing works, maybe switch to a different email service like SendGrid? they have a laravel driver too. good luck!
I’ve dealt with similar Mailgun issues in Laravel before. One thing that’s not immediately obvious from your code is whether you’re catching any exceptions. Mailgun can silently fail, leaving you wondering why emails aren’t being sent.
Try wrapping your Mail::send() in a try-catch block:
try {
Mail::send('invite_template', $inviteData, function ($message) use ($inviteData) {
$message->to($inviteData['recipient'])->subject('Join Our App');
});
} catch (\Exception $e) {
Log::error('Failed to send email: ' + $e->getMessage());
return redirect()->back()->with('error', 'Failed to send invitation');
}
This way, you’ll at least see if there’s an error occurring. Also, double-check your Mailgun sandbox domain if you’re using a free account - those have limitations. Lastly, ensure your DNS records are properly set up if you’re using a custom domain. These steps helped me resolve my Mailgun issues in Laravel.
Have you checked your Mailgun logs? They can be incredibly helpful for troubleshooting. Log into your Mailgun dashboard and look for any error messages or failed deliveries. Sometimes the issue isn’t in your Laravel code, but on the Mailgun side.
Also, make sure you’re using the correct Mailgun region in your config. If you’re in the EU, you need to set a different endpoint. In your .env file, it should look like this:
Lastly, try sending a test email directly through the Mailgun API to isolate whether it’s a Laravel issue or a Mailgun configuration problem. This approach has saved me hours of debugging in the past.