-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSmtpMailAdapter.php
82 lines (61 loc) · 2.18 KB
/
SmtpMailAdapter.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
declare(strict_types=1);
namespace Sendportal\Base\Adapters;
use Swift_Mailer;
use Swift_Message;
use Swift_SmtpTransport;
use Swift_TransportException;
use Illuminate\Support\Arr;
use Sendportal\Base\Services\Messages\MessageTrackingOptions;
class SmtpMailAdapter extends BaseMailAdapter
{
/** @var Mailer */
protected $client;
/** @var SmtpTransport */
protected $transport;
public function send(string $fromEmail, string $fromName, string $toEmail, string $subject, MessageTrackingOptions $trackingOptions, string $content): string
{
$failedRecipients = [];
try{
$result = $this->resolveClient()->send($this->resolveMessage( $subject, $content, $fromEmail, $fromName, $toEmail), $failedRecipients);
}
catch(Swift_TransportException $e){
return $this->resolveMessageId(0);
}
return $this->resolveMessageId($result);
}
protected function resolveClient(): Swift_Mailer
{
if ($this->client) {
return $this->client;
}
$this->client = new Swift_Mailer($this->resolveTransport());
return $this->client;
}
protected function resolveTransport(): Swift_SmtpTransport
{
if ($this->transport) {
return $this->transport;
}
$this->transport = new Swift_SmtpTransport(
Arr::get($this->config, 'domain'),
Arr::get($this->config, 'port'),
Arr::get($this->config, 'encryption')
);
$this->transport->setUsername(Arr::get($this->config, 'username'));
$this->transport->setPassword(Arr::get($this->config, 'password'));
$this->transport->setAuthMode('login');
return $this->transport;
}
protected function resolveMessage(string $subject, string $content,string $fromEmail, string $fromName, string $toEmail): Swift_Message
{
$msg = new Swift_Message($subject,$content,'text/html');
$msg->setTo($toEmail);
$msg->setFrom($fromEmail);
return $msg;
}
protected function resolveMessageId($result): string
{
return ($result == 1) ? strval($result) : '-1';
}
}