Laravel 5.4通知邮件添加BCC报错,求解决方案
Hey there! Let's get this BCC issue sorted out for you. That error you're seeing happens because the notify() method doesn't return a mail object you can chain methods onto—it actually returns either void or a boolean, hence the "call on null" error when you try to add ->bcc() after it.
The Correct Way to Add BCC to Laravel Notifications
Instead of trying to chain BCC onto the notify() call, you need to set up the BCC directly inside your SupportEmail notification class. Here's how to adjust your code:
First, update your SupportEmail class to include the BCC in the toMail() method:
use Illuminate\Notifications\Notification; use Illuminate\Notifications\Messages\MailMessage; class SupportEmail extends Notification { protected $userEmail; protected $inquiryEmail; public function __construct($userEmail, $inquiryEmail) { $this->userEmail = $userEmail; $this->inquiryEmail = $inquiryEmail; } public function via($notifiable) { return ['mail']; } public function toMail($notifiable) { return (new MailMessage) ->subject('Support Inquiry Received') ->greeting("Hi there!") ->line("We've received your inquiry and will get back to you soon.") // Add your BCC recipients here ->bcc("example@example.com") ->bcc("admin@admin.com") // Alternatively, pass an array of emails in one call: // ->bcc(["example@example.com", "admin@admin.com"]) ->action('Check Your Inquiry Status', url('/inquiries/' . $notifiable->id)) ->line("Thanks for reaching out!"); } }
Then, call the notification normally without the chained bcc() method:
$inquiry->notify(new SupportEmail("user@user.com", $inquiry->email));
Why This Works
Laravel's MailMessage class in 5.4 includes the bcc() method, which lets you add blind carbon copy recipients directly when building your email message. By setting this inside the notification class, you're ensuring the BCC is applied to the actual mail instance that gets sent out.
内容的提问来源于stack exchange,提问作者Kenneth

