-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathEmail.php
More file actions
158 lines (129 loc) · 3.41 KB
/
Copy pathEmail.php
File metadata and controls
158 lines (129 loc) · 3.41 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
namespace PHPCensor\Helper;
use PHPCensor\Builder;
use PHPCensor\Common\Application\ConfigurationInterface;
use Swift_Message;
/**
* Helper class for sending emails using email configuration.
*
* @package PHP Censor
* @subpackage Application
*
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
*/
class Email
{
public const DEFAULT_FROM = 'PHP Censor <no-reply@php-censor.local>';
protected $emailTo = [];
protected $emailCc = [];
protected $subject = 'Email from PHP Censor';
protected $body = '';
protected $isHtml = false;
protected $config;
public function __construct(ConfigurationInterface $config)
{
$this->config = $config;
}
/**
* Set the email's To: header.
* @param string $email
* @param string|null $name
* @return $this
*/
public function setEmailTo($email, $name = null)
{
$this->emailTo[$email] = $name;
return $this;
}
/**
* Add an address to the email's CC header.
* @param string $email
* @param string|null $name
* @return $this
*/
public function addCc($email, $name = null)
{
$this->emailCc[$email] = $name;
return $this;
}
/**
* Set the email subject.
* @param string $subject
* @return $this
*/
public function setSubject($subject)
{
$this->subject = $subject;
return $this;
}
/**
* Set the email body.
* @param string $body
* @return $this
*/
public function setBody($body)
{
$this->body = $body;
return $this;
}
/**
* Set whether or not the email body is HTML.
* @param bool $isHtml
* @return $this
*/
public function setHtml($isHtml = false)
{
$this->isHtml = $isHtml;
return $this;
}
/**
* Get the from address to use for the email.
*
* @return array
*/
public function getFrom()
{
$from = $this->config->get(
'php-censor.email_settings.from_address',
self::DEFAULT_FROM
);
if (\strpos($from, '<') === false) {
return [(string)\trim($from) => 'PHP Censor'];
}
\preg_match('#^(.*?)<(.*?)>$#ui', $from, $fromParts);
return [\trim($fromParts[2]) => \trim($fromParts[1])];
}
/**
* Send the email.
*
*
* @return int
*/
public function send(Builder $builder = null)
{
$smtpServer = $this->config->get('php-censor.email_settings.smtp_address');
if (null !== $builder) {
$builder->logDebug(\sprintf("SMTP: '%s'", !empty($smtpServer) ? 'true' : 'false'));
}
$factory = new MailerFactory($this->config->get('php-censor'));
$mailer = $factory->getSwiftMailerFromConfig();
$message = new Swift_Message($this->subject);
$message
->setFrom($this->getFrom())
->setTo($this->emailTo)
->setBody($this->body);
if ($this->isHtml) {
$message->setContentType('text/html');
}
if (\is_array($this->emailCc) && \count($this->emailCc)) {
$message->setCc($this->emailCc);
}
\ob_start();
$result = $mailer->send($message);
$rawOutput = \ob_get_clean();
if ($rawOutput) {
$builder->getBuildLogger()->logWarning($rawOutput);
}
return $result;
}
}