mirror of
https://github.com/flarum/core.git
synced 2025-08-08 09:26:34 +02:00
Mail drivers: Separate definition from validation
This commit is contained in:
@@ -10,6 +10,8 @@
|
||||
namespace Flarum\Mail;
|
||||
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Illuminate\Contracts\Validation\Factory;
|
||||
use Illuminate\Support\MessageBag;
|
||||
use Swift_Transport;
|
||||
|
||||
/**
|
||||
@@ -31,6 +33,26 @@ interface DriverInterface
|
||||
*/
|
||||
public function availableSettings(): array;
|
||||
|
||||
/**
|
||||
* Ensure the given settings are enough to send emails.
|
||||
*
|
||||
* This method is responsible for determining whether the user-provided
|
||||
* values stored in Flarum's settings are "valid" as far as a simple
|
||||
* inspection of these values can determine it. Of course, this does not
|
||||
* mean that the mail server or API will actually accept e.g. credentials.
|
||||
*
|
||||
* Any errors must be wrapped in a {@see \Illuminate\Support\MessageBag}.
|
||||
* If there are no errors, an empty instance can be returned. In the
|
||||
* presence of validation problems with the configured mail driver, Flarum
|
||||
* will fall back to its no-op {@see \Flarum\Mail\NullDriver}.
|
||||
*/
|
||||
public function validate(SettingsRepositoryInterface $settings, Factory $validator): MessageBag;
|
||||
|
||||
/**
|
||||
* Does this driver actually send out emails?
|
||||
*/
|
||||
public function canSend(): bool;
|
||||
|
||||
/**
|
||||
* Build a mail transport based on Flarum's current settings.
|
||||
*/
|
||||
|
@@ -10,7 +10,9 @@
|
||||
namespace Flarum\Mail;
|
||||
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Illuminate\Contracts\Validation\Factory;
|
||||
use Illuminate\Mail\Transport\LogTransport;
|
||||
use Illuminate\Support\MessageBag;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Swift_Transport;
|
||||
|
||||
@@ -31,6 +33,16 @@ class LogDriver implements DriverInterface
|
||||
return [];
|
||||
}
|
||||
|
||||
public function validate(SettingsRepositoryInterface $settings, Factory $validator): MessageBag
|
||||
{
|
||||
return new MessageBag;
|
||||
}
|
||||
|
||||
public function canSend(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function buildTransport(SettingsRepositoryInterface $settings): Swift_Transport
|
||||
{
|
||||
return new LogTransport($this->logger);
|
||||
|
@@ -9,10 +9,11 @@
|
||||
|
||||
namespace Flarum\Mail;
|
||||
|
||||
use Flarum\Forum\ValidateMailConfiguration;
|
||||
use Flarum\Foundation\AbstractServiceProvider;
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Illuminate\Contracts\Validation\Factory;
|
||||
use Illuminate\Mail\Mailer;
|
||||
use Illuminate\Support\Arr;
|
||||
use Swift_Mailer;
|
||||
|
||||
class MailServiceProvider extends AbstractServiceProvider
|
||||
@@ -31,11 +32,29 @@ class MailServiceProvider extends AbstractServiceProvider
|
||||
});
|
||||
|
||||
$this->app->singleton('mail.driver', function () {
|
||||
return $this->app->make(ValidateMailConfiguration::class)->getWorkingDriver();
|
||||
$configured = $this->app->make('flarum.mail.configured_driver');
|
||||
$settings = $this->app->make(SettingsRepositoryInterface::class);
|
||||
$validator = $this->app->make(Factory::class);
|
||||
|
||||
return $configured->validate($settings, $validator)->any()
|
||||
? $this->app->make(NullDriver::class)
|
||||
: $configured;
|
||||
});
|
||||
|
||||
$this->app->alias('mail.driver', DriverInterface::class);
|
||||
|
||||
$this->app->singleton('flarum.mail.configured_driver', function () {
|
||||
$drivers = $this->app->make('mail.supported_drivers');
|
||||
$settings = $this->app->make(SettingsRepositoryInterface::class);
|
||||
$driverName = $settings->get('mail_driver');
|
||||
|
||||
$driverClass = Arr::get($drivers, $driverName);
|
||||
|
||||
return $driverClass
|
||||
? $this->app->make($driverClass)
|
||||
: $this->app->make(NullDriver::class);
|
||||
});
|
||||
|
||||
$this->app->singleton('swift.mailer', function ($app) {
|
||||
return new Swift_Mailer(
|
||||
$app->make('mail.driver')->buildTransport(
|
||||
|
@@ -11,7 +11,9 @@ namespace Flarum\Mail;
|
||||
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use GuzzleHttp\Client;
|
||||
use Illuminate\Contracts\Validation\Factory;
|
||||
use Illuminate\Mail\Transport\MailgunTransport;
|
||||
use Illuminate\Support\MessageBag;
|
||||
use Swift_Transport;
|
||||
|
||||
class MailgunDriver implements DriverInterface
|
||||
@@ -19,11 +21,29 @@ class MailgunDriver implements DriverInterface
|
||||
public function availableSettings(): array
|
||||
{
|
||||
return [
|
||||
'mail_mailgun_secret' => 'required', // the secret key
|
||||
'mail_mailgun_domain' => 'required|regex:/^(?!\-)(?:[a-zA-Z\d\-]{0,62}[a-zA-Z\d]\.){1,126}(?!\d+)[a-zA-Z\d]{1,63}$/', // the API base URL
|
||||
'mail_mailgun_secret' => '', // the secret key
|
||||
'mail_mailgun_domain' => '', // the API base URL
|
||||
'mail_mailgun_region' => [ // region's endpoint
|
||||
'api.mailgun.net' => 'US',
|
||||
'api.eu.mailgun.net' => 'EU',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function validate(SettingsRepositoryInterface $settings, Factory $validator): MessageBag
|
||||
{
|
||||
return $validator->make($settings->all(), [
|
||||
'mail_mailgun_secret' => 'required',
|
||||
'mail_mailgun_domain' => 'required|regex:/^(?!\-)(?:[a-zA-Z\d\-]{0,62}[a-zA-Z\d]\.){1,126}(?!\d+)[a-zA-Z\d]{1,63}$/',
|
||||
'mail_mailgun_region' => 'required|in:api.mailgun.net,api.eu.mailgun.net',
|
||||
])->errors();
|
||||
}
|
||||
|
||||
public function canSend(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function buildTransport(SettingsRepositoryInterface $settings): Swift_Transport
|
||||
{
|
||||
return new MailgunTransport(
|
||||
|
@@ -11,7 +11,9 @@ namespace Flarum\Mail;
|
||||
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use GuzzleHttp\Client;
|
||||
use Illuminate\Contracts\Validation\Factory;
|
||||
use Illuminate\Mail\Transport\MandrillTransport;
|
||||
use Illuminate\Support\MessageBag;
|
||||
use Swift_Transport;
|
||||
|
||||
class MandrillDriver implements DriverInterface
|
||||
@@ -19,10 +21,22 @@ class MandrillDriver implements DriverInterface
|
||||
public function availableSettings(): array
|
||||
{
|
||||
return [
|
||||
'mail_mandrill_secret' => 'required',
|
||||
'mail_mandrill_secret' => '',
|
||||
];
|
||||
}
|
||||
|
||||
public function validate(SettingsRepositoryInterface $settings, Factory $validator): MessageBag
|
||||
{
|
||||
return $validator->make($settings->all(), [
|
||||
'mail_mandrill_secret' => 'required',
|
||||
])->errors();
|
||||
}
|
||||
|
||||
public function canSend(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function buildTransport(SettingsRepositoryInterface $settings): Swift_Transport
|
||||
{
|
||||
return new MandrillTransport(
|
||||
|
@@ -10,6 +10,8 @@
|
||||
namespace Flarum\Mail;
|
||||
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Illuminate\Contracts\Validation\Factory;
|
||||
use Illuminate\Support\MessageBag;
|
||||
use Swift_NullTransport;
|
||||
use Swift_Transport;
|
||||
|
||||
@@ -20,6 +22,16 @@ class NullDriver implements DriverInterface
|
||||
return [];
|
||||
}
|
||||
|
||||
public function validate(SettingsRepositoryInterface $settings, Factory $validator): MessageBag
|
||||
{
|
||||
return new MessageBag;
|
||||
}
|
||||
|
||||
public function canSend(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function buildTransport(SettingsRepositoryInterface $settings): Swift_Transport
|
||||
{
|
||||
return new Swift_NullTransport();
|
||||
|
@@ -10,6 +10,8 @@
|
||||
namespace Flarum\Mail;
|
||||
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Illuminate\Contracts\Validation\Factory;
|
||||
use Illuminate\Support\MessageBag;
|
||||
use Swift_SendmailTransport;
|
||||
use Swift_Transport;
|
||||
|
||||
@@ -20,6 +22,16 @@ class SendmailDriver implements DriverInterface
|
||||
return [];
|
||||
}
|
||||
|
||||
public function validate(SettingsRepositoryInterface $settings, Factory $validator): MessageBag
|
||||
{
|
||||
return new MessageBag;
|
||||
}
|
||||
|
||||
public function canSend(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function buildTransport(SettingsRepositoryInterface $settings): Swift_Transport
|
||||
{
|
||||
return new Swift_SendmailTransport;
|
||||
|
@@ -11,7 +11,9 @@ namespace Flarum\Mail;
|
||||
|
||||
use Aws\Ses\SesClient;
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Illuminate\Contracts\Validation\Factory;
|
||||
use Illuminate\Mail\Transport\SesTransport;
|
||||
use Illuminate\Support\MessageBag;
|
||||
use Swift_Transport;
|
||||
|
||||
class SesDriver implements DriverInterface
|
||||
@@ -19,10 +21,24 @@ class SesDriver implements DriverInterface
|
||||
public function availableSettings(): array
|
||||
{
|
||||
return [
|
||||
'mail_ses_key' => '',
|
||||
'mail_ses_secret' => '',
|
||||
'mail_ses_region' => '',
|
||||
];
|
||||
}
|
||||
|
||||
public function validate(SettingsRepositoryInterface $settings, Factory $validator): MessageBag
|
||||
{
|
||||
return $validator->make($settings->all(), [
|
||||
'mail_ses_key' => 'required',
|
||||
'mail_ses_secret' => 'required',
|
||||
'mail_ses_region' => 'required',
|
||||
];
|
||||
])->errors();
|
||||
}
|
||||
|
||||
public function canSend(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function buildTransport(SettingsRepositoryInterface $settings): Swift_Transport
|
||||
|
@@ -10,6 +10,8 @@
|
||||
namespace Flarum\Mail;
|
||||
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Illuminate\Contracts\Validation\Factory;
|
||||
use Illuminate\Support\MessageBag;
|
||||
use Swift_SmtpTransport;
|
||||
use Swift_Transport;
|
||||
|
||||
@@ -18,12 +20,28 @@ class SmtpDriver implements DriverInterface
|
||||
public function availableSettings(): array
|
||||
{
|
||||
return [
|
||||
'mail_host' => 'required', // a hostname, IPv4 address or IPv6 wrapped in []
|
||||
'mail_port' => 'nullable|integer', // a number, defaults to 25
|
||||
'mail_encryption' => 'nullable|in:tls,ssl', // "tls" or "ssl"
|
||||
'mail_host' => '', // a hostname, IPv4 address or IPv6 wrapped in []
|
||||
'mail_port' => '', // a number, defaults to 25
|
||||
'mail_encryption' => '', // "tls" or "ssl"
|
||||
'mail_username' => '',
|
||||
'mail_password' => '',
|
||||
];
|
||||
}
|
||||
|
||||
public function validate(SettingsRepositoryInterface $settings, Factory $validator): MessageBag
|
||||
{
|
||||
return $validator->make($settings->all(), [
|
||||
'mail_host' => 'required',
|
||||
'mail_port' => 'nullable|integer',
|
||||
'mail_encryption' => 'nullable|in:tls,ssl',
|
||||
'mail_username' => 'required',
|
||||
'mail_password' => 'required',
|
||||
];
|
||||
])->errors();
|
||||
}
|
||||
|
||||
public function canSend(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function buildTransport(SettingsRepositoryInterface $settings): Swift_Transport
|
||||
|
Reference in New Issue
Block a user