Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Know if email was a reply using IMAP in PHP

Tags:

php

email

imap

pop3

I am not sure if it is in the headers or not, but I am looking for a way to tell if an email I receive is a response to an email I sent, and if so, to only grab the new text, not "quoted text"

A little background: I am creating a script that will send out emails automatically. I am creating a cron job to run at periodic intervals to check to see if there were any replies. If there were replies, I only want to grab the new stuff, and not the old stuff.

In the past, I would send out emails with the id in the subject (You have a new response [1234]), and would then check the subject for the stuff in between the [ and ]. Then I would grab all the message and store it since every web browser/email uses a different character or style for quoted-text. Some do ">" some do a horizontal rule, some do absolutely nothing.

Anyways, I am just looking for something in the email header that would indicate they are replying and what the new text might be. If it's not possible, I will just keep on doing what I am doing.

like image 971
Tim Withers Avatar asked Dec 06 '22 18:12

Tim Withers


1 Answers

You can know if an email is the reply of another email or not by using the combination of In-Reply-To and References.

Every email has a unique ID in its header called Message-ID, according to this RFC 1, you can track the ancestors of any email.

I have checked it and it is working in all clients (Outlook, Thunderbird)
I will give an example to use.

1- In the header of the email you send for the first time, you (your mail server or you in code) send an ID (Message-ID), if you open source of the email you will see it like this in top section:

... // You (your code) send:
Message-ID: <[email protected]>    
...

You just need to keep this Message-ID in your program. any subsequent reply will refer to this ID.

2- Client will reply email 1 to you. Client will send a crucial header for you to tell you for which email this reply is in addition to its own Message-ID.

... // Client(Thunderbird) send:
Message-ID: <[email protected]>    
In-Reply-To: <[email protected]>
...

When you receive the second email, it will be easy for you to keep track of the previous email you have sent because the ID of mail(1) is in the In-Reply-To header of the mail(2).

3- if you want to reply back this email again inside your code, you just need to put the Message-ID of the mail(2) in In-Reply-To header and Message-ID of mail(1) and mail(2) in References header. So the client will understand the chain correctly.

... // You (your code) send:
Message-ID: <[email protected]>
In-Reply-To: <[email protected]>
References: <[email protected]> <[email protected]>   
...

By this header, you are telling the client that this email is a reply to the mail(2) and the ancestors are mail(1) and mail(2).

I have worked with them and read about them and it is working, my problem now is to just get the text of the last email and not the quoted text from the replies. (we are running our own Ticketing system, we create a comment for each email)

like image 52
Mohammad Eghlima Avatar answered Dec 10 '22 11:12

Mohammad Eghlima