1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByJG\Mail\Wrapper; |
4
|
|
|
|
5
|
|
|
use ByJG\Convert\FromUTF8; |
6
|
|
|
use ByJG\Mail\Envelope; |
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
|
|
|
* |
24
|
|
|
* @param Envelope $envelope |
25
|
|
|
* @return PHPMailerOverride |
26
|
|
|
*/ |
27
|
8 |
|
protected function prepareMailer(Envelope $envelope) |
28
|
|
|
{ |
29
|
8 |
|
$mail = $this->getMailer(); |
30
|
8 |
|
$mail->Subject = FromUTF8::toIso88591Email($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
|
8 |
|
} |
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
|
4 |
|
} |
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
|
8 |
|
} |
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
|
8 |
|
} |
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
|
8 |
|
} |
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
|
2 |
|
$name, |
76
|
2 |
|
'base64', |
77
|
2 |
|
$value['content-type'], |
78
|
|
|
'attachment' |
79
|
2 |
|
); |
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
|
4 |
|
} |
89
|
8 |
|
} |
90
|
|
|
|
91
|
8 |
|
return $mail; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param Envelope $envelope |
96
|
|
|
* @return bool |
97
|
|
|
* @throws \ByJG\Mail\Exception\MailApiException |
98
|
|
|
*/ |
99
|
4 |
|
public function send(Envelope $envelope) |
100
|
|
|
{ |
101
|
4 |
|
$this->validate($envelope); |
102
|
|
|
|
103
|
4 |
|
$mail = $this->prepareMailer($envelope); |
104
|
|
|
|
105
|
4 |
|
$mail->Host = $this->uri->getHost(); |
106
|
4 |
|
$mail->Port = $this->uri->getPort(); |
|
|
|
|
107
|
|
|
|
108
|
4 |
|
if (!empty($this->uri->getUsername())) { |
109
|
4 |
|
$mail->SMTPAuth = true; |
110
|
4 |
|
$mail->Username = $this->uri->getUsername(); // SMTP account username |
111
|
4 |
|
$mail->Password = $this->uri->getPassword(); // SMTP account password |
112
|
4 |
|
} |
113
|
|
|
|
114
|
4 |
|
if (!$mail->send()) { |
115
|
|
|
throw new MailApiException($mail->ErrorInfo); |
116
|
|
|
} |
117
|
|
|
|
118
|
4 |
|
return true; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.