/
SmtpDriver.php
60 lines (51 loc) · 1.69 KB
/
SmtpDriver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
/*
* This file is part of Flarum.
*
* For detailed 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\Validation\Factory;
use Illuminate\Support\MessageBag;
use Swift_SmtpTransport;
use Swift_Transport;
class SmtpDriver implements DriverInterface
{
public function availableSettings(): array
{
return [
'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,TLS,SSL',
'mail_username' => 'nullable|string',
'mail_password' => 'nullable|string',
])->errors();
}
public function canSend(): bool
{
return true;
}
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;
}
}