使用 PHP 脚本处理传入的电子邮件

发布于 2024-09-28 08:07:47 字数 272 浏览 6 评论 0原文

因此,我试图弄清楚如何将电子邮件发送到某个地址,例如 [email  ;protected],而不是将电子邮件发送到那里,而是将其发送或转发到我创建的脚本,以读取电子邮件的内容并将内容存储到数据库中。关于如何在 PHP 中做到这一点有什么建议吗?

谢谢!

So I'm trying to figure out how to send an email to an address for example, [email protected] and instead of the e-mail going to there it would be instead sent or forwarded to a script that I create to read the contents of the email and store the contents into a database. Any suggestions on how to do it in PHP?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

记忆里有你的影子 2024-10-05 08:07:47

您可以使用以下代码来开始:

// set user to check
$strUser     = "username";
$strPassword = "password";

// open
$hMail = imap_open ("{mail.yourdomain.com:143/notls}INBOX", "$strUser", "$strPassword");

// get headers
$aHeaders = imap_headers( $hMail );

// get message count
$objMail = imap_mailboxmsginfo( $hMail );

// process messages
for( $idxMsg = 1; $idxMsg <= $objMail->Nmsgs; $idxMsg++  )
{
    // get header info
    $objHeader = imap_headerinfo( $hMail, $idxMsg );

    // get from object array
    $aFrom = $objHeader->from;

    // process headers
    for( $idx = 0; $idx < count($aFrom); $idx++ )
    {
        // get object
        $objData = $aFrom[ $idx ];

        // get email from
        $strEmailFrom = $objData->mailbox . "@" . $objData->host;

        // do some stuff here
    }

    // delete message
    imap_delete( $hMail, $idxMsg );
}

// expunge deleted messages
imap_expunge( $hMail );

// close
imap_close( $hMail );

Here's some code you could use to get you going:

// set user to check
$strUser     = "username";
$strPassword = "password";

// open
$hMail = imap_open ("{mail.yourdomain.com:143/notls}INBOX", "$strUser", "$strPassword");

// get headers
$aHeaders = imap_headers( $hMail );

// get message count
$objMail = imap_mailboxmsginfo( $hMail );

// process messages
for( $idxMsg = 1; $idxMsg <= $objMail->Nmsgs; $idxMsg++  )
{
    // get header info
    $objHeader = imap_headerinfo( $hMail, $idxMsg );

    // get from object array
    $aFrom = $objHeader->from;

    // process headers
    for( $idx = 0; $idx < count($aFrom); $idx++ )
    {
        // get object
        $objData = $aFrom[ $idx ];

        // get email from
        $strEmailFrom = $objData->mailbox . "@" . $objData->host;

        // do some stuff here
    }

    // delete message
    imap_delete( $hMail, $idxMsg );
}

// expunge deleted messages
imap_expunge( $hMail );

// close
imap_close( $hMail );
清音悠歌 2024-10-05 08:07:47

有两个选项:

1) 如果您有权访问收件人电子邮件服务器上的 shell,则可以设置一条规则,以便在收到新邮件时触发脚本(例如通过 procmail 等)。

2) 您可以允许电子邮件像平常一样放入邮箱,然后编写 PHP 脚本通过 IMAP 访问邮箱,拉取新邮件,处理它们,然后删除它们。然后每隔几分钟通过 cron 运行一次。

Two options:

1) If you have access to the shell on the recipient email server, you can set up a rule to fire a script (like via procmail or the like) whenever a new message is received.

2) You can allow the email to drop into the mailbox as normal, then write your PHP script to access the mailbox via IMAP, pull the new messages, process them, then delete them. And then run the thing by cron every few minutes.

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