使用Mailgun发邮件遇“Trying to get property 'message' of non-object”错误求助
Hey Reza, let's break down this error and fix it step by step. That error message tells us your code is trying to access a message property on something that isn't an object—usually a null value, array, or unexpected response format from Mailgun. Here's how to diagnose and resolve it:
1. First, Fix Critical .env Configuration Issues
Looking at your .env settings, there are two red flags that could cause Mailgun to return invalid responses:
- Incorrect variable syntax: You used a colon (
:) forMAIL_FROM_ADDRESSinstead of an equals sign (=). Laravel won't parse this variable correctly. Change it to:MAIL_FROM_ADDRESS=hello@yourdomain.com - Invalid encryption setting: Setting
MAIL_ENCRYPTION=nullcan cause connection issues on port 2525. Mailgun recommends using TLS for this port, so update it to:MAIL_ENCRYPTION=tls
Double-check these other settings too to be safe:
- Confirm
MAIL_USERNAME(postmaster@your-sandbox-domain) matches your Mailgun sandbox domain exactly - Verify
MAIL_PASSWORDis the SMTP password from your Mailgun dashboard (not the same asMAILGUN_SECRET)
2. Diagnose the Response Handling Code
The error almost certainly comes from how you're processing the Mailgun response after sending. For example, if you have code like this:
$response = Mail::send(...); echo $response->message;
Laravel's Mail::send() returns a boolean (true on success) by default—not an object. Trying to access ->message on a boolean will throw this exact error.
To safely handle responses, use Laravel's built-in methods to get valid Mailgun data:
// Send your email as usual Mail::send('your-email-template', $data, function($message) { $message->to('recipient@example.com')->subject($data['title']); }); // Fetch the raw Mailgun responses (Laravel 5.5+) $transport = Mail::getSwiftMailer()->getTransport(); $responses = $transport->getResponses(); foreach ($responses as $response) { // Check if the response is an object before accessing properties if (is_object($response) && property_exists($response, 'message')) { // Do something with $response->message dd($response->message); } // Fallback for array-format responses elseif (is_array($response) && isset($response['message'])) { dd($response['message']); } }
3. Rule Out Edge Cases in Your $data Array
While not directly causing the non-object error, your $data has a potential bug:
'date' => $time, 'time' => $request->input('start'), 'address' => $request->input('title') // <-- This uses 'title' instead of 'address'!
This will populate the address field with the title value, which might break your email template. Correct it to $request->input('address') if that's intended.
4. Test with a Minimal Email
To isolate the issue, send a simple test email without your custom $data to confirm your Mailgun connection works:
Mail::raw('This is a test email from Mailgun', function($message) { $message->to('your-personal-email@example.com')->subject('Mailgun Test'); });
If this works, the problem is in your template or $data setup. If it fails, revisit your .env configuration or check Mailgun's dashboard for failed request logs.
Final Tip: Enable Debug Mode
Turn on debug mode in your .env to get a full error stack trace, which will show you exactly which line of code is triggering the non-object error:
APP_DEBUG=true
内容的提问来源于stack exchange,提问作者reza baghiee

