Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. A universal to in development environments ensures all emails go to that address, instead of out to real customers/clients/whatever.

I can't work out how to specify this differently in production though. In production there should be no universal to - emails should go out to real addresses.

The standard approach of using different env() values does not seem to work. For example:

config/mail.php:

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

development .env:

[email protected]

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:

production .env

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' => '[email protected]'
    ];
}

return $return;

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

like image 797
Don't Panic Avatar asked Apr 11 '17 08:04

Don't Panic


People also ask

What is the difference between notification and mail in Laravel?

From what I understand, Mailables are used to send only emails whereas Notifications can be used to send emails and sms. In my application, I dont have plans to send sms notifications for now, so I am confused if I should just use the Mailable class in this case.

What is SMTP in Laravel?

SMTP, Mailgun, Postmark, and Amazon SES are used in Laravel for sending simple, transactional, and bulk emails. Laravel has an email-sending library named SwiftMailer to send an email with an email template. This tutorial shows you how to send a simple email using SMTP.

How do I get email notifications in Laravel?

Send Email Notifications in Laravel php use App\Notifications\Newvisit; Route::get('/', function () { $user = App\User::first(); $user->notify(new Newvisit("A new user has visited on your application.")); return view('welcome'); });

What is Sendmail in Laravel?

Laravel uses free feature-rich library SwiftMailer to send emails. Using the library function, we can easily send emails without too many hassles. The e-mail templates are loaded in the same way as views, which means you can use the Blade syntax and inject data into your templates.


1 Answers

I think this should work - it leaves config('mail.to') as null unless UNIVERSAL_TO is set.

'to' => env('UNIVERSAL_TO', false) ? [
    'address' => env('UNIVERSAL_TO'),
    'name' => env('UNIVERSAL_TO_NAME')
] : null,
like image 110
ceejayoz Avatar answered Sep 29 '22 09:09

ceejayoz