|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Fabrica\Helper; |
|
4
|
|
|
|
|
5
|
|
|
use Fabrica\Tools\Config; |
|
6
|
|
|
use Fabrica\Tools\Builder; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Helper class for sending emails using email configuration. |
|
10
|
|
|
*/ |
|
11
|
|
|
class Email |
|
12
|
|
|
{ |
|
13
|
|
|
const DEFAULT_FROM = 'PHP Censor <[email protected]>'; |
|
14
|
|
|
|
|
15
|
|
|
protected $emailTo = []; |
|
16
|
|
|
protected $emailCc = []; |
|
17
|
|
|
protected $subject = 'Email from PHP Censor'; |
|
18
|
|
|
protected $body = ''; |
|
19
|
|
|
protected $isHtml = false; |
|
20
|
|
|
protected $config; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param Config $config |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(Config $config) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->config = $config; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Set the email's To: header. |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $email |
|
34
|
|
|
* @param string|null $name |
|
35
|
|
|
* @return $this |
|
36
|
|
|
*/ |
|
37
|
|
|
public function setEmailTo($email, $name = null) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->emailTo[$email] = $name; |
|
40
|
|
|
|
|
41
|
|
|
return $this; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Add an address to the email's CC header. |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $email |
|
48
|
|
|
* @param string|null $name |
|
49
|
|
|
* @return $this |
|
50
|
|
|
*/ |
|
51
|
|
|
public function addCc($email, $name = null) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->emailCc[$email] = $name; |
|
54
|
|
|
|
|
55
|
|
|
return $this; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Set the email subject. |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $subject |
|
62
|
|
|
* @return $this |
|
63
|
|
|
*/ |
|
64
|
|
|
public function setSubject($subject) |
|
65
|
|
|
{ |
|
66
|
|
|
$this->subject = $subject; |
|
67
|
|
|
|
|
68
|
|
|
return $this; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Set the email body. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $body |
|
75
|
|
|
* @return $this |
|
76
|
|
|
*/ |
|
77
|
|
|
public function setBody($body) |
|
78
|
|
|
{ |
|
79
|
|
|
$this->body = $body; |
|
80
|
|
|
|
|
81
|
|
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Set whether or not the email body is HTML. |
|
86
|
|
|
* |
|
87
|
|
|
* @param bool $isHtml |
|
88
|
|
|
* @return $this |
|
89
|
|
|
*/ |
|
90
|
|
|
public function setHtml($isHtml = false) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->isHtml = $isHtml; |
|
93
|
|
|
|
|
94
|
|
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Get the from address to use for the email. |
|
99
|
|
|
* |
|
100
|
|
|
* @return array |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getFrom() |
|
103
|
|
|
{ |
|
104
|
|
|
$from = $this->config->get( |
|
105
|
|
|
'php-censor.email_settings.from_address', |
|
106
|
|
|
self::DEFAULT_FROM |
|
107
|
|
|
); |
|
108
|
|
|
|
|
109
|
|
|
if (strpos($from, '<') === false) { |
|
110
|
|
|
return [(string)trim($from) => 'PHP Censor']; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
preg_match('#^(.*?)<(.*?)>$#ui', $from, $fromParts); |
|
114
|
|
|
|
|
115
|
|
|
return [trim($fromParts[2]) => trim($fromParts[1])]; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Send the email. |
|
120
|
|
|
* |
|
121
|
|
|
* @param Builder $builder |
|
122
|
|
|
* |
|
123
|
|
|
* @return int |
|
124
|
|
|
*/ |
|
125
|
|
|
public function send(Builder $builder = null) |
|
126
|
|
|
{ |
|
127
|
|
|
$smtpServer = $this->config->get('php-censor.email_settings.smtp_address'); |
|
128
|
|
|
if (null !== $builder) { |
|
129
|
|
|
$builder->logDebug(sprintf("SMTP: '%s'", !empty($smtpServer) ? 'true' : 'false')); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
$factory = new MailerFactory($this->config->get('php-censor')); |
|
133
|
|
|
$mailer = $factory->getSwiftMailerFromConfig(); |
|
134
|
|
|
|
|
135
|
|
|
$message = \Swift_Message::newInstance($this->subject) |
|
|
|
|
|
|
136
|
|
|
->setFrom($this->getFrom()) |
|
137
|
|
|
->setTo($this->emailTo) |
|
138
|
|
|
->setBody($this->body); |
|
139
|
|
|
|
|
140
|
|
|
if ($this->isHtml) { |
|
141
|
|
|
$message->setContentType('text/html'); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
if (is_array($this->emailCc) && count($this->emailCc)) { |
|
145
|
|
|
$message->setCc($this->emailCc); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
ob_start(); |
|
149
|
|
|
|
|
150
|
|
|
$result = $mailer->send($message); |
|
151
|
|
|
|
|
152
|
|
|
$rawOutput = ob_get_clean(); |
|
153
|
|
|
|
|
154
|
|
|
if ($rawOutput) { |
|
155
|
|
|
$builder->getBuildLogger()->logWarning($rawOutput); |
|
|
|
|
|
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return $result; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.