Skip to content

Commit

Permalink
Merge branch 'sw-26872/send-mail-message-id' into '5.7'
Browse files Browse the repository at this point in the history
SW-26872 - Consider hostname config for 'Message-Id' header

See merge request shopware/5/product/shopware!868
  • Loading branch information
mitelg committed Jul 18, 2022
2 parents 1914869 + 5790db2 commit cc87efc
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 33 deletions.
27 changes: 13 additions & 14 deletions engine/Library/Zend/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
* @see Zend_Mime_Part
*/

use Shopware\Components\Random;

/**
* Class for sending an email.
Expand Down Expand Up @@ -119,7 +120,7 @@ class Zend_Mail extends Zend_Mime_Message

/**
* Subject: header
* @var string
* @var string|null
*/
protected $_subject = null;

Expand Down Expand Up @@ -167,6 +168,7 @@ class Zend_Mail extends Zend_Mime_Message
*/
public $hasAttachments = false;

private ?string $messageIdHostName;

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

/**
* Public constructor
*
* @param string $charset
* @return void
*/
public function __construct($charset = null)
public function __construct(?string $charset = null, ?string $messageIdHostName = null)
{
if ($charset != null) {
if ($charset !== null) {
$this->_charset = $charset;
}
$this->messageIdHostName = $messageIdHostName;
}

/**
Expand Down Expand Up @@ -926,7 +923,7 @@ public function setSubject($subject)
/**
* Returns the encoded subject of the message
*
* @return string
* @return string|null
*/
public function getSubject()
{
Expand Down Expand Up @@ -1087,17 +1084,19 @@ public function createMessageId() {
$user = getmypid();
}

$rand = Shopware\Components\Random::getInteger(0, PHP_INT_MAX);
$rand = Random::getInteger(0, PHP_INT_MAX);

if ($this->_recipients !== array()) {
$recipient = array_rand($this->_recipients);
} else {
$recipient = 'unknown';
}

if (isset($_SERVER["SERVER_NAME"])) {
$hostName = $_SERVER["SERVER_NAME"];
} else {
$hostName = $this->messageIdHostName;
if (!$hostName) {
$hostName = (string) ($_SERVER['SERVER_NAME'] ?? '');
}
if (!$hostName) {
$hostName = php_uname('n');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
class="Shopware\Bundle\MailBundle\Service\LogEntryMailBuilder">
<argument type="service" id="shopware.filesystem.private" />
<argument type="service" id="Shopware\Bundle\MediaBundle\MediaServiceInterface" />
<argument type="service" id="mail"/>
</service>

<service id="Shopware\Bundle\MailBundle\Service\LogServiceInterface"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function build(Enlight_Components_Mail $mail): Log
{
$logEntry = new Log();

$logEntry->setSubject((string) iconv_mime_decode($mail->getSubject()));
$logEntry->setSubject((string) iconv_mime_decode((string) $mail->getSubject()));
$logEntry->setSender($mail->getFrom());
$logEntry->setSentAt(new DateTime((string) $mail->getDate()));
$logEntry->setContentText($mail->getPlainBodyText());
Expand Down
19 changes: 7 additions & 12 deletions engine/Shopware/Bundle/MailBundle/Service/LogEntryMailBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,30 @@
use RuntimeException;
use Shopware\Bundle\MediaBundle\MediaServiceInterface;
use Shopware\Models\Mail\Log;
use Shopware\Models\Order\Document\Document;

class LogEntryMailBuilder implements LogEntryMailBuilderInterface
{
public const INVALID_SENDER_REPLACEMENT_ADDRESS = '[email protected]';

/**
* @var FilesystemInterface
*/
private $filesystem;
private FilesystemInterface $filesystem;

/**
* @var MediaServiceInterface
*/
private $mediaService;
private MediaServiceInterface $mediaService;

private Enlight_Components_Mail $mail;

public function __construct(FilesystemInterface $filesystem, MediaServiceInterface $mediaService)
public function __construct(FilesystemInterface $filesystem, MediaServiceInterface $mediaService, Enlight_Components_Mail $mail)
{
$this->filesystem = $filesystem;
$this->mediaService = $mediaService;
$this->mail = $mail;
}

/**
* {@inheritdoc}
*/
public function build(Log $entry): Enlight_Components_Mail
{
$mail = new Enlight_Components_Mail('UTF-8');
$mail = clone $this->mail;

try {
$mail->setFrom($entry->getSender());
Expand Down Expand Up @@ -104,7 +100,6 @@ protected function assignOrderDocuments(Log $logEntry, Enlight_Components_Mail $
return;
}

/** @var Document $document */
foreach ($logEntry->getDocuments() as $document) {
$filePath = sprintf('documents/%s.pdf', $document->getHash());
$fileName = sprintf('%s.pdf', $document->getType()->getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ public function factory(Container $container, Shopware_Components_Config $config

if (isset($options['charset'])) {
$defaultCharSet = $options['charset'];
} elseif (!empty($config->CharSet)) {
$defaultCharSet = $config->CharSet;
} elseif (!empty($config->get('CharSet'))) {
$defaultCharSet = $config->get('CharSet');
} else {
$defaultCharSet = null;
}

return new Enlight_Components_Mail($defaultCharSet);
return new Enlight_Components_Mail($defaultCharSet, $config->get('mailer_hostname') ?: null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ protected function setUp(): void
$this->entryBuilder = new LogEntryBuilder($this->getContainer()->get(ModelManager::class));
$this->mailBuilder = new LogEntryMailBuilder(
$this->getContainer()->get('shopware.filesystem.private'),
$this->getContainer()->get(MediaServiceInterface::class)
$this->getContainer()->get(MediaServiceInterface::class),
$this->getContainer()->get('mail')
);
}

Expand Down
1 change: 1 addition & 0 deletions tests/Functional/Bundle/MailBundle/LogRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ public function testUniqueConstraintIsCaseSensitive(): void
$firstMail = $this->createSimpleMail();
$secondMail = new Enlight_Components_Mail('UTF-8');

static::assertIsString($firstMail->getSubject());
$secondMail->setSubject($firstMail->getSubject());
$secondMail->setFrom(ucfirst($firstMail->getFrom()));
$secondMail->setBodyText($firstMail->getBodyText()->getRawContent());
Expand Down
4 changes: 2 additions & 2 deletions tests/Functional/Components/MailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function testMessageId(): void
{
$mailTransport = $this->getContainer()->get('mailtransport');

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

Expand All @@ -46,7 +46,7 @@ public function testMessageId(): void
static::assertArrayHasKey('Message-Id', $headers);
$messageId = $headers['Message-Id'][0];
static::assertIsString($messageId);
static::assertStringContainsString('@', $messageId);
static::assertStringContainsString('@test.com', $messageId);
static::assertStringStartsWith('<', $messageId);
static::assertStringEndsWith('>', $messageId);
}
Expand Down

0 comments on commit cc87efc

Please sign in to comment.