diff --git a/app/Http/Controllers/AppController.php b/app/Http/Controllers/AppController.php index f9290d2221da..53e01611239b 100644 --- a/app/Http/Controllers/AppController.php +++ b/app/Http/Controllers/AppController.php @@ -169,14 +169,18 @@ class AppController extends BaseController $_ENV['DB_PASSWORD'] = $db['type']['password']; if ($mail) { - $_ENV['MAIL_DRIVER'] = $mail['driver']; - $_ENV['MAIL_PORT'] = $mail['port']; - $_ENV['MAIL_ENCRYPTION'] = $mail['encryption']; - $_ENV['MAIL_HOST'] = $mail['host']; - $_ENV['MAIL_USERNAME'] = $mail['username']; - $_ENV['MAIL_FROM_NAME'] = $mail['from']['name']; - $_ENV['MAIL_FROM_ADDRESS'] = $mail['from']['address']; - $_ENV['MAIL_PASSWORD'] = $mail['password']; + $prefix = ''; + if (($user = auth()->user()) && Account::count() > 1) { + $prefix = $user->account_id . '_'; + } + $_ENV[$prefix . 'MAIL_DRIVER'] = $mail['driver']; + $_ENV[$prefix . 'MAIL_PORT'] = $mail['port']; + $_ENV[$prefix . 'MAIL_ENCRYPTION'] = $mail['encryption']; + $_ENV[$prefix . 'MAIL_HOST'] = $mail['host']; + $_ENV[$prefix . 'MAIL_USERNAME'] = $mail['username']; + $_ENV[$prefix . 'MAIL_FROM_NAME'] = $mail['from']['name']; + $_ENV[$prefix . 'MAIL_FROM_ADDRESS'] = $mail['from']['address']; + $_ENV[$prefix . 'MAIL_PASSWORD'] = $mail['password']; $_ENV['MAILGUN_DOMAIN'] = $mail['mailgun_domain']; $_ENV['MAILGUN_SECRET'] = $mail['mailgun_secret']; } diff --git a/app/Libraries/HTMLUtils.php b/app/Libraries/HTMLUtils.php index 1b42f1bbe7b7..3fc7b5957bbb 100644 --- a/app/Libraries/HTMLUtils.php +++ b/app/Libraries/HTMLUtils.php @@ -61,4 +61,17 @@ class HTMLUtils return $previous; } } + + public static function getEnvForAccount($field, $default = '') + { + $key = ''; + + if ($user = auth()->user()) { + $key .= $user->account->id . '_'; + } + + $key .= $field; + + return env($key, env($field, $default)); + } } diff --git a/app/Ninja/Mailers/Mailer.php b/app/Ninja/Mailers/Mailer.php index eded42974e06..aeda14942136 100644 --- a/app/Ninja/Mailers/Mailer.php +++ b/app/Ninja/Mailers/Mailer.php @@ -29,22 +29,36 @@ class Mailer return true; } - /* - if (isset($_ENV['POSTMARK_API_TOKEN'])) { - $views = 'emails.'.$view.'_html'; - } else { - $views = [ - 'emails.'.$view.'_html', - 'emails.'.$view.'_text', - ]; - } - */ - $views = [ 'emails.'.$view.'_html', 'emails.'.$view.'_text', ]; + if (Utils::isSelfHost()) { + if (isset($data['account'])) { + $account = $data['account']; + if (env($account->id . '_MAIL_FROM_ADDRESS')) { + $fields = [ + 'driver', + 'host', + 'port', + 'from.address', + 'from.name', + 'encryption', + 'username', + 'password', + ]; + foreach ($fields as $field) { + $envKey = strtoupper(str_replace('.', '_', $field)); + if ($value = env($account->id . '_MAIL_' . $envKey)) { + config(['mail.' . $field => $value]); + } + } + (new \Illuminate\Mail\MailServiceProvider(app()))->register(); + } + } + } + try { $response = Mail::send($views, $data, function ($message) use ($toEmail, $fromEmail, $fromName, $subject, $data) { $toEmail = strtolower($toEmail); diff --git a/resources/views/partials/system_settings.blade.php b/resources/views/partials/system_settings.blade.php index 77331408abc7..223d36bb1716 100644 --- a/resources/views/partials/system_settings.blade.php +++ b/resources/views/partials/system_settings.blade.php @@ -33,23 +33,23 @@