Tuesday, April 11, 2017

Laravel "universal to" in development vs production

I'm working with a Laravel 5.2 application. In my development and staging environments, I'd like to make use of the "Universal To" mail config option described in the docs.

I can't work out how to specify this differently in production though. The standard approach of using different env() values does not seem to work. For example:

config/mail.php:

'to' => [
    'address'   => env('UNIVERSAL_TO', false)
],

.env:

UNIVERSAL_TO=my-testing-address@somewhere.com

This works fine - all emails go to the specified UNIVERSAL_TO, as expected. But if I change that to what I will want in production, eg:

UNIVERSAL_TO=

(or ='', or =false, or simply omitting this completely), sending any mail fails with (in 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 is just returning an array, so I suppose I could instead set it as a variable and then depending on environment append the 'to' to it, like so:

$return = [ ... normal mail config array ... ];

if (!\App::environment('production')) {
    $return['to'] => [
        'address' => 'my-testing-address@somewhere.com'
    ];
}

return $return;

But this seems a little ... hacky. Is there a better way?



via Don't Panic

Advertisement