|
1
|
|
|
<?php |
|
2
|
|
|
namespace PlaygroundCore\Mail\Service; |
|
3
|
|
|
|
|
4
|
|
|
use Zend\ServiceManager\ServiceManager; |
|
5
|
|
|
use Zend\Mail\Message as MailMessage; |
|
6
|
|
|
use Zend\Mime\Message as MimeMessage; |
|
7
|
|
|
use Zend\Mime\Mime; |
|
8
|
|
|
use Zend\Mime\Part; |
|
9
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
|
10
|
|
|
|
|
11
|
|
|
class Message |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* |
|
15
|
|
|
* @var ServiceManager |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $serviceLocator; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct(ServiceLocatorInterface $locator) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->serviceLocator = $locator; |
|
|
|
|
|
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* |
|
26
|
|
|
* @var \Zend\View\Renderer\RendererInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $renderer; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* |
|
32
|
|
|
* @var \Zend\Mail\Transport\TransportInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $transport; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Return a HTML message ready to be sent |
|
38
|
|
|
* |
|
39
|
|
|
* @param array|string $from |
|
40
|
|
|
* A string containing the sender e-mail address, or if array with keys email and name |
|
41
|
|
|
* @param array|string $to |
|
42
|
|
|
* An array containing the recipients of the mail |
|
43
|
|
|
* @param string $subject |
|
44
|
|
|
* Subject of the mail |
|
45
|
|
|
* @param string|\Zend\View\Model\ModelInterface $nameOrModel |
|
46
|
|
|
* Either the template to use, or a ViewModel |
|
47
|
|
|
* @param null|array $values |
|
48
|
|
|
* Values to use when the template is rendered |
|
49
|
|
|
* @return Message |
|
50
|
|
|
*/ |
|
51
|
|
|
public function createHtmlMessage($from, $to, $subject, $nameOrModel, $values = array()) |
|
52
|
|
|
{ |
|
53
|
|
|
if (is_string($from)) { |
|
54
|
|
|
$from = array('email' => $from, 'name' => $from); |
|
55
|
|
|
} |
|
56
|
|
|
$renderer = $this->getRenderer(); |
|
57
|
|
|
$content = $renderer->render($nameOrModel, $values); |
|
58
|
|
|
$resolver = $this->serviceLocator->get('Zend\View\Resolver\TemplatePathStack'); |
|
59
|
|
|
// check if plain text email template exist |
|
60
|
|
|
if ($resolver->resolve($nameOrModel . '-plain')) { |
|
61
|
|
|
$contentText = $renderer->render($nameOrModel . '-plain', $values); |
|
62
|
|
|
} else { |
|
63
|
|
|
$contentText = ''; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$mail = new MailMessage(); |
|
67
|
|
|
$mail->addTo($to); |
|
68
|
|
|
$mail->addFrom($from['email'], $from['name']); |
|
69
|
|
|
$mail->setSubject($subject); |
|
70
|
|
|
$text = new Part($contentText); |
|
71
|
|
|
$text->type = Mime::TYPE_TEXT; |
|
72
|
|
|
$text->encoding = Mime::ENCODING_QUOTEDPRINTABLE; |
|
73
|
|
|
$text->disposition = Mime::DISPOSITION_INLINE; |
|
74
|
|
|
$text->charset = 'UTF-8'; |
|
75
|
|
|
$html = new Part($content); |
|
76
|
|
|
$html->type = Mime::TYPE_HTML; |
|
77
|
|
|
$html->encoding = Mime::ENCODING_QUOTEDPRINTABLE; |
|
78
|
|
|
$html->disposition = Mime::DISPOSITION_INLINE; |
|
79
|
|
|
$html->charset = 'UTF-8'; |
|
80
|
|
|
$bodyMessage = new MimeMessage(); |
|
81
|
|
|
|
|
82
|
|
|
$multiPartContentMessage = new MimeMessage(); |
|
83
|
|
|
$multiPartContentMessage->addPart($text); |
|
84
|
|
|
$multiPartContentMessage->addPart($html); |
|
85
|
|
|
$multiPartContentMimePart = new Part($multiPartContentMessage->generateMessage()); |
|
86
|
|
|
$multiPartContentMimePart->charset = 'UTF-8'; |
|
87
|
|
|
$multiPartContentMimePart->type = 'multipart/alternative'; |
|
88
|
|
|
$multiPartContentMimePart->boundary = $multiPartContentMessage->getMime()->boundary(); |
|
89
|
|
|
$bodyMessage->addPart($multiPartContentMimePart); |
|
90
|
|
|
$mail->setBody($bodyMessage); |
|
91
|
|
|
$mail->setEncoding('UTF-8'); |
|
92
|
|
|
|
|
93
|
|
|
return $mail; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Return a text message ready to be sent |
|
98
|
|
|
* |
|
99
|
|
|
* @param array|string $from |
|
100
|
|
|
* A string containing the sender e-mail address, or if array with keys email and name |
|
101
|
|
|
* @param array|string $to |
|
102
|
|
|
* An array containing the recipients of the mail |
|
103
|
|
|
* @param string $subject |
|
104
|
|
|
* Subject of the mail |
|
105
|
|
|
* @param string|\Zend\View\Model\ModelInterface $nameOrModel |
|
106
|
|
|
* Either the template to use, or a ViewModel |
|
107
|
|
|
* @param null|array $values |
|
108
|
|
|
* Values to use when the template is rendered |
|
109
|
|
|
* @return Message |
|
110
|
|
|
*/ |
|
111
|
|
|
public function createTextMessage($from, $to, $subject, $nameOrModel, $values = array()) |
|
112
|
|
|
{ |
|
113
|
|
|
$renderer = $this->getRenderer(); |
|
114
|
|
|
$content = $renderer->render($nameOrModel, $values); |
|
115
|
|
|
|
|
116
|
|
|
return $this->getDefaultMessage($from, 'utf-8', $to, $subject, $content); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Send the message |
|
121
|
|
|
* |
|
122
|
|
|
* @param Message $message |
|
123
|
|
|
*/ |
|
124
|
|
|
public function send(MailMessage $message) |
|
125
|
|
|
{ |
|
126
|
|
|
$this->getTransport() |
|
127
|
|
|
->send($message); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Get the renderer |
|
132
|
|
|
* |
|
133
|
|
|
* @return \Zend\View\Renderer\RendererInterface |
|
134
|
|
|
*/ |
|
135
|
|
|
protected function getRenderer() |
|
136
|
|
|
{ |
|
137
|
|
|
if ($this->renderer === null) { |
|
138
|
|
|
$this->renderer = $this->serviceLocator->get('ViewRenderer'); |
|
|
|
|
|
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return $this->renderer; |
|
|
|
|
|
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Get the transport |
|
146
|
|
|
* |
|
147
|
|
|
* @return \Zend\Mail\Transport\TransportInterface |
|
148
|
|
|
*/ |
|
149
|
|
|
protected function getTransport() |
|
150
|
|
|
{ |
|
151
|
|
|
if ($this->transport === null) { |
|
152
|
|
|
$this->transport = $this->serviceLocator->get('playgroundcore_transport'); |
|
|
|
|
|
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
return $this->transport; |
|
|
|
|
|
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* |
|
160
|
|
|
* @return Message |
|
161
|
|
|
*/ |
|
162
|
|
|
protected function getDefaultMessage($from, $encoding, $to, $subject, $body) |
|
163
|
|
|
{ |
|
164
|
|
|
if (is_string($from)) { |
|
165
|
|
|
$from = array('email' => $from, 'name' => $from); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
$message = new MailMessage(); |
|
169
|
|
|
$message->setFrom($from['email'], $from['name']) |
|
170
|
|
|
->setEncoding($encoding) |
|
171
|
|
|
->setSubject($subject) |
|
172
|
|
|
->setBody($body) |
|
173
|
|
|
->setTo($to); |
|
174
|
|
|
|
|
175
|
|
|
$message->getHeaders()->get('content-type')->setType('multipart/alternative'); |
|
176
|
|
|
|
|
177
|
|
|
return $message; |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
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 given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.