简体   繁体   English

如何在Laravel 5 .env文件中设置多个发送地址?

[英]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. 我一直在搜索,我查看了Laravel 5文档,但未找到任何内容。 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? 在Laravel .env示例文件中,它显示了如何将其设置为从一个电子邮件地址发送,但是说我想针对不同情况从不同的电子邮件地址发送,我该怎么做?

The .env file is simply a way of setting values for your application. .env文件只是为您的应用程序设置值的一种方法。 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: 因此,如果您想在环境中为不同的事件设置多个电子邮件地址,则可以在.env文件中执行以下操作:

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. 我还应该提到.env文件旨在供开发使用-当需要将应用程序部署到生产环境时,请删除.env文件,并将服务器上的“实际”环境变量用于这些值。

There is no "default" way to define sets of from addresses in Laravel, although you could implement it. 尽管可以实现,但没有“默认”方法来定义Laravel中的发件人地址集。

Here is how it works the use of "From" when sending email in Laravel: 这是在Laravel中发送电子邮件时使用“发件人”的工作方式:

  • If you set anything other than null in the from configuration on the config/mail.php file. 如果在config/mail.php文件的from配置中设置了除null以外的任何内容。
  • Then the MailServiceProvider will call the Mailer method alwaysFrom with it when setting the mailer object. 然后,在设置邮件程序对象时, MailServiceProvider将使用它alwaysFrom调用Mailer方法。
  • 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. 如果alwaysFrom设置了一个地址,则每次发送电子邮件时,它将使用该地址作为发件人地址。

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. 未设置它的问题是,对于不受控制的发件人地址的情况,您将没有“默认”发件人地址,例如PasswordBroker(发送密码重置电子邮件),以及任何其他您包括那可以发送电子邮件。

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. (可选)如果要使用默认的config/mail.php地址,请在config/mail.php文件中设置config/mail.php地址,您可以使用env().env文件中进行设置。
  • 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. 然后,在每次调用Mailer::send()方法并想要一个不同的“ Mailer::alwaysFrom() ”地址之前,只需使用Mailer::alwaysFrom()地址调用Mailer::alwaysFrom()方法,或将此Mailer::alwaysFrom(null)设置为null即可。 Mailer::alwaysFrom(null) ,然后,可以在发送到Mailer::send()方法的信息中发送“ Mailer::alwaysFrom(null) ”地址和名称。

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 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. 覆盖自定义Mailer中的send()方法,以便它使用$view名称来查找应从数据库表或类似表中使用的“发件人”地址,并在未找到时使用“默认” 。
  • Create your own extended class from Illuminate\\Mail\\MailServiceProvider Illuminate\\Mail\\MailServiceProvider创建您自己的扩展类
  • Override the register() method in your service provider, so that instantiates your own Mailer instead of the original one. 重写服务提供者中的register()方法,以便实例化您自己的Mailer而不是原始的Mailer
  • Remove or comment out the Illuminate\\Mail\\MailServiceProvider::class, from your config/app.php file and put your own ServiceProvider there. config/app.php文件中删除或注释掉Illuminate\\Mail\\MailServiceProvider::class,然后在其中放置您自己的ServiceProvider。

And that is it!, you have now an improved Mailer that will use the from address based on your view name. 就是这样!您现在有了一个改进的Mailer,它将根据您的视图名称使用发件人地址。

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. 注意:如果您在config/mail.php设置config/mail.php地址,它将在创建电子邮件时覆盖所有以“发件人”发送的内容。

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: 一种方法是创建一个包含地址数组的配置文件(例如config / addresses.php ):

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: 在您的.env文件中,将所有电子邮件作为逗号分隔值添加:

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): 因此,当您要发送电子邮件时,只需执行以下操作(是的!消息生成器实例的“ to”方法接受一个数组):

So, 所以,

$to = explode(',', env('ADMIN_EMAILS'));

and... 和...

$message->to($to);

will now send the mail to all the admins. 现在将邮件发送给所有管理员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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