33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+ declare (strict_types=1 );
7+
68namespace Magento \Newsletter \Model \Queue ;
79
810use Magento \Email \Model \AbstractTemplate ;
11+ use Magento \Framework \Exception \MailException ;
12+ use Magento \Framework \Mail \EmailMessageInterfaceFactory ;
13+ use Magento \Framework \Mail \AddressConverter ;
14+ use Magento \Framework \Mail \MessageInterface ;
15+ use Magento \Framework \Mail \MessageInterfaceFactory ;
16+ use Magento \Framework \Mail \MimeMessageInterfaceFactory ;
17+ use Magento \Framework \Mail \MimePartInterfaceFactory ;
18+ use Magento \Framework \Mail \Template \FactoryInterface ;
19+ use Magento \Framework \Mail \Template \SenderResolverInterface ;
20+ use Magento \Framework \Mail \TemplateInterface ;
21+ use Magento \Framework \Mail \TransportInterfaceFactory ;
22+ use Magento \Framework \ObjectManagerInterface ;
923
24+ /**
25+ * Class TransportBuilder
26+ *
27+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
28+ */
1029class TransportBuilder extends \Magento \Framework \Mail \Template \TransportBuilder
1130{
1231 /**
@@ -16,6 +35,194 @@ class TransportBuilder extends \Magento\Framework\Mail\Template\TransportBuilder
1635 */
1736 protected $ templateData = [];
1837
38+ /**
39+ * Param that used for storing all message data until it will be used
40+ *
41+ * @var array
42+ */
43+ private $ messageData = [];
44+
45+ /**
46+ * @var EmailMessageInterfaceFactory
47+ */
48+ private $ emailMessageInterfaceFactory ;
49+
50+ /**
51+ * @var MimeMessageInterfaceFactory
52+ */
53+ private $ mimeMessageInterfaceFactory ;
54+
55+ /**
56+ * @var MimePartInterfaceFactory
57+ */
58+ private $ mimePartInterfaceFactory ;
59+
60+ /**
61+ * @var AddressConverter|null
62+ */
63+ private $ addressConverter ;
64+
65+ /**
66+ * TransportBuilder constructor
67+ *
68+ * @param FactoryInterface $templateFactory
69+ * @param MessageInterface $message
70+ * @param SenderResolverInterface $senderResolver
71+ * @param ObjectManagerInterface $objectManager
72+ * @param TransportInterfaceFactory $mailTransportFactory
73+ * @param MessageInterfaceFactory|null $messageFactory
74+ * @param EmailMessageInterfaceFactory|null $emailMessageInterfaceFactory
75+ * @param MimeMessageInterfaceFactory|null $mimeMessageInterfaceFactory
76+ * @param MimePartInterfaceFactory|null $mimePartInterfaceFactory
77+ * @param AddressConverter|null $addressConverter
78+ * @SuppressWarnings(PHPMD.UnusedFormalParameter)
79+ * @SuppressWarnings(PHPMD.ExcessiveParameterList)
80+ */
81+ public function __construct (
82+ FactoryInterface $ templateFactory ,
83+ MessageInterface $ message ,
84+ SenderResolverInterface $ senderResolver ,
85+ ObjectManagerInterface $ objectManager ,
86+ TransportInterfaceFactory $ mailTransportFactory ,
87+ MessageInterfaceFactory $ messageFactory = null ,
88+ EmailMessageInterfaceFactory $ emailMessageInterfaceFactory = null ,
89+ MimeMessageInterfaceFactory $ mimeMessageInterfaceFactory = null ,
90+ MimePartInterfaceFactory $ mimePartInterfaceFactory = null ,
91+ AddressConverter $ addressConverter = null
92+ ) {
93+ parent ::__construct (
94+ $ templateFactory ,
95+ $ message ,
96+ $ senderResolver ,
97+ $ objectManager ,
98+ $ mailTransportFactory ,
99+ $ messageFactory ,
100+ $ emailMessageInterfaceFactory ,
101+ $ mimeMessageInterfaceFactory ,
102+ $ mimePartInterfaceFactory ,
103+ $ addressConverter
104+ );
105+ $ this ->emailMessageInterfaceFactory = $ emailMessageInterfaceFactory ?: $ this ->objectManager
106+ ->get (EmailMessageInterfaceFactory::class);
107+ $ this ->mimeMessageInterfaceFactory = $ mimeMessageInterfaceFactory ?: $ this ->objectManager
108+ ->get (MimeMessageInterfaceFactory::class);
109+ $ this ->mimePartInterfaceFactory = $ mimePartInterfaceFactory ?: $ this ->objectManager
110+ ->get (MimePartInterfaceFactory::class);
111+ $ this ->addressConverter = $ addressConverter ?: $ this ->objectManager
112+ ->get (AddressConverter::class);
113+ }
114+
115+ /**
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
123+ */
124+ public function addCc ($ address , $ name = '' )
125+ {
126+ $ this ->addAddressByType ('cc ' , $ address , $ name );
127+
128+ return $ this ;
129+ }
130+
131+ /**
132+ * Add to address
133+ *
134+ * @param array|string $address
135+ * @param string $name
136+ *
137+ * @return $this
138+ * @throws MailException
139+ */
140+ public function addTo ($ address , $ name = '' )
141+ {
142+ $ this ->addAddressByType ('to ' , $ address , $ name );
143+
144+ return $ this ;
145+ }
146+
147+ /**
148+ * Add bcc address
149+ *
150+ * @param array|string $address
151+ *
152+ * @return $this
153+ * @throws MailException
154+ */
155+ public function addBcc ($ address )
156+ {
157+ $ this ->addAddressByType ('bcc ' , $ address );
158+
159+ return $ this ;
160+ }
161+
162+ /**
163+ * Set Reply-To Header
164+ *
165+ * @param string $email
166+ * @param string|null $name
167+ *
168+ * @return $this
169+ * @throws MailException
170+ */
171+ public function setReplyTo ($ email , $ name = null )
172+ {
173+
174+ $ this ->addAddressByType ('replyTo ' , $ email , $ name );
175+
176+ return $ this ;
177+ }
178+
179+ /**
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.
190+ */
191+ public function setFrom ($ from )
192+ {
193+ return $ this ->setFromByScope ($ from );
194+ }
195+
196+ /**
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
204+ */
205+ public function setFromByScope ($ from , $ scopeId = null )
206+ {
207+ $ result = $ this ->_senderResolver ->resolve ($ from , $ scopeId );
208+ $ this ->addAddressByType ('from ' , $ result ['email ' ], $ result ['name ' ]);
209+
210+ return $ this ;
211+ }
212+
213+ /**
214+ * @inheritDoc
215+ */
216+ protected function reset ()
217+ {
218+ $ this ->messageData = [];
219+ $ this ->templateIdentifier = null ;
220+ $ this ->templateVars = null ;
221+ $ this ->templateOptions = null ;
222+
223+ return $ this ;
224+ }
225+
19226 /**
20227 * Set template data
21228 *
@@ -25,11 +232,15 @@ class TransportBuilder extends \Magento\Framework\Mail\Template\TransportBuilder
25232 public function setTemplateData ($ data )
26233 {
27234 $ this ->templateData = $ data ;
235+
28236 return $ this ;
29237 }
30238
31239 /**
240+ * Sets up template filter
241+ *
32242 * @param AbstractTemplate $template
243+ *
33244 * @return void
34245 */
35246 protected function setTemplateFilter (AbstractTemplate $ template )
@@ -44,16 +255,44 @@ protected function setTemplateFilter(AbstractTemplate $template)
44255 */
45256 protected function prepareMessage ()
46257 {
47- /** @var AbstractTemplate $template */
258+ /** @var AbstractTemplate|TemplateInterface $template */
48259 $ template = $ this ->getTemplate ()->setData ($ this ->templateData );
49260 $ this ->setTemplateFilter ($ template );
261+ $ content = $ template ->getProcessedTemplate ($ this ->templateVars );
262+ $ this ->messageData ['subject ' ] = $ template ->getSubject ();
50263
51- $ this ->message ->setBodyHtml (
52- $ template ->getProcessedTemplate ($ this ->templateVars )
53- )->setSubject (
54- $ template ->getSubject ()
264+ $ mimePart = $ this ->mimePartInterfaceFactory ->create (
265+ ['content ' => $ content ]
266+ );
267+ $ this ->messageData ['body ' ] = $ this ->mimeMessageInterfaceFactory ->create (
268+ ['parts ' => [$ mimePart ]]
55269 );
56270
271+ $ this ->message = $ this ->emailMessageInterfaceFactory ->create ($ this ->messageData );
272+
57273 return $ this ;
58274 }
275+
276+ /**
277+ * Handles possible incoming types of email (string or array)
278+ *
279+ * @param string $addressType
280+ * @param string|array $email
281+ * @param string|null $name
282+ *
283+ * @return void
284+ * @throws MailException
285+ */
286+ private function addAddressByType (string $ addressType , $ email , ?string $ name = null ): void
287+ {
288+ if (is_array ($ email )) {
289+ $ this ->messageData [$ addressType ] = array_merge (
290+ $ this ->messageData [$ addressType ],
291+ $ this ->addressConverter ->convertMany ($ email )
292+ );
293+
294+ return ;
295+ }
296+ $ this ->messageData [$ addressType ][] = $ this ->addressConverter ->convert ($ email , $ name );
297+ }
59298}
0 commit comments