1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByJG\Mail\Wrapper; |
4
|
|
|
|
5
|
|
|
use ByJG\Mail\Envelope; |
6
|
|
|
use ByJG\Mail\Exception\InvalidEMailException; |
7
|
|
|
use ByJG\Mail\Exception\MailApiException; |
8
|
|
|
use ByJG\Mail\Override\PHPMailerOverride; |
9
|
|
|
use ByJG\Mail\Util; |
10
|
|
|
|
11
|
|
|
class PHPMailerWrapper extends BaseWrapper |
12
|
|
|
{ |
13
|
3 |
|
public static function schema() |
14
|
|
|
{ |
15
|
3 |
|
return ['smtp', 'tls', 'ssl']; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @return \ByJG\Mail\Override\PHPMailerOverride |
20
|
|
|
*/ |
21
|
4 |
|
public function getMailer() |
22
|
|
|
{ |
23
|
|
|
// the true param means it will throw exceptions on errors, which we need to catch |
24
|
4 |
|
return new PHPMailerOverride(true); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param Envelope $envelope |
29
|
|
|
* @return PHPMailerOverride |
30
|
|
|
* @throws \PHPMailer\PHPMailer\Exception |
31
|
|
|
*/ |
32
|
8 |
|
protected function prepareMailer(Envelope $envelope) |
33
|
|
|
{ |
34
|
8 |
|
$mail = $this->getMailer(); |
35
|
8 |
|
$mail->Subject = $envelope->getSubject(); |
36
|
8 |
|
$mail->CharSet = "utf-8"; |
37
|
8 |
|
$mail->Body = $envelope->getBody(); |
38
|
8 |
|
if ($envelope->isHtml()) { |
39
|
8 |
|
$mail->msgHTML($envelope->getBody()); |
40
|
8 |
|
$mail->AltBody = $envelope->getBodyText(); |
41
|
|
|
} |
42
|
|
|
|
43
|
8 |
|
$mail->isSMTP(); // telling the class to use SMTP |
44
|
|
|
|
45
|
8 |
|
if ($this->uri->getScheme() != "smtp") { |
46
|
4 |
|
$mail->SMTPSecure = $this->uri->getScheme(); // ssl ou tls! |
47
|
|
|
} |
48
|
|
|
|
49
|
8 |
|
$replyTo = Util::decomposeEmail($envelope->getReplyTo()); |
50
|
8 |
|
$mail->addReplyTo($replyTo["email"], $replyTo["name"]); |
51
|
|
|
|
52
|
|
|
// Define From email |
53
|
8 |
|
$from = Util::decomposeEmail($envelope->getFrom()); |
54
|
8 |
|
$mail->setFrom($from["email"], $from["name"]); |
55
|
|
|
|
56
|
|
|
// Add Recipients |
57
|
8 |
|
foreach ((array)$envelope->getTo() as $toItem) { |
58
|
8 |
|
$to = Util::decomposeEmail($toItem); |
59
|
8 |
|
$mail->addAddress($to["email"], $to["name"]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// Add Carbon Copy |
63
|
8 |
|
foreach ((array)$envelope->getCC() as $ccItem) { |
64
|
6 |
|
$cc = Util::decomposeEmail($ccItem); |
65
|
6 |
|
$mail->addCC($cc["email"], $cc["name"]); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// Add Blind Carbon Copy |
69
|
8 |
|
foreach ((array)$envelope->getBCC() as $bccItem) { |
70
|
6 |
|
$bcc = Util::decomposeEmail($bccItem); |
71
|
6 |
|
$mail->addCustomHeader("Bcc: " . $bcc["email"]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// Attachments |
75
|
8 |
|
foreach ((array)$envelope->getAttachments() as $name => $value) { |
76
|
4 |
|
switch ($value['disposition']) { |
77
|
4 |
|
case 'attachment': |
78
|
2 |
|
$mail->addAttachment( |
79
|
2 |
|
$value['content'], |
80
|
2 |
|
$name, |
81
|
2 |
|
'base64', |
82
|
2 |
|
$value['content-type'], |
83
|
2 |
|
'attachment' |
84
|
2 |
|
); |
85
|
2 |
|
break; |
86
|
|
|
|
87
|
2 |
|
case 'inline': |
88
|
2 |
|
$mail->addEmbeddedImage($value['content'], $name, $name, 'base64', $value['content-type']); |
89
|
2 |
|
break; |
90
|
|
|
|
91
|
|
|
default: |
92
|
|
|
throw new \InvalidArgumentException('Invalid attachment type'); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
8 |
|
return $mail; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param Envelope $envelope |
101
|
|
|
* @return bool |
102
|
|
|
* @throws MailApiException |
103
|
|
|
* @throws InvalidEMailException |
104
|
|
|
* @throws \PHPMailer\PHPMailer\Exception |
105
|
|
|
*/ |
106
|
4 |
|
public function send(Envelope $envelope) |
107
|
|
|
{ |
108
|
4 |
|
$this->validate($envelope); |
109
|
|
|
|
110
|
4 |
|
$mail = $this->prepareMailer($envelope); |
111
|
|
|
|
112
|
4 |
|
$mail->Host = $this->uri->getHost(); |
113
|
4 |
|
$mail->Port = $this->uri->getPort(); |
114
|
|
|
|
115
|
4 |
|
if (!empty($this->uri->getUsername())) { |
116
|
4 |
|
$mail->SMTPAuth = true; |
117
|
4 |
|
$mail->Username = $this->uri->getUsername(); // SMTP account username |
118
|
4 |
|
$mail->Password = $this->uri->getPassword(); // SMTP account password |
119
|
|
|
} |
120
|
|
|
|
121
|
4 |
|
if (!$mail->send()) { |
122
|
|
|
throw new MailApiException($mail->ErrorInfo); |
123
|
|
|
} |
124
|
|
|
|
125
|
4 |
|
return true; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|