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