1
1
<?php
2
+ /** @noinspection SenselessMethodDuplicationInspection */
3
+ /** @noinspection ReturnTypeCanBeDeclaredInspection */
4
+ /** @noinspection PhpUndefinedClassInspection */
2
5
/**
3
6
* Copyright © Magento, Inc. All rights reserved.
4
7
* See COPYING.txt for license details.
8
11
namespace Magento \Newsletter \Model \Queue ;
9
12
10
13
use Magento \Email \Model \AbstractTemplate ;
14
+ use Magento \Framework \Exception \MailException ;
11
15
use Magento \Framework \Mail \EmailMessageInterfaceFactory ;
16
+ use Magento \Framework \Mail \MailAddressConverter ;
17
+ use Magento \Framework \Mail \MailAddressList ;
12
18
use Magento \Framework \Mail \MessageInterface ;
13
19
use Magento \Framework \Mail \MessageInterfaceFactory ;
14
20
use Magento \Framework \Mail \MimeMessageInterfaceFactory ;
15
21
use Magento \Framework \Mail \MimePartInterfaceFactory ;
16
22
use Magento \Framework \Mail \Template \FactoryInterface ;
17
23
use Magento \Framework \Mail \Template \SenderResolverInterface ;
24
+ use Magento \Framework \Mail \TemplateInterface ;
18
25
use Magento \Framework \Mail \TransportInterfaceFactory ;
19
26
use Magento \Framework \ObjectManagerInterface ;
20
27
@@ -52,6 +59,11 @@ class TransportBuilder extends \Magento\Framework\Mail\Template\TransportBuilder
52
59
*/
53
60
private $ mimePartInterfaceFactory ;
54
61
62
+ /**
63
+ * @var MailAddressConverter|null
64
+ */
65
+ private $ mailAddressConverter ;
66
+
55
67
/**
56
68
* TransportBuilder constructor
57
69
*
@@ -64,6 +76,7 @@ class TransportBuilder extends \Magento\Framework\Mail\Template\TransportBuilder
64
76
* @param EmailMessageInterfaceFactory|null $emailMessageInterfaceFactory
65
77
* @param MimeMessageInterfaceFactory|null $mimeMessageInterfaceFactory
66
78
* @param MimePartInterfaceFactory|null $mimePartInterfaceFactory
79
+ * @param MailAddressConverter|null $mailAddressConverter
67
80
*/
68
81
public function __construct (
69
82
FactoryInterface $ templateFactory ,
@@ -74,7 +87,8 @@ public function __construct(
74
87
MessageInterfaceFactory $ messageFactory = null ,
75
88
EmailMessageInterfaceFactory $ emailMessageInterfaceFactory = null ,
76
89
MimeMessageInterfaceFactory $ mimeMessageInterfaceFactory = null ,
77
- MimePartInterfaceFactory $ mimePartInterfaceFactory = null
90
+ MimePartInterfaceFactory $ mimePartInterfaceFactory = null ,
91
+ MailAddressConverter $ mailAddressConverter = null
78
92
) {
79
93
parent ::__construct (
80
94
$ templateFactory ,
@@ -85,22 +99,31 @@ public function __construct(
85
99
$ messageFactory ,
86
100
$ emailMessageInterfaceFactory ,
87
101
$ mimeMessageInterfaceFactory ,
88
- $ mimePartInterfaceFactory
102
+ $ mimePartInterfaceFactory ,
103
+ $ mailAddressConverter
89
104
);
90
105
$ this ->emailMessageInterfaceFactory = $ emailMessageInterfaceFactory ?: $ this ->objectManager
91
106
->get (EmailMessageInterfaceFactory::class);
92
107
$ this ->mimeMessageInterfaceFactory = $ mimeMessageInterfaceFactory ?: $ this ->objectManager
93
108
->get (MimeMessageInterfaceFactory::class);
94
109
$ this ->mimePartInterfaceFactory = $ mimePartInterfaceFactory ?: $ this ->objectManager
95
110
->get (MimePartInterfaceFactory::class);
111
+ $ this ->mailAddressConverter = $ mailAddressConverter ?: $ this ->objectManager
112
+ ->get (MailAddressConverter::class);
96
113
}
97
114
98
115
/**
99
- * @inheritDoc
116
+ * Add cc address
117
+ *
118
+ * @param array|string $address
119
+ * @param string $name
120
+ *
121
+ * @return \Magento\Framework\Mail\Template\TransportBuilder
122
+ * @throws MailException
100
123
*/
101
124
public function addCc ($ address , $ name = '' )
102
125
{
103
- $ this ->messageData [ 'cc ' ][ $ address] = $ name ;
126
+ $ this ->getMailAddresses ( 'cc ' , $ address, $ name) ;
104
127
105
128
return $ this ;
106
129
}
@@ -110,54 +133,79 @@ public function addCc($address, $name = '')
110
133
*
111
134
* @param array|string $address
112
135
* @param string $name
136
+ *
113
137
* @return $this
138
+ * @throws MailException
114
139
*/
115
140
public function addTo ($ address , $ name = '' )
116
141
{
117
- if (!$ name ) {
118
- $ this ->messageData ['to ' ][] = $ address ;
119
- } else {
120
- $ this ->messageData ['to ' ][$ address ] = $ name ;
121
- }
142
+ $ this ->getMailAddresses ('to ' , $ address , $ name );
122
143
123
144
return $ this ;
124
145
}
125
146
126
147
/**
127
- * @inheritDoc
148
+ * Add bcc address
149
+ *
150
+ * @param array|string $address
151
+ *
152
+ * @return $this
153
+ * @throws MailException
128
154
*/
129
155
public function addBcc ($ address )
130
156
{
131
- $ this ->messageData [ 'bcc ' ] = $ address ;
157
+ $ this ->getMailAddresses ( 'bcc ' , $ address) ;
132
158
133
159
return $ this ;
134
160
}
135
161
136
162
/**
137
- * @inheritDoc
163
+ * Set Reply-To Header
164
+ *
165
+ * @param string $email
166
+ * @param string|null $name
167
+ *
168
+ * @return $this
169
+ * @throws MailException
138
170
*/
139
171
public function setReplyTo ($ email , $ name = null )
140
172
{
141
- $ this ->messageData ['replyTo ' ][$ email ] = $ name ;
173
+
174
+ $ this ->getMailAddresses ('replyTo ' , $ email , $ name );
142
175
143
176
return $ this ;
144
177
}
145
178
146
179
/**
147
- * @inheritDoc
180
+ * Set mail from address
181
+ *
182
+ * @param string|array $from
183
+ *
184
+ * @return $this
185
+ * @throws MailException
186
+ * @see setFromByScope()
187
+ *
188
+ * @deprecated This function sets the from address but does not provide
189
+ * a way of setting the correct from addresses based on the scope.
148
190
*/
149
191
public function setFrom ($ from )
150
192
{
151
- return $ this ->setFromByScope ($ from, null );
193
+ return $ this ->setFromByScope ($ from );
152
194
}
153
195
154
196
/**
155
- * @inheritDoc
197
+ * Set mail from address by scopeId
198
+ *
199
+ * @param string|array $from
200
+ * @param string|int $scopeId
201
+ *
202
+ * @return $this
203
+ * @throws MailException
156
204
*/
157
205
public function setFromByScope ($ from , $ scopeId = null )
158
206
{
159
207
$ result = $ this ->_senderResolver ->resolve ($ from , $ scopeId );
160
- $ this ->messageData [ 'from ' ][ $ result ['email ' ]] = $ result ['name ' ];
208
+ $ this ->getMailAddresses ( 'from ' , $ result ['email ' ], $ result ['name ' ]) ;
161
209
162
210
return $ this ;
163
211
}
@@ -207,21 +255,44 @@ protected function setTemplateFilter(AbstractTemplate $template)
207
255
*/
208
256
protected function prepareMessage ()
209
257
{
210
- /** @var AbstractTemplate $template */
258
+ /** @var AbstractTemplate|TemplateInterface $template */
211
259
$ template = $ this ->getTemplate ()->setData ($ this ->templateData );
212
260
$ this ->setTemplateFilter ($ template );
213
- $ part [ ' content ' ] = $ template ->getProcessedTemplate ($ this ->templateVars );
261
+ $ content = $ template ->getProcessedTemplate ($ this ->templateVars );
214
262
$ this ->messageData ['subject ' ] = $ template ->getSubject ();
215
263
216
- $ this ->messageData ['body ' ] = $ this ->mimeMessageInterfaceFactory
217
- ->create (['parts ' => [$ this ->mimePartInterfaceFactory ->create ([$ part ])]]);
218
-
219
- $ this ->messageData ['subject ' ] = html_entity_decode (
220
- (string )$ template ->getSubject (),
221
- ENT_QUOTES
264
+ $ mimePart = $ this ->mimePartInterfaceFactory ->create (
265
+ ['content ' => $ content ]
222
266
);
267
+ $ this ->messageData ['body ' ] = $ this ->mimeMessageInterfaceFactory ->create (
268
+ ['parts ' => [$ mimePart ]]
269
+ );
270
+
223
271
$ this ->message = $ this ->emailMessageInterfaceFactory ->create ($ this ->messageData );
224
272
225
273
return $ this ;
226
274
}
275
+
276
+ /**
277
+ * Handles possible incoming types of email (string or array)
278
+ *
279
+ * @param string $addressType
280
+ * @param string|array $emailOrList
281
+ * @param string|null $name
282
+ *
283
+ * @return void
284
+ * @throws MailException
285
+ */
286
+ private function getMailAddresses (string $ addressType , $ emailOrList , ?string $ name = null ): void
287
+ {
288
+ if (is_array ($ emailOrList )) {
289
+ $ this ->messageData [$ addressType ] = array_merge (
290
+ $ this ->messageData [$ addressType ],
291
+ $ this ->mailAddressConverter ->convertMany ($ emailOrList )
292
+ );
293
+
294
+ return ;
295
+ }
296
+ $ this ->messageData [$ addressType ][] = $ this ->mailAddressConverter ->convert ($ emailOrList , $ name );
297
+ }
227
298
}
0 commit comments