Skip to content

Commit 3aabe19

Browse files
committed
MC-15295: Fix MailAddress disign, Add MailAddressConverter
1 parent 7fd2dc8 commit 3aabe19

File tree

9 files changed

+312
-372
lines changed

9 files changed

+312
-372
lines changed

app/code/Magento/Newsletter/Model/Queue/TransportBuilder.php

Lines changed: 96 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<?php
2+
/** @noinspection SenselessMethodDuplicationInspection */
3+
/** @noinspection ReturnTypeCanBeDeclaredInspection */
4+
/** @noinspection PhpUndefinedClassInspection */
25
/**
36
* Copyright © Magento, Inc. All rights reserved.
47
* See COPYING.txt for license details.
@@ -8,13 +11,17 @@
811
namespace Magento\Newsletter\Model\Queue;
912

1013
use Magento\Email\Model\AbstractTemplate;
14+
use Magento\Framework\Exception\MailException;
1115
use Magento\Framework\Mail\EmailMessageInterfaceFactory;
16+
use Magento\Framework\Mail\MailAddressConverter;
17+
use Magento\Framework\Mail\MailAddressList;
1218
use Magento\Framework\Mail\MessageInterface;
1319
use Magento\Framework\Mail\MessageInterfaceFactory;
1420
use Magento\Framework\Mail\MimeMessageInterfaceFactory;
1521
use Magento\Framework\Mail\MimePartInterfaceFactory;
1622
use Magento\Framework\Mail\Template\FactoryInterface;
1723
use Magento\Framework\Mail\Template\SenderResolverInterface;
24+
use Magento\Framework\Mail\TemplateInterface;
1825
use Magento\Framework\Mail\TransportInterfaceFactory;
1926
use Magento\Framework\ObjectManagerInterface;
2027

@@ -52,6 +59,11 @@ class TransportBuilder extends \Magento\Framework\Mail\Template\TransportBuilder
5259
*/
5360
private $mimePartInterfaceFactory;
5461

62+
/**
63+
* @var MailAddressConverter|null
64+
*/
65+
private $mailAddressConverter;
66+
5567
/**
5668
* TransportBuilder constructor
5769
*
@@ -64,6 +76,7 @@ class TransportBuilder extends \Magento\Framework\Mail\Template\TransportBuilder
6476
* @param EmailMessageInterfaceFactory|null $emailMessageInterfaceFactory
6577
* @param MimeMessageInterfaceFactory|null $mimeMessageInterfaceFactory
6678
* @param MimePartInterfaceFactory|null $mimePartInterfaceFactory
79+
* @param MailAddressConverter|null $mailAddressConverter
6780
*/
6881
public function __construct(
6982
FactoryInterface $templateFactory,
@@ -74,7 +87,8 @@ public function __construct(
7487
MessageInterfaceFactory $messageFactory = null,
7588
EmailMessageInterfaceFactory $emailMessageInterfaceFactory = null,
7689
MimeMessageInterfaceFactory $mimeMessageInterfaceFactory = null,
77-
MimePartInterfaceFactory $mimePartInterfaceFactory = null
90+
MimePartInterfaceFactory $mimePartInterfaceFactory = null,
91+
MailAddressConverter $mailAddressConverter = null
7892
) {
7993
parent::__construct(
8094
$templateFactory,
@@ -85,22 +99,31 @@ public function __construct(
8599
$messageFactory,
86100
$emailMessageInterfaceFactory,
87101
$mimeMessageInterfaceFactory,
88-
$mimePartInterfaceFactory
102+
$mimePartInterfaceFactory,
103+
$mailAddressConverter
89104
);
90105
$this->emailMessageInterfaceFactory = $emailMessageInterfaceFactory ?: $this->objectManager
91106
->get(EmailMessageInterfaceFactory::class);
92107
$this->mimeMessageInterfaceFactory = $mimeMessageInterfaceFactory ?: $this->objectManager
93108
->get(MimeMessageInterfaceFactory::class);
94109
$this->mimePartInterfaceFactory = $mimePartInterfaceFactory ?: $this->objectManager
95110
->get(MimePartInterfaceFactory::class);
111+
$this->mailAddressConverter = $mailAddressConverter ?: $this->objectManager
112+
->get(MailAddressConverter::class);
96113
}
97114

98115
/**
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
100123
*/
101124
public function addCc($address, $name = '')
102125
{
103-
$this->messageData['cc'][$address] = $name;
126+
$this->getMailAddresses('cc', $address, $name);
104127

105128
return $this;
106129
}
@@ -110,54 +133,79 @@ public function addCc($address, $name = '')
110133
*
111134
* @param array|string $address
112135
* @param string $name
136+
*
113137
* @return $this
138+
* @throws MailException
114139
*/
115140
public function addTo($address, $name = '')
116141
{
117-
if (!$name) {
118-
$this->messageData['to'][] = $address;
119-
} else {
120-
$this->messageData['to'][$address] = $name;
121-
}
142+
$this->getMailAddresses('to', $address, $name);
122143

123144
return $this;
124145
}
125146

126147
/**
127-
* @inheritDoc
148+
* Add bcc address
149+
*
150+
* @param array|string $address
151+
*
152+
* @return $this
153+
* @throws MailException
128154
*/
129155
public function addBcc($address)
130156
{
131-
$this->messageData['bcc'] = $address;
157+
$this->getMailAddresses('bcc', $address);
132158

133159
return $this;
134160
}
135161

136162
/**
137-
* @inheritDoc
163+
* Set Reply-To Header
164+
*
165+
* @param string $email
166+
* @param string|null $name
167+
*
168+
* @return $this
169+
* @throws MailException
138170
*/
139171
public function setReplyTo($email, $name = null)
140172
{
141-
$this->messageData['replyTo'][$email] = $name;
173+
174+
$this->getMailAddresses('replyTo', $email, $name);
142175

143176
return $this;
144177
}
145178

146179
/**
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.
148190
*/
149191
public function setFrom($from)
150192
{
151-
return $this->setFromByScope($from, null);
193+
return $this->setFromByScope($from);
152194
}
153195

154196
/**
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
156204
*/
157205
public function setFromByScope($from, $scopeId = null)
158206
{
159207
$result = $this->_senderResolver->resolve($from, $scopeId);
160-
$this->messageData['from'][$result['email']] = $result['name'];
208+
$this->getMailAddresses('from', $result['email'], $result['name']);
161209

162210
return $this;
163211
}
@@ -207,21 +255,44 @@ protected function setTemplateFilter(AbstractTemplate $template)
207255
*/
208256
protected function prepareMessage()
209257
{
210-
/** @var AbstractTemplate $template */
258+
/** @var AbstractTemplate|TemplateInterface $template */
211259
$template = $this->getTemplate()->setData($this->templateData);
212260
$this->setTemplateFilter($template);
213-
$part['content'] = $template->getProcessedTemplate($this->templateVars);
261+
$content = $template->getProcessedTemplate($this->templateVars);
214262
$this->messageData['subject'] = $template->getSubject();
215263

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]
222266
);
267+
$this->messageData['body'] = $this->mimeMessageInterfaceFactory->create(
268+
['parts' => [$mimePart]]
269+
);
270+
223271
$this->message = $this->emailMessageInterfaceFactory->create($this->messageData);
224272

225273
return $this;
226274
}
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+
}
227298
}

0 commit comments

Comments
 (0)