Skip to content

Commit

Permalink
Add Message-Id to SentMessage when sending an email
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Oct 12, 2019
1 parent c7238f7 commit 592a01c
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ public function __construct(Headers $headers = null, AbstractPart $body = null)

public function __clone()
{
if (null !== $this->headers) {
$this->headers = clone $this->headers;
}
$this->headers = clone $this->headers;

if (null !== $this->body) {
$this->body = clone $this->body;
Expand Down Expand Up @@ -86,16 +84,12 @@ public function getPreparedHeaders(): Headers
}

// determine the "real" sender
$senders = $headers->get('From')->getAddresses();
$sender = $senders[0];
if ($headers->has('Sender')) {
$sender = $headers->get('Sender')->getAddress();
} elseif (\count($senders) > 1) {
$headers->addMailboxHeader('Sender', $sender);
if (!$headers->has('Sender') && \count($froms = $headers->get('From')->getAddresses()) > 1) {
$headers->addMailboxHeader('Sender', $froms[0]);
}

if (!$headers->has('Message-ID')) {
$headers->addIdHeader('Message-ID', $this->generateMessageId($sender->getAddress()));
$headers->addIdHeader('Message-ID', $this->generateMessageId());
}

// remove the Bcc field which should NOT be part of the sent message
Expand Down Expand Up @@ -132,9 +126,17 @@ public function ensureValidity()
parent::ensureValidity();
}

private function generateMessageId(string $email): string
public function generateMessageId(): string
{
return bin2hex(random_bytes(16)).strstr($email, '@');
if ($this->headers->has('Sender')) {
$sender = $this->headers->get('Sender')->getAddress();
} elseif ($this->headers->has('From')) {
$sender = $this->headers->get('From')->getAddresses()[0];
} else {
throw new LogicException('An email must have a "From" or a "Sender" header to compute a Messsage ID.');
}

return bin2hex(random_bytes(16)).strstr($sender->getAddress(), '@');
}

public function __serialize(): array
Expand Down

0 comments on commit 592a01c

Please sign in to comment.