php 如何接收电子邮件并在 Web 应用程序中处理它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/965178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I receive email and process it in a web application
提问by Niyaz
I have set up an email id my PHP web application. Users will send emails to this id.
我已经为我的 PHP Web 应用程序设置了电子邮件 ID。用户将向此 ID 发送电子邮件。
I want to process these emails in the application. Ho do I go about doing this?
我想在应用程序中处理这些电子邮件。我该怎么做?
Thanks in advance.
提前致谢。
采纳答案by Shoan
I recently worked on a project that required parsing of email from gmail and updating database with certain values based on the contents of the email. I used the ezcMail(now) Zeta Componentslibrary to connect to the mail server and parse the emails.
我最近参与了一个项目,该项目需要解析来自 gmail 的电子邮件并根据电子邮件的内容使用某些值更新数据库。我使用ezcMail(现在)Zeta 组件库连接到邮件服务器并解析电子邮件。
The strategy I adopted was to filter all interesting incoming mail with a label "unprocessed". Run the PHP script via a crontab every 15 minutes. The script would connect to the mail server and open the IMAP unprocessed folder and parse each email. After inserting the interesting values into the database, the script moves the files to another IMAP folder "Proccessed".
我采用的策略是过滤所有带有“未处理”标签的有趣传入邮件。每 15 分钟通过 crontab 运行一次 PHP 脚本。该脚本将连接到邮件服务器并打开 IMAP 未处理文件夹并解析每封电子邮件。将感兴趣的值插入数据库后,脚本将文件移动到另一个 IMAP 文件夹“已处理”。
I also found IMAP to be better than POPfor this sort of processing.
我还发现IMAP 在这种处理方面比 POP 更好。
回答by Capitao
Recently I wanted to be able to receive emails immediately in something I was making so I did some research (I came looking on this question here too actually) and I ended up finding Google App Engine to be pretty helpful. It has an api you can use to receive and process emails sent to [email protected]. I know that it doesn't really seem helpful since you probably don't want your app on App Engine and you want to receive emails at yourdomain.tld, but with a little setup you can get what you want.
最近我希望能够在我正在制作的东西中立即收到电子邮件,所以我做了一些研究(我实际上也是在这里查看这个问题的),最终我发现 Google App Engine 非常有帮助。它有一个 api,您可以使用它来接收和处理发送到[email protected]. 我知道这看起来并没有什么帮助,因为您可能不希望在 App Engine 上使用您的应用程序并且您希望在 yourdomain.tld 上接收电子邮件,但是通过一些设置,您可以获得您想要的。
My basic setup is like this:
我的基本设置是这样的:
- User sends email to [email protected] (an email address that doesn't actually exist)
- mydomain.tld has a catchall email address that forwards to [email protected]
- GAEapp (a tiny app on app engine) receives the email, processes it out, and sends a post request with relevant stuff to mydomain.tld
- 用户发送电子邮件到 [email protected](一个实际上不存在的电子邮件地址)
- mydomain.tld 有一个通用的电子邮件地址,可以转发到 [email protected]
- GAEapp(应用引擎上的一个小应用)接收电子邮件,处理它,然后将包含相关内容的 post 请求发送到 mydomain.tld
So basically you can make a little GAE app that works like a go between to grab the emails. Even with the redirect it'll work out ok, the email will be fine.
所以基本上你可以制作一个小的 GAE 应用程序,它的工作原理就像是在抓取电子邮件之间的夹缝。即使使用重定向,它也会正常工作,电子邮件也可以。
Also I decided to learn me some django and I made a free app called Emailizationthat will basically do that for you. You create a recipient like [email protected]and give a URL to POST to. Anything sent to that address gets POSTed to you URL. You can make a catchall on your domain that forwards to that emailization recipient and you'll get email through the catchall too!
此外,我决定学习一些 django,并制作了一个名为Emailization的免费应用程序,它基本上可以为您做到这一点。您创建一个喜欢的收件人[email protected]并提供一个 POST 的 URL。发送到该地址的任何内容都会被发布到您的 URL。您可以在您的域上创建一个转发给该电子邮件收件人的包罗万象,您也将通过该包罗万象收到电子邮件!
or you can see a small GAE appI made that you can setup yourself that does the same thing.
或者你可以看到我制作的一个小型 GAE 应用程序,你可以设置自己做同样的事情。
Hope that helps somebody!
希望对某人有所帮助!
回答by bmb
Use procmail if it is installed on your system. Put these lines in a .procmailrc file in the home directory of the user who receives the e-mail.
如果您的系统上安装了 procmail,请使用它。将这些行放在接收电子邮件的用户的主目录中的 .procmailrc 文件中。
:0
| /path/to/your/script.php
Or you can also use a .forward file containing
或者您也可以使用包含的 .forward 文件
"|/path/to/your/script.php"
Procmail has the advantage that it allows you to deal with more complicated filtering if your application ever requires it.
Procmail 的优势在于它允许您在应用程序需要时处理更复杂的过滤。
Your script.php file will read the headers and body of the e-mail from stdin.
您的 script.php 文件将从标准输入读取电子邮件的标题和正文。
回答by ToughPal
回答by barbushin
There is a great library: Try this: http://code.google.com/p/php-imap
有一个很棒的库:试试这个:http: //code.google.com/p/php-imap
回答by Doug T.
You need to implement an email client in Php. This is probably going to be a POPclient.
您需要在 PHP 中实现一个电子邮件客户端。这可能是一个POP客户端。
This code would query the POP server containing your email, download it, and then you could parse it as needed.
此代码将查询包含您的电子邮件的 POP 服务器,下载它,然后您可以根据需要对其进行解析。
A quick google search of "POP client php" has revealed a vast array of different options. Its hard to tell if there's really "The One True PHP POP Library", otherwise I'd include it here. If you are using a preexisting framework, you may wish to check to see its level of POP support, otherwise check the google results above and take your pick. Or it may just be easiest (and most educational :) ) to roll your own.
在谷歌上快速搜索“POP 客户端 php”会发现大量不同的选项。很难说是否真的有“The One True PHP POP Library”,否则我会把它包括在这里。如果您使用的是预先存在的框架,您可能希望查看其 POP 支持级别,否则请查看上面的谷歌搜索结果并自行选择。或者它可能是最简单的(也是最有教育意义的:))自己动手。
回答by Travis Austin
There are a number of hosted solutions that will accept email for your domain and then post it a script on your website. Most of these will handle the parsing of the messages for you (separating the attachments, "to" "from" and other addresses, etc).
有许多托管解决方案可以接受您域的电子邮件,然后将其发布到您的网站上。其中大部分将为您处理消息的解析(分离附件、“收件人”“发件人”和其他地址等)。
You just create a script that receives a FORM POST and does whatever you need with it.
您只需创建一个接收 FORM POST 的脚本并使用它执行您需要的任何操作。
You can also look at Mandrill (by MailChimp), SendGrid, and PostMarkApp.
您还可以查看 Mandrill(由 MailChimp 提供)、SendGrid 和 PostMarkApp。
回答by Vern Jensen
There is a great tutorial for this here:
这里有一个很棒的教程:
http://www.evolt.org/incoming_mail_and_php
http://www.evolt.org/incoming_mail_and_php
which covers how to have the emails forwarded directly to your script, which your script reads via stdin (fopen, fread, etc.) The tutorial code even does basic parsing of the header/body for you.
它涵盖了如何将电子邮件直接转发到您的脚本,您的脚本通过 stdin(fopen、fread 等)读取该脚本。教程代码甚至会为您进行标题/正文的基本解析。
回答by Flolagale
Hosted solutions as Travis Austinsuggested work well.
Travis Austin建议的托管解决方案运行良好。
If you are looking for a self-hosted one, you can have a look at the Mailinmodule allows you to receive emails, parse them and post them to a webhook of your choice.It also checks the dkim and spf, computes a spamassassin score and determines the message language.
如果您正在寻找自托管的,您可以查看Mailin模块,该模块允许您接收电子邮件、解析它们并将它们发布到您选择的 webhook。它还检查 dkim 和 spf,计算 spamassassin 分数并确定消息语言。
I don't know if it will suit your needs since it is written in node.js, but the more options you have, the better. (Disclaimer: I am the maintainer of Mailin)
我不知道它是否适合您的需求,因为它是用 node.js 编写的,但是您拥有的选项越多越好。(免责声明:我是Mailin的维护者)

