Skip to content

Commit cc87efc

Browse files
committed
Merge branch 'sw-26872/send-mail-message-id' into '5.7'
SW-26872 - Consider hostname config for 'Message-Id' header See merge request shopware/5/product/shopware!868
2 parents 1914869 + 5790db2 commit cc87efc

File tree

8 files changed

+30
-33
lines changed

8 files changed

+30
-33
lines changed

engine/Library/Zend/Mail.php

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
* @see Zend_Mime_Part
3737
*/
3838

39+
use Shopware\Components\Random;
3940

4041
/**
4142
* Class for sending an email.
@@ -119,7 +120,7 @@ class Zend_Mail extends Zend_Mime_Message
119120

120121
/**
121122
* Subject: header
122-
* @var string
123+
* @var string|null
123124
*/
124125
protected $_subject = null;
125126

@@ -167,6 +168,7 @@ class Zend_Mail extends Zend_Mime_Message
167168
*/
168169
public $hasAttachments = false;
169170

171+
private ?string $messageIdHostName;
170172

171173
/**
172174
* Sets the default mail transport for all following uses of
@@ -201,17 +203,12 @@ public static function clearDefaultTransport()
201203
self::$_defaultTransport = null;
202204
}
203205

204-
/**
205-
* Public constructor
206-
*
207-
* @param string $charset
208-
* @return void
209-
*/
210-
public function __construct($charset = null)
206+
public function __construct(?string $charset = null, ?string $messageIdHostName = null)
211207
{
212-
if ($charset != null) {
208+
if ($charset !== null) {
213209
$this->_charset = $charset;
214210
}
211+
$this->messageIdHostName = $messageIdHostName;
215212
}
216213

217214
/**
@@ -926,7 +923,7 @@ public function setSubject($subject)
926923
/**
927924
* Returns the encoded subject of the message
928925
*
929-
* @return string
926+
* @return string|null
930927
*/
931928
public function getSubject()
932929
{
@@ -1087,17 +1084,19 @@ public function createMessageId() {
10871084
$user = getmypid();
10881085
}
10891086

1090-
$rand = Shopware\Components\Random::getInteger(0, PHP_INT_MAX);
1087+
$rand = Random::getInteger(0, PHP_INT_MAX);
10911088

10921089
if ($this->_recipients !== array()) {
10931090
$recipient = array_rand($this->_recipients);
10941091
} else {
10951092
$recipient = 'unknown';
10961093
}
10971094

1098-
if (isset($_SERVER["SERVER_NAME"])) {
1099-
$hostName = $_SERVER["SERVER_NAME"];
1100-
} else {
1095+
$hostName = $this->messageIdHostName;
1096+
if (!$hostName) {
1097+
$hostName = (string) ($_SERVER['SERVER_NAME'] ?? '');
1098+
}
1099+
if (!$hostName) {
11011100
$hostName = php_uname('n');
11021101
}
11031102

engine/Shopware/Bundle/MailBundle/DependencyInjection/services.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
class="Shopware\Bundle\MailBundle\Service\LogEntryMailBuilder">
4242
<argument type="service" id="shopware.filesystem.private" />
4343
<argument type="service" id="Shopware\Bundle\MediaBundle\MediaServiceInterface" />
44+
<argument type="service" id="mail"/>
4445
</service>
4546

4647
<service id="Shopware\Bundle\MailBundle\Service\LogServiceInterface"

engine/Shopware/Bundle/MailBundle/Service/LogEntryBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function build(Enlight_Components_Mail $mail): Log
7070
{
7171
$logEntry = new Log();
7272

73-
$logEntry->setSubject((string) iconv_mime_decode($mail->getSubject()));
73+
$logEntry->setSubject((string) iconv_mime_decode((string) $mail->getSubject()));
7474
$logEntry->setSender($mail->getFrom());
7575
$logEntry->setSentAt(new DateTime((string) $mail->getDate()));
7676
$logEntry->setContentText($mail->getPlainBodyText());

engine/Shopware/Bundle/MailBundle/Service/LogEntryMailBuilder.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,34 +29,30 @@
2929
use RuntimeException;
3030
use Shopware\Bundle\MediaBundle\MediaServiceInterface;
3131
use Shopware\Models\Mail\Log;
32-
use Shopware\Models\Order\Document\Document;
3332

3433
class LogEntryMailBuilder implements LogEntryMailBuilderInterface
3534
{
3635
public const INVALID_SENDER_REPLACEMENT_ADDRESS = '[email protected]';
3736

38-
/**
39-
* @var FilesystemInterface
40-
*/
41-
private $filesystem;
37+
private FilesystemInterface $filesystem;
4238

43-
/**
44-
* @var MediaServiceInterface
45-
*/
46-
private $mediaService;
39+
private MediaServiceInterface $mediaService;
40+
41+
private Enlight_Components_Mail $mail;
4742

48-
public function __construct(FilesystemInterface $filesystem, MediaServiceInterface $mediaService)
43+
public function __construct(FilesystemInterface $filesystem, MediaServiceInterface $mediaService, Enlight_Components_Mail $mail)
4944
{
5045
$this->filesystem = $filesystem;
5146
$this->mediaService = $mediaService;
47+
$this->mail = $mail;
5248
}
5349

5450
/**
5551
* {@inheritdoc}
5652
*/
5753
public function build(Log $entry): Enlight_Components_Mail
5854
{
59-
$mail = new Enlight_Components_Mail('UTF-8');
55+
$mail = clone $this->mail;
6056

6157
try {
6258
$mail->setFrom($entry->getSender());
@@ -104,7 +100,6 @@ protected function assignOrderDocuments(Log $logEntry, Enlight_Components_Mail $
104100
return;
105101
}
106102

107-
/** @var Document $document */
108103
foreach ($logEntry->getDocuments() as $document) {
109104
$filePath = sprintf('documents/%s.pdf', $document->getHash());
110105
$fileName = sprintf('%s.pdf', $document->getType()->getName());

engine/Shopware/Components/DependencyInjection/Bridge/Mail.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ public function factory(Container $container, Shopware_Components_Config $config
4141

4242
if (isset($options['charset'])) {
4343
$defaultCharSet = $options['charset'];
44-
} elseif (!empty($config->CharSet)) {
45-
$defaultCharSet = $config->CharSet;
44+
} elseif (!empty($config->get('CharSet'))) {
45+
$defaultCharSet = $config->get('CharSet');
4646
} else {
4747
$defaultCharSet = null;
4848
}
4949

50-
return new Enlight_Components_Mail($defaultCharSet);
50+
return new Enlight_Components_Mail($defaultCharSet, $config->get('mailer_hostname') ?: null);
5151
}
5252
}

tests/Functional/Bundle/MailBundle/LogEntryMailBuilderTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ protected function setUp(): void
5353
$this->entryBuilder = new LogEntryBuilder($this->getContainer()->get(ModelManager::class));
5454
$this->mailBuilder = new LogEntryMailBuilder(
5555
$this->getContainer()->get('shopware.filesystem.private'),
56-
$this->getContainer()->get(MediaServiceInterface::class)
56+
$this->getContainer()->get(MediaServiceInterface::class),
57+
$this->getContainer()->get('mail')
5758
);
5859
}
5960

tests/Functional/Bundle/MailBundle/LogRepositoryTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ public function testUniqueConstraintIsCaseSensitive(): void
134134
$firstMail = $this->createSimpleMail();
135135
$secondMail = new Enlight_Components_Mail('UTF-8');
136136

137+
static::assertIsString($firstMail->getSubject());
137138
$secondMail->setSubject($firstMail->getSubject());
138139
$secondMail->setFrom(ucfirst($firstMail->getFrom()));
139140
$secondMail->setBodyText($firstMail->getBodyText()->getRawContent());

tests/Functional/Components/MailTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function testMessageId(): void
3636
{
3737
$mailTransport = $this->getContainer()->get('mailtransport');
3838

39-
$mail = new Enlight_Components_Mail();
39+
$mail = new Enlight_Components_Mail(null, 'test.com');
4040
$mail->setBodyText('Test Hello');
4141
$mail->addTo('[email protected]');
4242

@@ -46,7 +46,7 @@ public function testMessageId(): void
4646
static::assertArrayHasKey('Message-Id', $headers);
4747
$messageId = $headers['Message-Id'][0];
4848
static::assertIsString($messageId);
49-
static::assertStringContainsString('@', $messageId);
49+
static::assertStringContainsString('@test.com', $messageId);
5050
static::assertStringStartsWith('<', $messageId);
5151
static::assertStringEndsWith('>', $messageId);
5252
}

0 commit comments

Comments
 (0)