我正在使用 Laravel 5.2 应用程序。在我的开发和暂存环境中,我想使用“通用收件人”邮件配置选项 described in the docs .开发环境中的通用 to 确保所有电子邮件都发送到该地址,而不是发送给真正的客户/客户/其他任何东西。
不过,我不知道如何在生产中以不同方式指定它。在生产环境中,应该没有通用地址——电子邮件应该发送到真实地址。
使用不同 env()
值的标准方法似乎不起作用。例如:
config/mail.php:
'to' => [
'address' => env('UNIVERSAL_TO', false)
],
开发.env:
UNIVERSAL_TO=my-testing-address@somewhere.com
这工作正常 - 所有电子邮件都按预期发送到指定的 UNIVERSAL_TO
。但是,如果我将其更改为我在生产中想要的内容,例如:
生产.env
UNIVERSAL_TO=
(或 =''
,或 =false
,或完全省略它),发送任何邮件失败(在 storage/laravel.log
):
local.ERROR: exception 'Swift_RfcComplianceException' with message 'Address in mailbox given [] does not comply with RFC 2822, 3.6.2.' in path/to/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mime/Headers/MailboxHeader.php:348
config/mail.php
只是返回一个数组,所以我想我可以改为将它设置为一个变量,然后根据环境将“to”附加到它,如下所示:
$return = [ ... normal mail config array ... ];
if (!\App::environment('production')) {
$return['to'] => [
'address' => 'my-testing-address@somewhere.com'
];
}
return $return;
但这似乎有点……老套。有没有更好的办法?
最佳答案
我认为这应该可行 - 它会将 config('mail.to')
保留为 null
除非设置了 UNIVERSAL_TO
。
'to' => env('UNIVERSAL_TO', false) ? [
'address' => env('UNIVERSAL_TO'),
'name' => env('UNIVERSAL_TO_NAME')
] : null,
关于php - 开发与生产中的 Laravel "universal to",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43341220/