简体   繁体   English

在PHP中处理对多个收件人的传入邮件

[英]Handling Incoming Mail to Multiple Recipients in PHP

Alright, this may take a moment or two to explain: 好吧,这可能需要一两个时间来解释:

I'm working on creating an Email<>SMS Bridge (like Teleflip). 我正在努力创建一个电子邮件<> SMS Bridge(如Teleflip)。 I have a few set parameters to work in: 我有一些设置参数可供使用:

  • Dreamhost Webhosting Dreamhost虚拟主机
  • PHP 5 (without PEAR) PHP 5(不含PEAR)
  • Postfix 后缀
  • MySQL (If Needed) MySQL(如果需要)

What I have right now, is a catch-all email address that forwards the email sent to a shell account. 我现在拥有的是一个包罗万象的电子邮件地址,用于转发发送到shell帐户的电子邮件。 The shell account in turn forwards it to my PHP script. shell帐户又将它转发到我的PHP脚本。

The PHP script reads it, strips a few Email Headers in an effort to make sure it sends properly, then forwards it to the number specified as the recipient. PHP脚本读取它,剥离一些电子邮件标题以确保它正确发送,然后将其转发到指定为收件人的数字。 5551234567@sms.bridge.gvoms.com of course sends an SMS to +1 (555) 123-4567. 5551234567@sms.bridge.gvoms.com当然会发送短信至+1(555)123-4567。

This works really well, as I am parsing the To field and grabbing just the email address it is sending to. 这非常有效,因为我正在解析To字段并只抓取它发送到的电子邮件地址。 However, what I realized that I did not account for is multiple recipients. 但是,我意识到我没有考虑的是多个收件人。 For example, an email sent to both 5551234567 and 1235554567 (using the To line, the CC line, or any combination of those). 例如,发送到5551234567和1235554567的电子邮件(使用“收件人”行,“抄送”行或其中任何组合)。

The way email works of course, is I get two emails received, end up parsing each of them separately, and 5551234567 ends up getting the same message twice. 当然,电子邮件的工作方式是收到两封电子邮件,最后分别解析每封电子邮件,5551234567最终获得两次相同的邮件。

What is the best way to handle this situation, so that each number specified in TO and CC can get one copy of the message. 处理这种情况的最佳方法是什么,以便TO和CC中指定的每个数字都可以获得一个消息副本。

In addition, though I doubt its possible: Is there a way to handle BCC the same way? 此外,虽然我怀疑它的可能性:有没有办法以同样的方式处理BCC?

If you check the headers of the mail, you should find a Message-ID field (according to RFC2822 - section 3.6.4 ). 如果检查邮件的标题,则应找到Message-ID字段(根据RFC2822 - 第3.6.4节 )。 So you could test if you have already sent an SMS for a mail with the same Message-ID & phone number to prevent sending the same message to the same number twice. 因此,您可以测试是否已经为具有相同消息ID和电话号码的邮件发送了SMS,以防止将相同的消息发送到同一个号码两次。

Why not use something like imap to check the catch-all mailbox, loop through the messages and then delete them once finished? 为什么不使用像imap这样的东西来检查catch-all邮箱,遍历消息然后在完成后删除它们? That way you don't need to forward them to a seperate account. 这样您就不需要将它们转发到单独的帐户。

Stupid dirty solution: parse all recipients from the mail, then send them SMS, then put em all into temporary table with md5 of message text. 愚蠢的脏解决方案:解析邮件中的所有收件人,然后发送短信,然后将em全部放入临时表中,并使用md5的邮件文本。 And check all incoming mails against this table. 并根据此表检查所有传入的邮件。

Although wimvds had the best answer here, I found out elsewhere that Dreamhost includes a "X-DH-Original-To" header in the way I'm running it through the system. 尽管wimvds在这里得到了最好的答案,但我在其他地方发现,Dreamhost在我通过系统运行它的方式中包含了一个“X-DH-Original-To”标题。 Using this, I'm able to send to each number individually upon receipt of the email without checking it against a database. 使用此功能,我可以在收到电子邮件时单独发送给每个号码,而无需针对数据库进行检查。 This should also work with Blind Carbon Copy (I don't know the specifics of how email works enough to tell you how that works). 这也适用于Blind Carbon Copy(我不知道电子邮件如何工作的细节足以告诉你它是如何工作的)。

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

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