1 | <?php |
||
2 | |||
3 | /** |
||
4 | * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
||
5 | * @copyright Aimeos (aimeos.org), 2014-2018 |
||
6 | * @package MW |
||
7 | * @subpackage Mail |
||
8 | */ |
||
9 | |||
10 | |||
11 | namespace Aimeos\MW\Mail\Message; |
||
12 | |||
13 | |||
14 | /** |
||
15 | * Zend implementation for creating e-mails. |
||
16 | * |
||
17 | * @package MW |
||
18 | * @subpackage Mail |
||
19 | */ |
||
20 | class Zend2 implements \Aimeos\MW\Mail\Message\Iface |
||
21 | { |
||
22 | private $html; |
||
23 | private $text; |
||
24 | private $object; |
||
25 | private $charset; |
||
26 | private $attach = []; |
||
27 | private $embedded = []; |
||
28 | |||
29 | |||
30 | /** |
||
31 | * Initializes the message instance. |
||
32 | * |
||
33 | * @param \Zend\Mail\Message $object Zend mail object |
||
34 | */ |
||
35 | public function __construct( \Zend\Mail\Message $object, $charset = 'UTF-8' ) |
||
36 | { |
||
37 | $this->object = $object; |
||
38 | } |
||
39 | |||
40 | |||
41 | /** |
||
42 | * Adds a source e-mail address of the message. |
||
43 | * |
||
44 | * @param string $email Source e-mail address |
||
45 | * @param string|null $name Name of the user sending the e-mail or null for no name |
||
46 | * @return \Aimeos\MW\Mail\Message\Iface Message object |
||
47 | */ |
||
48 | public function addFrom( $email, $name = null ) |
||
49 | { |
||
50 | $this->object->setFrom( $email, $name ); |
||
51 | return $this; |
||
52 | } |
||
53 | |||
54 | |||
55 | /** |
||
56 | * Adds a destination e-mail address of the target user mailbox. |
||
57 | * |
||
58 | * @param string $email Destination address of the target mailbox |
||
59 | * @param string|null $name Name of the user owning the target mailbox or null for no name |
||
60 | * @return \Aimeos\MW\Mail\Message\Iface Message object |
||
61 | */ |
||
62 | public function addTo( $email, $name = null ) |
||
63 | { |
||
64 | $this->object->addTo( $email, $name ); |
||
65 | return $this; |
||
66 | } |
||
67 | |||
68 | |||
69 | /** |
||
70 | * Adds a destination e-mail address for a copy of the message. |
||
71 | * |
||
72 | * @param string $email Destination address for a copy |
||
73 | * @param string|null $name Name of the user owning the target mailbox or null for no name |
||
74 | * @return \Aimeos\MW\Mail\Message\Iface Message object |
||
75 | */ |
||
76 | public function addCc( $email, $name = null ) |
||
77 | { |
||
78 | $this->object->addCc( $email, $name ); |
||
79 | return $this; |
||
80 | } |
||
81 | |||
82 | |||
83 | /** |
||
84 | * Adds a destination e-mail address for a hidden copy of the message. |
||
85 | * |
||
86 | * @param string $email Destination address for a hidden copy |
||
87 | * @param string|null $name Name of the user owning the target mailbox or null for no name |
||
88 | * @return \Aimeos\MW\Mail\Message\Iface Message object |
||
89 | */ |
||
90 | public function addBcc( $email, $name = null ) |
||
91 | { |
||
92 | $this->object->addBcc( $email, $name ); |
||
93 | return $this; |
||
94 | } |
||
95 | |||
96 | |||
97 | /** |
||
98 | * Adds the return e-mail address for the message. |
||
99 | * |
||
100 | * @param string $email E-mail address which should receive all replies |
||
101 | * @param string|null $name Name of the user which should receive all replies or null for no name |
||
102 | * @return \Aimeos\MW\Mail\Message\Iface Message object |
||
103 | */ |
||
104 | public function addReplyTo( $email, $name = null ) |
||
105 | { |
||
106 | $this->object->setReplyTo( $email, $name ); |
||
107 | return $this; |
||
108 | } |
||
109 | |||
110 | |||
111 | /** |
||
112 | * Adds a custom header to the message. |
||
113 | * |
||
114 | * @param string $name Name of the custom e-mail header |
||
115 | * @param string $value Text content of the custom e-mail header |
||
116 | * @return \Aimeos\MW\Mail\Message\Iface Message object |
||
117 | */ |
||
118 | public function addHeader( $name, $value ) |
||
119 | { |
||
120 | $this->object->getHeaders()->addHeaderLine( $name, $value ); |
||
121 | return $this; |
||
122 | } |
||
123 | |||
124 | |||
125 | /** |
||
126 | * Sets the e-mail address and name of the sender of the message (higher precedence than "From"). |
||
127 | * |
||
128 | * @param string $email Source e-mail address |
||
129 | * @param string|null $name Name of the user who sent the message or null for no name |
||
130 | * @return \Aimeos\MW\Mail\Message\Iface Message object |
||
131 | */ |
||
132 | public function setSender( $email, $name = null ) |
||
133 | { |
||
134 | $this->object->setFrom( $email, $name ); |
||
135 | return $this; |
||
136 | } |
||
137 | |||
138 | |||
139 | /** |
||
140 | * Sets the subject of the message. |
||
141 | * |
||
142 | * @param string $subject Subject of the message |
||
143 | * @return \Aimeos\MW\Mail\Message\Iface Message object |
||
144 | */ |
||
145 | public function setSubject( $subject ) |
||
146 | { |
||
147 | $this->object->setSubject( $subject ); |
||
148 | return $this; |
||
149 | } |
||
150 | |||
151 | |||
152 | /** |
||
153 | * Sets the text body of the message. |
||
154 | * |
||
155 | * @param string $message Text body of the message |
||
156 | * @return \Aimeos\MW\Mail\Message\Iface Message object |
||
157 | */ |
||
158 | public function setBody( $message ) |
||
159 | { |
||
160 | $part = new \Zend\Mime\Part( $message ); |
||
161 | |||
162 | $part->charset = $this->charset; |
||
163 | $part->encoding = \Zend\Mime\Mime::ENCODING_QUOTEDPRINTABLE; |
||
164 | $part->type = \Zend\Mime\Mime::TYPE_TEXT; |
||
165 | |||
166 | $this->text = $part; |
||
167 | return $this; |
||
168 | } |
||
169 | |||
170 | |||
171 | /** |
||
172 | * Sets the HTML body of the message. |
||
173 | * |
||
174 | * @param string $message HTML body of the message |
||
175 | * @return \Aimeos\MW\Mail\Message\Iface Message object |
||
176 | */ |
||
177 | public function setBodyHtml( $message ) |
||
178 | { |
||
179 | $part = new \Zend\Mime\Part( $message ); |
||
180 | |||
181 | $part->charset = $this->charset; |
||
182 | $part->encoding = \Zend\Mime\Mime::ENCODING_QUOTEDPRINTABLE; |
||
183 | $part->disposition = \Zend\Mime\Mime::DISPOSITION_INLINE; |
||
184 | $part->type = \Zend\Mime\Mime::TYPE_HTML; |
||
185 | |||
186 | $this->html = $part; |
||
187 | return $this; |
||
188 | } |
||
189 | |||
190 | |||
191 | /** |
||
192 | * Adds an attachment to the message. |
||
193 | * |
||
194 | * @param string $data Binary or string |
||
195 | * @param string $mimetype Mime type of the attachment (e.g. "text/plain", "application/octet-stream", etc.) |
||
196 | * @param string|null $filename Name of the attached file (or null if inline disposition is used) |
||
197 | * @param string $disposition Type of the disposition ("attachment" or "inline") |
||
198 | * @return \Aimeos\MW\Mail\Message\Iface Message object |
||
199 | */ |
||
200 | public function addAttachment( $data, $mimetype, $filename, $disposition = 'attachment' ) |
||
201 | { |
||
202 | $part = new \Zend\Mime\Part( $data ); |
||
203 | |||
204 | $part->encoding = \Zend\Mime\Mime::ENCODING_BASE64; |
||
205 | $part->disposition = $disposition; |
||
206 | $part->filename = $filename; |
||
207 | $part->type = $mimetype; |
||
208 | |||
209 | $this->attach[] = $part; |
||
210 | |||
211 | return $this; |
||
212 | } |
||
213 | |||
214 | |||
215 | /** |
||
216 | * Embeds an attachment into the message and returns its reference. |
||
217 | * |
||
218 | * @param string $data Binary or string |
||
219 | * @param string $mimetype Mime type of the attachment (e.g. "text/plain", "application/octet-stream", etc.) |
||
220 | * @param string|null $filename Name of the attached file |
||
221 | * @return string Content ID for referencing the attachment in the HTML body |
||
222 | */ |
||
223 | public function embedAttachment( $data, $mimetype, $filename ) |
||
224 | { |
||
225 | $cnt = 0; |
||
226 | $newfile = $filename; |
||
227 | |||
228 | while( isset( $this->embedded[$newfile] ) ) { |
||
229 | $newfile = ++$cnt . '_' . $filename; |
||
230 | } |
||
231 | |||
232 | $part = new \Zend\Mime\Part( $data ); |
||
233 | |||
234 | $part->disposition = \Zend\Mime\Mime::DISPOSITION_INLINE; |
||
235 | $part->encoding = \Zend\Mime\Mime::ENCODING_BASE64; |
||
236 | $part->filename = $newfile; |
||
237 | $part->type = $mimetype; |
||
238 | $part->id = md5( $newfile . mt_rand() ); |
||
239 | |||
240 | $this->embedded[$newfile] = $part; |
||
241 | |||
242 | return 'cid:' . $part->id; |
||
243 | } |
||
244 | |||
245 | |||
246 | /** |
||
247 | * Returns the internal Zend mail message object. |
||
248 | * |
||
249 | * @return Zend\Mail\Message Zend mail message object |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
250 | */ |
||
251 | public function getObject() |
||
252 | { |
||
253 | $msgparts = $parts = []; |
||
254 | |||
255 | if( !empty( $this->embedded ) ) |
||
256 | { |
||
257 | $type = \Zend\Mime\Mime::MULTIPART_RELATED; |
||
258 | |||
259 | if( $this->html != null ) { |
||
260 | $parts[] = $this->createContainer( array_merge( array( $this->html ), $this->embedded ), $type ); |
||
261 | } else { |
||
262 | $parts[] = $this->createContainer( $this->embedded, $type ); |
||
263 | } |
||
264 | } |
||
265 | else if( $this->html != null ) |
||
266 | { |
||
267 | $parts[] = $this->html; |
||
268 | } |
||
269 | |||
270 | if( $this->text !== null ) { |
||
271 | $parts[] = $this->text; |
||
272 | } |
||
273 | |||
274 | if( count( $parts ) === 2 ) |
||
275 | { |
||
276 | $type = \Zend\Mime\Mime::MULTIPART_ALTERNATIVE; |
||
277 | $msgparts = array( $this->createContainer( array_reverse( $parts ), $type ) ); |
||
278 | } |
||
279 | else if( !empty( $parts ) ) |
||
280 | { |
||
281 | $msgparts = $parts; |
||
282 | } |
||
283 | |||
284 | $msg = new \Zend\Mime\Message(); |
||
285 | $msg->setParts( array_merge( $msgparts, $this->attach ) ); |
||
286 | |||
287 | $this->object->setBody( $msg ); |
||
288 | |||
289 | return $this->object; |
||
290 | } |
||
291 | |||
292 | |||
293 | /** |
||
294 | * Clones the internal objects. |
||
295 | */ |
||
296 | public function __clone() |
||
297 | { |
||
298 | $this->object = clone $this->object; |
||
299 | } |
||
300 | |||
301 | |||
302 | /** |
||
303 | * Creates a mail message container of the given type for the mime parts. |
||
304 | * |
||
305 | * @param Zend\Mime\Part[] $parts List of mime parts that should be included in the container |
||
306 | * @param string $type Mime type, e.g. "multipart/related" or "multipart/alternative" |
||
307 | * @return \Zend\Mime\Part Container mime object |
||
308 | */ |
||
309 | protected function createContainer( array $parts, $type ) |
||
310 | { |
||
311 | $msg = new \Zend\Mime\Message(); |
||
312 | $msg->setParts( $parts ); |
||
313 | |||
314 | $part = new \Zend\Mime\Part( $msg->generateMessage() ); |
||
315 | |||
316 | $part->encoding = \Zend\Mime\Mime::ENCODING_8BIT; |
||
317 | $part->boundary = $msg->getMime()->boundary(); |
||
318 | $part->disposition = null; |
||
319 | $part->charset = null; |
||
320 | $part->type = $type; |
||
321 | |||
322 | return $part; |
||
323 | } |
||
324 | } |
||
325 |