将邮件通过管道传送到 PHP 时提取附件
我知道通过我的 cPanel 托管,我可以将电子邮件收件箱传送到脚本,但我想要做的是:
- 发送到 [email protected]
- 管道到mail.php
- mail.php读取主题,以及.txt附件
- 主题和.txt附件的内容存储在数据库中
有没有办法用直接的 PHP 来做这个?
I know with my cPanel hosting I can pipe an email inbox to a script, but what I want to do is:
- Send to [email protected]
- Pipe to mail.php
- mail.php reads the subject, and a .txt attachment
- The contents of the subject and .txt attachment are stored in the database
Is there a way to do this with straight-forward PHP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一个 PHP 库,名为
php-mime-mail-parser< /code>
本身依赖于 PECL mailparse 库。安装这些后,实现您想要的功能的代码非常简单:
您可以将邮件通过管道传输到脚本中,因为它是从
STDIN
读取的。您还可以通过将setStream
更改为setPath
来读取文件。请参阅库文档。There exists a PHP library, called
php-mime-mail-parser
which itself depends on the PECL mailparse library. When you have those installed, the code to achieve what you want is quite straight forward:You can pipe the mail into the script, as it reads from
STDIN
. You can also read from a file by changingsetStream
tosetPath
. See the documentation of the library.您可能需要执行以下操作:
编写可在 CLI 上执行的 PHP 脚本(通过在脚本顶部添加指向 PHP 二进制文件的 #! 声明,然后设置其可执行权限)。
获取该脚本以从 php://stdin 读取原始电子邮件(file_get_contents 最简单)
获取脚本将邮件解码为多个部分,使用类似 PEAR::Mail::Mime::Decode 的东西,或者我认为有一个方便的 Zend Framework 组件)。
从解码的消息中读取附件和主题,并按正常方式存储
最后的 exit(0) 告诉 CLI这是一次干净的退出 - 任何其他 exit() 状态都可能导致电子邮件退回。
You'll probably need do the following:
Write a PHP script that's executable at the CLI (by adding a #! declaration at the top of the script that points to the PHP binary, then settings its executable permissions).
Get that script to read the raw email from php://stdin (file_get_contents is easiest)
Get that script to decode the mail in to parts, using something like PEAR::Mail::Mime::Decode or I think there's a handy Zend Framework component).
Read the attachment and subject from the decoded message, and store as normal
exit(0) at the end to tell the CLI that it was a clean exit - any other exit() status could cause a bounced email.