Reputation: 451
I have been searching, I checked Laravel 5 documentation but I haven’t found anything. In the Laravel .env example file it shows how you can set it to send from one email address, but say I want to send from different email addresses for different situations, how would I do that?
Upvotes: 2
Views: 4107
Reputation: 3967
If you want to send emails simultaneously to all the admins, you can do something like this:
In your .env file add all the emails as comma separated values:
[email protected],[email protected],[email protected]
so when you going to send the email just do this (yes! the 'to' method of message builder instance accepts an array):
So,
$to = explode(',', env('ADMIN_EMAILS'));
and...
$message->to($to);
will now send the mail to all the admins.
Upvotes: 2
Reputation: 1359
There is no "default" way to define sets of from addresses in Laravel, although you could implement it.
Here is how it works the use of "From" when sending email in Laravel:
null
in the from
configuration on the config/mail.php
file. MailServiceProvider
will call the Mailer
method alwaysFrom
with it when setting the mailer object.alwaysFrom
is set with an address then each time that it sends an email it will use this address as the from address.The problem of not setting it is that you won't have a "default" from address for the cases that you don't control the from address, like PasswordBroker (which sends the password reset email) for example, and any other library that you include that could send emails.
So, with that in mind, you could do the following:
config/mail.php
file, you could use env()
to set it actually in the .env
file.Mailer::send()
method and you want a different "from" address, you just call Mailer::alwaysFrom()
method with the from address that you want or set it to null like this Mailer::alwaysFrom(null)
and then, you can send the "from" address and name in the information that you send to the Mailer::send()
method.For example:
Mail::alwaysFrom(null);
Mail::send('emails.welcome', $data, function ($message) {
$message->from('[email protected]', 'Laravel');
$message->to('[email protected]')->cc('[email protected]');
// ...
});
Or you could do something like this:
// Get from the database, config or any other way which address from you want to use
Mail::alwaysFrom($fromEmail, $fromName);
Mail::send('emails.welcome', $data, function ($message) {
// ...
});
Or, if you want something more "automatic" you could do the following:
Illuminate\Mail\Mailer
send()
method in your custom Mailer, so that it uses the $view
name to find which "from" address should use from maybe a database table or something like that and has a "default" from for when it was not found.Illuminate\Mail\MailServiceProvider
register()
method in your service provider, so that instantiates your own Mailer
instead of the original one.Illuminate\Mail\MailServiceProvider::class,
from your config/app.php
file and put your own ServiceProvider there.And that is it!, you have now an improved Mailer that will use the from address based on your view name.
I hope that helps.
Note: If you set the from address in the config/mail.php
it will override anything that you send as "From" when creating the email.
Upvotes: 2
Reputation: 39389
There’s no de facto way to set multiple from addresses. How you implement this is up to you.
One approach would be to create a config file (say config/addresses.php) that has an array of your addresses:
return [
'enquiries' => '[email protected]',
'orders' => '[email protected]',
];
And then just select them when sending an email:
Mail::send('enquiry', $data, function ($message) {
$message->from(config('addresses.enquiries'));
});
If you don’t want email addresses hard-coded in your application’s source code like this, then you could either move them to environment variables, or the database if you want them configurable.
Upvotes: 1
Reputation: 15760
The .env
file is simply a way of setting values for your application. You can (within certain limits -see this answer for a discussion) call these values anything you like. So if you wanted to set up multiple email addresses in your environment for different events, you could do something like this in your .env
file:
[email protected]
[email protected]
[email protected]
Then, in your application's event handlers (or wherever you need to), just access the appropriate email address like this:
// time to inform someone of a catastrophic error
Mail::send(
'emails.error',
['error' => $exception],
function ($m) use ($exception) {
$m->to(env('MAIL_ONERROR'))
->subject('Something broke! ' . $exception->getMessage());
});
I should also mention that the .env
file is intended to be for development use - when it comes time to deploy your app to production, remove the .env
file and use "real" environment variables on your server for those values.
Upvotes: 1