diff --git a/src/Foundation/InstalledSite.php b/src/Foundation/InstalledSite.php index de15623bd..d59a5443f 100644 --- a/src/Foundation/InstalledSite.php +++ b/src/Foundation/InstalledSite.php @@ -40,7 +40,6 @@ use Illuminate\Contracts\Container\Container; use Illuminate\Filesystem\Filesystem; use Illuminate\Filesystem\FilesystemServiceProvider; use Illuminate\Hashing\HashServiceProvider; -use Illuminate\Mail\MailServiceProvider; use Illuminate\Validation\ValidationServiceProvider; use Illuminate\View\ViewServiceProvider; use Monolog\Formatter\LineFormatter; @@ -118,18 +117,6 @@ class InstalledSite implements SiteInterface $laravel->register(MailServiceProvider::class); $laravel->register(ViewServiceProvider::class); $laravel->register(ValidationServiceProvider::class); - - $settings = $laravel->make(SettingsRepositoryInterface::class); - - $config->set('mail.driver', $settings->get('mail_driver')); - $config->set('mail.host', $settings->get('mail_host')); - $config->set('mail.port', $settings->get('mail_port')); - $config->set('mail.from.address', $settings->get('mail_from')); - $config->set('mail.from.name', $settings->get('forum_title')); - $config->set('mail.encryption', $settings->get('mail_encryption')); - $config->set('mail.username', $settings->get('mail_username')); - $config->set('mail.password', $settings->get('mail_password')); - $laravel->register(DiscussionServiceProvider::class); $laravel->register(FormatterServiceProvider::class); $laravel->register(FrontendServiceProvider::class); diff --git a/src/Foundation/MailServiceProvider.php b/src/Foundation/MailServiceProvider.php new file mode 100644 index 000000000..362832c36 --- /dev/null +++ b/src/Foundation/MailServiceProvider.php @@ -0,0 +1,79 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Foundation; + +use Flarum\Settings\SettingsRepositoryInterface; +use Illuminate\Mail\Mailer; +use Illuminate\Mail\Transport\LogTransport; +use InvalidArgumentException; +use Psr\Log\LoggerInterface; +use Swift_Mailer; +use Swift_SendmailTransport; +use Swift_SmtpTransport; +use Swift_Transport; + +class MailServiceProvider extends AbstractServiceProvider +{ + public function register() + { + $this->app->singleton('swift.mailer', function ($app) { + $settings = $app->make(SettingsRepositoryInterface::class); + + return new Swift_Mailer( + $this->buildTransport($settings) + ); + }); + + $this->app->singleton('mailer', function ($app) { + $mailer = new Mailer( + $app['view'], $app['swift.mailer'], $app['events'] + ); + + if ($app->bound('queue')) { + $mailer->setQueue($app->make('queue')); + } + + $settings = $app->make(SettingsRepositoryInterface::class); + $mailer->alwaysFrom($settings->get('mail_from'), $settings->get('forum_title')); + + return $mailer; + }); + } + + private function buildTransport(SettingsRepositoryInterface $settings): Swift_Transport + { + switch ($settings->get('mail_driver')) { + case 'smtp': + return $this->buildSmtpTransport($settings); + case 'mail': + return new Swift_SendmailTransport; + case 'log': + return new LogTransport($this->app->make(LoggerInterface::class)); + default: + throw new InvalidArgumentException('Invalid mail driver configuration'); + } + } + + private function buildSmtpTransport(SettingsRepositoryInterface $settings): Swift_Transport + { + $transport = new Swift_SmtpTransport( + $settings->get('mail_host'), + $settings->get('mail_port'), + $settings->get('mail_encryption') + ); + + $transport->setUsername($settings->get('mail_username')); + $transport->setPassword($settings->get('mail_password')); + + return $transport; + } +}