diff --git a/framework/core/src/Foundation/InstalledSite.php b/framework/core/src/Foundation/InstalledSite.php index 88abddbd6..b7262ce41 100644 --- a/framework/core/src/Foundation/InstalledSite.php +++ b/framework/core/src/Foundation/InstalledSite.php @@ -23,6 +23,7 @@ use Flarum\Forum\ForumServiceProvider; use Flarum\Frontend\FrontendServiceProvider; use Flarum\Group\GroupServiceProvider; use Flarum\Locale\LocaleServiceProvider; +use Flarum\Mail\MailServiceProvider; use Flarum\Notification\NotificationServiceProvider; use Flarum\Post\PostServiceProvider; use Flarum\Search\SearchServiceProvider; diff --git a/framework/core/src/Foundation/MailServiceProvider.php b/framework/core/src/Foundation/MailServiceProvider.php deleted file mode 100644 index a4f8e6d63..000000000 --- a/framework/core/src/Foundation/MailServiceProvider.php +++ /dev/null @@ -1,87 +0,0 @@ - - * - * 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('mail.supported_drivers', function () { - return ['smtp', 'mail', 'log']; - }); - - $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 ($driver = $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: - if ($this->app->bound('mail.driver.'.$driver)) { - return $this->app->make('mail.driver.'.$driver); - } - - 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; - } -} diff --git a/framework/core/src/Mail/DriverInterface.php b/framework/core/src/Mail/DriverInterface.php new file mode 100644 index 000000000..2da9c2411 --- /dev/null +++ b/framework/core/src/Mail/DriverInterface.php @@ -0,0 +1,31 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Mail; + +use Flarum\Settings\SettingsRepositoryInterface; +use Swift_Transport; + +/** + * An interface for a mail service + * + * This interface provides all methods necessary for configuring, checking and + * using one of Laravel's various email drivers throughout Flarum. + * + * @public + */ +interface DriverInterface +{ + /** + * Build a mail transport based on Flarum's current settings + */ + public function buildTransport(SettingsRepositoryInterface $settings): Swift_Transport; +} diff --git a/framework/core/src/Mail/LogDriver.php b/framework/core/src/Mail/LogDriver.php new file mode 100644 index 000000000..4faa021fa --- /dev/null +++ b/framework/core/src/Mail/LogDriver.php @@ -0,0 +1,36 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Mail; + +use Flarum\Settings\SettingsRepositoryInterface; +use Illuminate\Contracts\Support\MessageBag as MessageBagContract; +use Illuminate\Mail\Transport\LogTransport; +use Psr\Log\LoggerInterface; +use Swift_Transport; + +class LogDriver implements DriverInterface +{ + /** + * @var LoggerInterface + */ + private $logger; + + public function __construct(LoggerInterface $logger) + { + $this->logger = $logger; + } + + public function buildTransport(SettingsRepositoryInterface $settings): Swift_Transport + { + return new LogTransport($this->logger); + } +} diff --git a/framework/core/src/Mail/MailServiceProvider.php b/framework/core/src/Mail/MailServiceProvider.php new file mode 100644 index 000000000..f9a0a1804 --- /dev/null +++ b/framework/core/src/Mail/MailServiceProvider.php @@ -0,0 +1,63 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Mail; + +use Flarum\Foundation\AbstractServiceProvider; +use Flarum\Settings\SettingsRepositoryInterface; +use Illuminate\Mail\Mailer; +use Swift_Mailer; + +class MailServiceProvider extends AbstractServiceProvider +{ + public function register() + { + $this->app->singleton('mail.supported_drivers', function () { + return [ + 'smtp' => SmtpDriver::class, + 'mail' => SendmailDriver::class, + 'log' => LogDriver::class, + ]; + }); + + $this->app->singleton('mail.driver', function () { + $settings = $this->app->make(SettingsRepositoryInterface::class); + $drivers = $this->app->make('mail.supported_drivers'); + + return $this->app->make($drivers[$settings->get('mail_driver')]); + }); + + $this->app->alias('mail.driver', DriverInterface::class); + + $this->app->singleton('swift.mailer', function ($app) { + return new Swift_Mailer( + $app->make('mail.driver')->buildTransport( + $app->make(SettingsRepositoryInterface::class) + ) + ); + }); + + $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; + }); + } +} diff --git a/framework/core/src/Mail/SendmailDriver.php b/framework/core/src/Mail/SendmailDriver.php new file mode 100644 index 000000000..502b0e369 --- /dev/null +++ b/framework/core/src/Mail/SendmailDriver.php @@ -0,0 +1,24 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Mail; + +use Flarum\Settings\SettingsRepositoryInterface; +use Swift_SendmailTransport; +use Swift_Transport; + +class SendmailDriver implements DriverInterface +{ + public function buildTransport(SettingsRepositoryInterface $settings): Swift_Transport + { + return new Swift_SendmailTransport; + } +} diff --git a/framework/core/src/Mail/SmtpDriver.php b/framework/core/src/Mail/SmtpDriver.php new file mode 100644 index 000000000..e2ed654ed --- /dev/null +++ b/framework/core/src/Mail/SmtpDriver.php @@ -0,0 +1,33 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Mail; + +use Flarum\Settings\SettingsRepositoryInterface; +use Swift_SmtpTransport; +use Swift_Transport; + +class SmtpDriver implements DriverInterface +{ + public function buildTransport(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; + } +}