简体   繁体   English

知道电子邮件是否是使用PHP中的IMAP回复的

[英]Know if email was a reply using IMAP in PHP

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. 我正在创建一个cron作业,定期运行以检查是否有任何回复。 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 ]. 在过去,我会发送主题中有id的电子邮件(你有一个新的回复[1234]),然后检查主题在[和]之间的东西。 Then I would grab all the message and store it since every web browser/email uses a different character or style for quoted-text. 然后我会抓取所有消息并存储它,因为每个Web浏览器/电子邮件使用不同的字符或样式的引用文本。 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. 如果不可能,我会继续做我正在做的事情。

You can know if an email is the reply of another email or not by using the combination of In-Reply-To and References . 您可以使用In-Reply-ToReferences的组合来确定电子邮件是否是其他电子邮件In-Reply-To

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. 每封电子邮件在其标题中都有一个名为Message-ID的唯一ID,根据此RFC 1 ,您可以跟踪任何电子邮件的祖先。

I have checked it and it is working in all clients (Outlook, Thunderbird) 我检查了它,它在所有客户端工作(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: 1-在您第一次发送的电子邮件的标题中,您(您的邮件服务器或代码中的您)发送了一个ID (Message-ID) ,如果您打开电子邮件的来源,您将在顶部看到它:

... // You (your code) send:
Message-ID: <1@your-domain-mandatory.com>    
...

You just need to keep this Message-ID in your program. 您只需要在程序中保留此Message-ID any subsequent reply will refer to this ID . 任何后续回复都将引用此ID

2- Client will reply email 1 to you. 2-客户将回复电子邮件1给您。 Client will send a crucial header for you to tell you for which email this reply is in addition to its own Message-ID . 客户端将发送一个crucial header ,告诉您除了自己的Message-ID之外,此回复的电子邮件。

... // Client(Thunderbird) send:
Message-ID: <2@your-domain-mandatory.com>    
In-Reply-To: <1@your-domain-mandatory.com>
...

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). 当您收到第二封电子邮件时,您可以轻松跟踪之前发送的电子邮件,因为邮件ID (1)位于邮件的In-Reply-To标题中(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. 3-如果你想在你的代码中再次回复这封电子邮件,你只需要将Message-ID (2)放入In-Reply-To标题和Message-ID (1)和邮件(2) )在References标题中。 So the client will understand the chain correctly. 因此客户端将正确理解链。

... // You (your code) send:
Message-ID: <3@your-domain-mandatory.com>
In-Reply-To: <2@your-domain-mandatory.com>
References: <1@your-domain-mandatory.com> <2@your-domain-mandatory.com>   
...

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). 通过此标题,您告诉客户端此电子邮件是对邮件(2)的回复,而祖先是邮件(1)和邮件(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) (我们正在运行自己的票务系统,我们为每封电子邮件创建评论)

Unfortunately, e-mail clients can essentially do whatever they want with your message, and there is no reliable standard for determining how a received message originated at the client. 遗憾的是,电子邮件客户端基本上可以根据您的消息做任何他们想做的事情,并且没有可靠的标准来确定收到的消息是如何在客户端发起的。 In addition, IMAP doesn't really have anything to do with it. 此外,IMAP并没有真正与它有任何关系。 E-mails can be sent a number of different ways, including webmail. 电子邮件可以通过多种不同方式发送,包括网络邮件。

The best you can do is look for an ID number in the subject line (assuming folks don't change it, which they rarely do). 您可以做的最好的事情是在主题行中查找ID号(假设人们不会更改它,他们很少这样做)。 You can also do what Google does... fuzzy match the reply text to e-mail you sent to that address. 您还可以执行Google所做的操作...模糊地将回复文本与您发送到该地址的电子邮件相匹配。 If it matches, consider it part of the reply. 如果匹配,请将其视为回复的一部分。 This takes great effort though. 这需要付出很大的努力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM