在 PHP 中处理发送给多个收件人的传入邮件
好吧,这可能需要一两分钟的时间来解释:
我正在创建一个电子邮件<>短信桥(如 Teleflip)。我有一些设置参数可以使用:
- Dreamhost Webhosting
- PHP 5(不带 PEAR)
- Postfix
- MySQL(如果需要)
我现在拥有的是一个包罗万象的电子邮件地址,它将发送到 shell 帐户的电子邮件转发。 shell 帐户又将其转发到我的 PHP 脚本。
PHP 脚本读取该邮件,删除一些电子邮件标头以确保其正确发送,然后将其转发到指定的收件人号码。 [电子邮件受保护] 当然会发送短信至 +1 (5 55)123 -4567。
这非常有效,因为我正在解析“收件人”字段并仅获取其发送到的电子邮件地址。然而,我意识到我没有考虑到多个收件人。例如,发送到 5551234567 和 1235554567 的电子邮件(使用“收件人”行、“抄送”行或这些行的任意组合)。
当然,电子邮件的工作方式是,我收到两封电子邮件,最终分别解析每封电子邮件,并且 5551234567 最终收到相同的消息两次。
处理这种情况的最佳方法是什么,以便 TO 和 CC 中指定的每个号码都可以获得一份消息副本。
另外,虽然我怀疑它的可能性:有没有办法以同样的方式处理BCC?
Alright, this may take a moment or two to explain:
I'm working on creating an Email<>SMS Bridge (like Teleflip). I have a few set parameters to work in:
- Dreamhost Webhosting
- PHP 5 (without PEAR)
- Postfix
- MySQL (If Needed)
What I have right now, is a catch-all email address that forwards the email sent to a shell account. The shell account in turn forwards it to my PHP script.
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. [email protected] of course sends an SMS to +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. 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).
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.
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.
In addition, though I doubt its possible: Is there a way to handle BCC the same way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您检查邮件的标头,您应该会找到 Message-ID 字段(根据 RFC2822 -第 3.6.4 节)。因此,您可以测试您是否已经为具有相同消息 ID 和消息 ID 的邮件发送了短信。的电话号码,以防止向同一号码发送同一消息两次。
If you check the headers of the mail, you should find a Message-ID field (according to RFC2822 - section 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.
为什么不使用类似 imap 的东西来检查全部邮箱,循环遍历消息然后在完成后删除它们?这样您就不需要将它们转发到单独的帐户。
Why not use something like imap to check the catch-all mailbox, loop through the messages and then delete them once finished? That way you don't need to forward them to a seperate account.
愚蠢肮脏的解决方案:解析邮件中的所有收件人,然后向他们发送短信,然后将它们全部放入带有消息文本 md5 的临时表中。并对照此表检查所有传入邮件。
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. And check all incoming mails against this table.
虽然 wimvds 在这里有最好的答案,但我在其他地方发现 Dreamhost 在我通过系统运行它的方式中包含一个“X-DH-Original-To”标头。使用此功能,我可以在收到电子邮件后单独发送给每个号码,而无需对照数据库进行检查。这也应该适用于密件抄送(我不知道电子邮件如何工作的具体细节,不足以告诉您它是如何工作的)。
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. 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).