Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a mail application to recognize a Reply-To email php

I can't figure out how to get a mail application (except google mail) to recognize that an email was sent as a "Reply-To" and have those emails grouped together as one list of sent and replied emails.

For example, using php, if I use

$header = "From: Testing <[email protected]>\r\n" .
                "Reply-To: [email protected]\r\n" . 
                "X-Mailer: PHP/" . phpversion();

$to = "[email protected]";

$message = "This is a reply";
$subject = "test 123";  
$success = mail($to, $subject, $message, $header);


And send this twice, I get two separate emails. Instead of one email composed of the two emails.
Is there a way to have them both grouped together as one email replied to another, or am I doing something wrong?
I've read the php mail() documentation and multiple webpages explaining how php mail works and still can't get the emails to reply to each other.

Thank you for your time and help!

like image 715
jao Avatar asked Dec 27 '22 10:12

jao


2 Answers

Most mail clients handle threading by examining the Message-ID, In-Reply-To, and the References headers. In your first message, set a Message-ID header, then use the same value as the References and In-Reply-To headers. The mail clients should group them by placing locating the original Message-ID and matching it with messages having related References and In-Reply-To headers.

First message:

// Create a unique value for message id based on time, random value, and the hostname
$message_id = md5(time() . rand()) . $_SERVER['HTTP_HOST'];

// Use as a header when constructing the email
Message-Id: $message_id

Second message:

// Use the same value as these two headers when constructing the reply message.
References: $message_id
In-Reply-To: $message_id

// Also, you should set a new unique message-id for this one
$new_message_id = md5(time() . rand()) . $_SERVER['HTTP_HOST'];
Message-ID: $new_message_id;
like image 188
Michael Berkowski Avatar answered Jan 12 '23 17:01

Michael Berkowski


The reply-to header is used to indicate a reply should be sent to a different email address than the one in the from header.

I think Google employs an algorithm to group messages together if part of the message body contains text that was part of a previously sent or replied to message, or if the subject contains Re: and matches a subject from another group of messages. But the reply-to header probably has no effect on grouping messages as a reply.

like image 21
drew010 Avatar answered Jan 12 '23 16:01

drew010