You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Mailgun发邮件遇“Trying to get property 'message' of non-object”错误求助

Troubleshooting "Trying to get property 'message' of non-object" in Mailgun + Laravel

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 (:) for MAIL_FROM_ADDRESS instead 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=null can 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_PASSWORD is the SMTP password from your Mailgun dashboard (not the same as MAILGUN_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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:17:46