-
-
Notifications
You must be signed in to change notification settings - Fork 835
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a custom service provider for email configuration
- Loading branch information
1 parent
9794a08
commit ac5e26a
Showing
2 changed files
with
79 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Flarum. | ||
* | ||
* (c) Toby Zerner <toby.zerner@gmail.com> | ||
* | ||
* 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; | ||
} | ||
} |
ac5e26a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@franzliedke I noticed an issue with this commit. As an extension you are able to add another driver (eg mailgun) using the resolving callback. But whenever the transport is being instantiated the Exception further down in this service provider is always triggered. Because there's no way to hook into it, I'm not sure this is the best solution.
What do you propose is a better solution? Would it make sense to have an event dispatched, listeners created per transport and when this returns nothing throw an exception?
ac5e26a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a stopgap, something like this in the
default
case might work:In the short-term, I'd rather see a more thorough abstraction for drivers that can handle the use-cases outlined here.