简体   繁体   中英

How can I set multiple sending addresses in Laravel 5 .env file?

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?

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:

MAIL_ONLOGIN=larry@example.com
MAIL_ONSIGNUP=moe@example.com
MAIL_ONERROR=curly@example.com

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.

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:

  • If you set anything other than null in the from configuration on the config/mail.php file.
  • Then the MailServiceProvider will call the Mailer method alwaysFrom with it when setting the mailer object.
  • If the 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:

  • (Optionally) If you want to have a default from address, do set the from address in the config/mail.php file, you could use env() to set it actually in the .env file.
  • Then, before each time you call the 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('us@example.com', 'Laravel');
  $message->to('foo@example.com')->cc('bar@example.com');
  // ...
});

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:

  • Create your own extended class from Illuminate\\Mail\\Mailer
  • Override the 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.
  • Create your own extended class from Illuminate\\Mail\\MailServiceProvider
  • Override the register() method in your service provider, so that instantiates your own Mailer instead of the original one.
  • Remove or comment out the 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.

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' => 'someone@example.com',
    'orders' => 'someone.else@example.com',

];

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.

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:

ADMIN_EMAILS=admin1@site.com,admin2@site.com,admin3@site.com

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM