Hi,
The 5.1 documentation says:
Another solution provided by Laravel is to set a universal recipient of all e-mails sent by the framework. This way, all the emails generated by your application will be sent to a specific address, instead of the address actually specified when sending the message. This can be done via the to option in your config/mail.php configuration file:
'to' => [
'address' => '[email protected]',
'name' => 'Dev Example'
],
In a Controller I have:
Mail::queue('emails.beta-invite', $data, function ($m) use ($userProfile) {
$m->to($userProfile->email)->subject('...');
});
In the config/mail.php I added what was suggested:
'to' => [
'address' => '[email protected]',
'name' => 'Dev Example Mail'
],
And for the mail driver I use Mandrill.
When I send the email it is sent at the $userProfile->email instead of the Unversal To.
Did I miss something?
The universal to is only used if you don't provide one in your code, but you did provide one, so we used that instead.
Thank you @GrahamCampbell
Don't you think we could update a little bit the documentation to make it even more "clear"?
This way, all the emails generated by your application will be sent to a specific address, instead of the address actually specified when sending the message.
This doesn't indicate we have to remove the $m->to ..
Maybe yeh. We're open to PRs if you have an free time. :)
As described by the documentation it should override a set to value. The description @GrahamCampbell you have provided seems more like a catch all for situations when you purposefully or otherwise forget to set a to address.
Is it the documentation or code that needs a PR? Which behaviour is correct?
For me, based on the documentation the Universal To should be an override and not a catch all.
Updated: when no ->to() call, the Universal To works properly as a catch all.
@jrean I agree strongly. Universal To should prevent any other email from being used, not only work as a fallback. When I roll out to production, the only thing that should have to change in my code is what's inside config/mail.php, not all the code with all the calls to the to() method
I'll create some PR tonight or something.
Meanwhile, if you encounter this: you can still configure the 'to' address in config/mail.php , and use the mailer event, roughly like so:
Event::listen('mailer.sending', function (Swift_Message $message) {
$message->setTo(Config::get('mail.to.address'), Config::get('mail.to.name'));
});
Most helpful comment
@jrean I agree strongly. Universal To should prevent any other email from being used, not only work as a fallback. When I roll out to production, the only thing that should have to change in my code is what's inside
config/mail.php, not all the code with all the calls to theto()method