Skip to content

Commit ff42643

Browse files
committed
Add Message-Id to SentMessage when sending an email
1 parent 3352304 commit ff42643

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

SentMessage.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class SentMessage
2222
private $original;
2323
private $raw;
2424
private $envelope;
25+
private $messageId;
2526
private $debug = '';
2627

2728
/**
@@ -31,9 +32,20 @@ public function __construct(RawMessage $message, Envelope $envelope)
3132
{
3233
$message->ensureValidity();
3334

34-
$this->raw = $message instanceof Message ? new RawMessage($message->toIterable()) : $message;
3535
$this->original = $message;
3636
$this->envelope = $envelope;
37+
38+
if ($message instanceof Message) {
39+
$message = clone $message;
40+
$headers = $message->getHeaders();
41+
if (!$headers->has('Message-ID')) {
42+
$headers->addIdHeader('Message-ID', $message->generateMessageId());
43+
}
44+
$this->messageId = $headers->get('Message-ID')->getId();
45+
$this->raw = new RawMessage($message->toIterable());
46+
} else {
47+
$this->raw = $message;
48+
}
3749
}
3850

3951
public function getMessage(): RawMessage
@@ -51,6 +63,16 @@ public function getEnvelope(): Envelope
5163
return $this->envelope;
5264
}
5365

66+
public function setMessageId(string $id): void
67+
{
68+
$this->messageId = $id;
69+
}
70+
71+
public function getMessageId(): string
72+
{
73+
return $this->messageId;
74+
}
75+
5476
public function getDebug(): string
5577
{
5678
return $this->debug;

Transport/AbstractApiTransport.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*/
2525
abstract class AbstractApiTransport extends AbstractHttpTransport
2626
{
27-
abstract protected function doSendApi(Email $email, Envelope $envelope): ResponseInterface;
27+
abstract protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $envelope): ResponseInterface;
2828

2929
protected function doSendHttp(SentMessage $message): ResponseInterface
3030
{
@@ -34,7 +34,7 @@ protected function doSendHttp(SentMessage $message): ResponseInterface
3434
throw new RuntimeException(sprintf('Unable to send message with the "%s" transport: %s', __CLASS__, $e->getMessage()), 0, $e);
3535
}
3636

37-
return $this->doSendApi($email, $message->getEnvelope());
37+
return $this->doSendApi($message, $email, $message->getEnvelope());
3838
}
3939

4040
protected function getRecipients(Email $email, Envelope $envelope): array

0 commit comments

Comments
 (0)