From fcb20659a43a4a9ddae3c830d8f8d66d57de865a Mon Sep 17 00:00:00 2001 From: Adam Nielsen <1765602+iwasherefirst2@users.noreply.github.com> Date: Tue, 10 Sep 2019 17:54:16 +0200 Subject: [PATCH] Added default mail for local setup --- README.md | 29 +++++++++++++++++++++++++++++ src/MultiMailer.php | 11 ++++------- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 7cf3511..3cb1efe 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,35 @@ i.e. they are either send explicitly be the `queue` method or the mailable class It is of course necessary to install a [queue driver](https://laravel.com/docs/5.8/queues#driver-prerequisites). + +### Default mails + +It is recommended to **avoid** putting your actual mail credentials into your local `.env` to prevent sending testing mails to actual users. +Instead of adding fake entries in your `.env` file for any mail provided in `config/multimail.php`, simply use a fallback mail that should +be used whenever the username/password cannot be found in the `.env` file. To do so, add a `default` entry inside the `email` array from `config/multimail.php`: + + 'emails' => [ + 'office@example.net' => + [ + 'pass' => env('first_mail_password'), + 'username' => env('first_mail_username'), + 'from' => "Max Musterman", + ], + 'contact@example.net' => + [ + 'pass' => env('second_mail_password'), + 'username' => env('second_mail_username'), + 'from' => "Alice Armania", + ], + 'default' => + [ + 'pass' => env('MAIL_PASSWORD'), + 'username' => env('MAIL_USERNAME'), + ] + ], + + + ### Bulk messages For bulk messages, you may first require a mailer object. You can define a pause in seconds ($timeout) after a number of mails ($frequency) has been send. diff --git a/src/MultiMailer.php b/src/MultiMailer.php index 7c2ac36..7512b99 100644 --- a/src/MultiMailer.php +++ b/src/MultiMailer.php @@ -44,11 +44,12 @@ public static function getMailer($name, $timout = null, $frequency = null) $name = $name['email']; } - $config = config('multimail.emails')[$name]; - if(empty($name) or empty($config)) + if(empty($name) || empty($config) || empty($config['pass']) || empty($config['username'])) { - throw new \Exception("Configuration for email: " . $name . ' is missing in config/multimail.php', 1); + $config = config('multimail.emails.default'); + + if(empty($config) || empty($config['pass']) || empty($config['username'])) throw new \Exception("Configuration for email: " . $name . ' is missing in config/multimail.php and no default is specified.', 1); } @@ -57,10 +58,6 @@ public static function getMailer($name, $timout = null, $frequency = null) return call_user_func_array($config['function_call'], $config['function_pars']); } - if(empty($config['pass']) || empty($config['username'])){ - throw new \Exception("Username or password is empty for mail provider " . $name, 1); - } - $provider = (!empty($config['provider'])) ? $config['provider'] : config('multimail.provider.default'); //https://stackoverflow.com/a/56965347/2311074