将邮件通过管道传送到 PHP 时提取附件

发布于 2024-08-08 11:30:38 字数 307 浏览 1 评论 0原文

我知道通过我的 cPanel 托管,我可以将电子邮件收件箱传送到脚本,但我想要做的是:

  1. 发送到 [email protected]
  2. 管道到mail.php
  3. mail.php读取主题,以及.txt附件
  4. 主题和.txt附件的内容存储在数据库中

有没有办法用直接的 PHP 来做这个?

I know with my cPanel hosting I can pipe an email inbox to a script, but what I want to do is:

  1. Send to [email protected]
  2. Pipe to mail.php
  3. mail.php reads the subject, and a .txt attachment
  4. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぺ禁宫浮华殁 2024-08-15 11:30:38

有一个 PHP 库,名为 php-mime-mail-parser< /code>本身依赖于 PECL mailparse 库。安装这些后,实现您想要的功能的代码非常简单:

<?php
require_once 'MimeMailParser.class.php';

$parser = new MimeMailParser();
$parser->setStream(STDIN);

$subject = $parser->getHeader('subject');

$attachment_content = false;
foreach ($parser->getAttachments() as $attachment) {
    $extension = pathinfo($attachment->filename, PATHINFO_EXTENSION);
    if ($extension == "txt") {
        $attachment_content = $attachment->content;
        break;
    }
}

// adapt to what ever database you are using
$sth = $mysqli->prepare("INSERT INTO mails (subject, attachment) VALUES (:subject, :attachment)");
$sth->bindParam(':subject', $subject, PDO::PARAM_STR);
$sth->bindParam(':attachment', $attachment_content, PDO::PARAM_STR);
$sth->execute();

您可以将邮件通过管道传输到脚本中,因为它是从 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:

<?php
require_once 'MimeMailParser.class.php';

$parser = new MimeMailParser();
$parser->setStream(STDIN);

$subject = $parser->getHeader('subject');

$attachment_content = false;
foreach ($parser->getAttachments() as $attachment) {
    $extension = pathinfo($attachment->filename, PATHINFO_EXTENSION);
    if ($extension == "txt") {
        $attachment_content = $attachment->content;
        break;
    }
}

// adapt to what ever database you are using
$sth = $mysqli->prepare("INSERT INTO mails (subject, attachment) VALUES (:subject, :attachment)");
$sth->bindParam(':subject', $subject, PDO::PARAM_STR);
$sth->bindParam(':attachment', $attachment_content, PDO::PARAM_STR);
$sth->execute();

You can pipe the mail into the script, as it reads from STDIN. You can also read from a file by changing setStream to setPath. See the documentation of the library.

遗失的美好 2024-08-15 11:30:38

您可能需要执行以下操作:

  1. 编写可在 CLI 上执行的 PHP 脚本(通过在脚本顶部添加指向 PHP 二进制文件的 #! 声明,然后设置其可执行权限)。

  2. 获取该脚本以从 php://stdin 读取原始电子邮件(file_get_contents 最简单)

  3. 获取脚本将邮件解码为多个部分,使用类似 PEAR::Mail::Mime::Decode 的东西,或者我认为有一个方便的 Zend Framework 组件)。

  4. 从解码的消息中读取附件和主题,并按正常方式存储

  5. 最后的 exit(0) 告诉 CLI这是一次干净的退出 - 任何其他 exit() 状态都可能导致电子邮件退回。

You'll probably need do the following:

  1. 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).

  2. Get that script to read the raw email from php://stdin (file_get_contents is easiest)

  3. 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).

  4. Read the attachment and subject from the decoded message, and store as normal

  5. exit(0) at the end to tell the CLI that it was a clean exit - any other exit() status could cause a bounced email.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文