3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace Magento \Newsletter \Model \Queue ;
7
9
8
10
use Magento \Email \Model \AbstractTemplate ;
11
+ use Magento \Framework \Mail \EmailMessageInterfaceFactory ;
12
+ use Magento \Framework \Mail \MessageInterface ;
13
+ use Magento \Framework \Mail \MessageInterfaceFactory ;
14
+ use Magento \Framework \Mail \MimeMessageInterfaceFactory ;
15
+ use Magento \Framework \Mail \MimePartInterfaceFactory ;
16
+ use Magento \Framework \Mail \Template \FactoryInterface ;
17
+ use Magento \Framework \Mail \Template \SenderResolverInterface ;
18
+ use Magento \Framework \Mail \TransportInterfaceFactory ;
19
+ use Magento \Framework \ObjectManagerInterface ;
9
20
21
+ /**
22
+ * Class TransportBuilder
23
+ */
10
24
class TransportBuilder extends \Magento \Framework \Mail \Template \TransportBuilder
11
25
{
12
26
/**
@@ -16,6 +30,151 @@ class TransportBuilder extends \Magento\Framework\Mail\Template\TransportBuilder
16
30
*/
17
31
protected $ templateData = [];
18
32
33
+ /**
34
+ * Param that used for storing all message data until it will be used
35
+ *
36
+ * @var array
37
+ */
38
+ private $ messageData = [];
39
+
40
+ /**
41
+ * @var EmailMessageInterfaceFactory
42
+ */
43
+ private $ emailMessageInterfaceFactory ;
44
+
45
+ /**
46
+ * @var MimeMessageInterfaceFactory
47
+ */
48
+ private $ mimeMessageInterfaceFactory ;
49
+
50
+ /**
51
+ * @var MimePartInterfaceFactory
52
+ */
53
+ private $ mimePartInterfaceFactory ;
54
+
55
+ /**
56
+ * TransportBuilder constructor
57
+ *
58
+ * @param FactoryInterface $templateFactory
59
+ * @param MessageInterface $message
60
+ * @param SenderResolverInterface $senderResolver
61
+ * @param ObjectManagerInterface $objectManager
62
+ * @param TransportInterfaceFactory $mailTransportFactory
63
+ * @param MessageInterfaceFactory|null $messageFactory
64
+ * @param EmailMessageInterfaceFactory|null $emailMessageInterfaceFactory
65
+ * @param MimeMessageInterfaceFactory|null $mimeMessageInterfaceFactory
66
+ * @param MimePartInterfaceFactory|null $mimePartInterfaceFactory
67
+ */
68
+ public function __construct (
69
+ FactoryInterface $ templateFactory ,
70
+ MessageInterface $ message ,
71
+ SenderResolverInterface $ senderResolver ,
72
+ ObjectManagerInterface $ objectManager ,
73
+ TransportInterfaceFactory $ mailTransportFactory ,
74
+ MessageInterfaceFactory $ messageFactory = null ,
75
+ EmailMessageInterfaceFactory $ emailMessageInterfaceFactory = null ,
76
+ MimeMessageInterfaceFactory $ mimeMessageInterfaceFactory = null ,
77
+ MimePartInterfaceFactory $ mimePartInterfaceFactory = null
78
+ ) {
79
+ parent ::__construct (
80
+ $ templateFactory ,
81
+ $ message ,
82
+ $ senderResolver ,
83
+ $ objectManager ,
84
+ $ mailTransportFactory ,
85
+ $ messageFactory ,
86
+ $ emailMessageInterfaceFactory ,
87
+ $ mimeMessageInterfaceFactory ,
88
+ $ mimePartInterfaceFactory
89
+ );
90
+ $ this ->emailMessageInterfaceFactory = $ emailMessageInterfaceFactory ?: $ this ->objectManager
91
+ ->get (EmailMessageInterfaceFactory::class);
92
+ $ this ->mimeMessageInterfaceFactory = $ mimeMessageInterfaceFactory ?: $ this ->objectManager
93
+ ->get (MimeMessageInterfaceFactory::class);
94
+ $ this ->mimePartInterfaceFactory = $ mimePartInterfaceFactory ?: $ this ->objectManager
95
+ ->get (MimePartInterfaceFactory::class);
96
+ }
97
+
98
+ /**
99
+ * @inheritDoc
100
+ */
101
+ public function addCc ($ address , $ name = '' )
102
+ {
103
+ $ this ->messageData ['cc ' ][$ address ] = $ name ;
104
+
105
+ return $ this ;
106
+ }
107
+
108
+ /**
109
+ * Add to address
110
+ *
111
+ * @param array|string $address
112
+ * @param string $name
113
+ * @return $this
114
+ */
115
+ public function addTo ($ address , $ name = '' )
116
+ {
117
+ if (!$ name ) {
118
+ $ this ->messageData ['to ' ][] = $ address ;
119
+ } else {
120
+ $ this ->messageData ['to ' ][$ address ] = $ name ;
121
+ }
122
+
123
+ return $ this ;
124
+ }
125
+
126
+ /**
127
+ * @inheritDoc
128
+ */
129
+ public function addBcc ($ address )
130
+ {
131
+ $ this ->messageData ['bcc ' ] = $ address ;
132
+
133
+ return $ this ;
134
+ }
135
+
136
+ /**
137
+ * @inheritDoc
138
+ */
139
+ public function setReplyTo ($ email , $ name = null )
140
+ {
141
+ $ this ->messageData ['replyTo ' ][$ email ] = $ name ;
142
+
143
+ return $ this ;
144
+ }
145
+
146
+ /**
147
+ * @inheritDoc
148
+ */
149
+ public function setFrom ($ from )
150
+ {
151
+ return $ this ->setFromByScope ($ from , null );
152
+ }
153
+
154
+ /**
155
+ * @inheritDoc
156
+ */
157
+ public function setFromByScope ($ from , $ scopeId = null )
158
+ {
159
+ $ result = $ this ->_senderResolver ->resolve ($ from , $ scopeId );
160
+ $ this ->messageData ['from ' ][$ result ['email ' ]] = $ result ['name ' ];
161
+
162
+ return $ this ;
163
+ }
164
+
165
+ /**
166
+ * @inheritDoc
167
+ */
168
+ protected function reset ()
169
+ {
170
+ $ this ->messageData = [];
171
+ $ this ->templateIdentifier = null ;
172
+ $ this ->templateVars = null ;
173
+ $ this ->templateOptions = null ;
174
+
175
+ return $ this ;
176
+ }
177
+
19
178
/**
20
179
* Set template data
21
180
*
@@ -25,11 +184,15 @@ class TransportBuilder extends \Magento\Framework\Mail\Template\TransportBuilder
25
184
public function setTemplateData ($ data )
26
185
{
27
186
$ this ->templateData = $ data ;
187
+
28
188
return $ this ;
29
189
}
30
190
31
191
/**
192
+ * Sets up template filter
193
+ *
32
194
* @param AbstractTemplate $template
195
+ *
33
196
* @return void
34
197
*/
35
198
protected function setTemplateFilter (AbstractTemplate $ template )
@@ -48,9 +211,16 @@ protected function prepareMessage()
48
211
$ template = $ this ->getTemplate ()->setData ($ this ->templateData );
49
212
$ this ->setTemplateFilter ($ template );
50
213
$ part ['content ' ] = $ template ->getProcessedTemplate ($ this ->templateVars );
51
- $ this ->messageData ['body ' ][] = $ part ;
52
214
$ this ->messageData ['subject ' ] = $ template ->getSubject ();
53
- $ this ->message = $ this ->mailEnvelopeBuilder ->buildByArray ($ this ->messageData );
215
+
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
222
+ );
223
+ $ this ->message = $ this ->emailMessageInterfaceFactory ->create ($ this ->messageData );
54
224
55
225
return $ this ;
56
226
}
0 commit comments