Skip to content

Commit d235c46

Browse files
AdityaGarg8gitster
authored andcommitted
send-email: retrieve Message-ID from outlook SMTP server
The script generates a Message-ID alongwith the other headers when gen_header is called, and is sent alongwith the email. For most email providers, including gmail, the Message-ID goes unchanged to the recipient. But, this does not seem to be a case with Outlook. In Outlook, when we send our own Message-ID as a part of the headers, it discards it. Then it generates a new random Message-ID and that is what the recipient gets. This is a problem because the Message-ID is crucial when we are sending multiple emails in a thread. The current implementation for threads in the script replies to the Message-ID it generated, but due to Outlook's behavior, it is not the same as the one that the recipient got, thus breaking threads. So a need arises to retrieve the Message-ID from the server response and set it in the In-Reply-To and References email headers instead of using the self generated one for the purpose of replies. The $smtp->message variable in this script for outlook is something like this: 2.0.0 OK <Message-ID> [Hostname=Some-hostname] The Message-ID here is the one the recipient gets, rather than the one the script generated. This patch uses the fact above and retrieves the Message-ID from the server response. It then changes the value of the $message_id variable to the one received from the server. This value will be used when next and subsequent messages are sent as replies to the message, thus preserving the threading of the messages. Signed-off-by: Aditya Garg <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4bbb303 commit d235c46

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

git-send-email.perl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1574,6 +1574,11 @@ sub gen_header {
15741574
return ($recipients_ref, $to, $date, $gitversion, $cc, $ccline, $header);
15751575
}
15761576

1577+
sub is_outlook {
1578+
my ($host) = @_;
1579+
return ($host eq 'smtp.office365.com' || $host eq 'smtp-mail.outlook.com');
1580+
}
1581+
15771582
# Prepares the email, then asks the user what to do.
15781583
#
15791584
# If the user chooses to send the email, it's sent and 1 is returned.
@@ -1737,6 +1742,22 @@ sub send_message {
17371742
$smtp->datasend("$line") or die $smtp->message;
17381743
}
17391744
$smtp->dataend() or die $smtp->message;
1745+
1746+
# Outlook discards the Message-ID header we set while sending the email
1747+
# and generates a new random Message-ID. So in order to avoid breaking
1748+
# threads, we simply retrieve the Message-ID from the server response
1749+
# and assign it to the $message_id variable, which will then be
1750+
# assigned to $in_reply_to by the caller when the next message is sent
1751+
# as a response to this message.
1752+
if (is_outlook($smtp_server)) {
1753+
if ($smtp->message =~ /<([^>]+)>/) {
1754+
$message_id = "<$1>";
1755+
printf __("Outlook reassigned Message-ID to: %s\n"), $message_id;
1756+
} else {
1757+
warn __("Warning: Could not retrieve Message-ID from server response.\n");
1758+
}
1759+
}
1760+
17401761
$smtp->code =~ /250|200/ or die sprintf(__("Failed to send %s\n"), $subject).$smtp->message;
17411762
}
17421763
if ($quiet) {

0 commit comments

Comments
 (0)