使用PHP脚本转发电子邮件 [英] Forwarding emails with a PHP script

查看:169
本文介绍了使用PHP脚本转发电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个croned的PHP脚本,每十分钟检查收件箱。此脚本的目的是为我们提供的SMS通知服务处理停止退出功能。如果脚本在电子邮件开头发现有STOP字样的电子邮件,我们将从用户的通知数据库中删除。

We have a cron'ed PHP script that checks an inbox every ten minutes. The purpose of this script is to handle "STOP to quit" functionality for our SMS notification service we provide. If the script finds any emails with the word "STOP" at the beginning of the email, we remove the user from our notification database.

为了覆盖我们的基础,喜欢任何不符合上述条件的电子邮件转发到另一个电子邮件地址(这是一个别名),几个人收到和检查小时。但是,我们遇到从PHP脚本转发电子邮件的问题。

To cover our bases, we'd like any emails that don't meet the above criteria to be forwarded on to another email address (which is an alias) that several people receive and check hourly. However, we're running into problems forwarding the emails from this PHP script.

了解邮件的功能PHP工作,很明显,我们需要在邮寄之前重新插入标题。但是,MIME多部分电子邮件总是作为文本大小发送,包括屏障和任何base64编码的附件。

Knowing how the mail function of PHP works, it's quite obvious we need to reinsert the headers before mailing. However, MIME multipart emails always get sent as a garble of text, including the barriers and any base64 encoded attachments.

有谁知道一个简单的方式来接收电子邮件并正确使用PHP脚本转发?

Does anyone know of a simple way to take an email message and forward it on properly using a PHP script?

我们正在使用内置于PHP 5的本机IMAP功能。我们还可以访问PEAR Mail模块。但是,我们无法通过搜索Google找到任何示例或人员进行类似的任务。

We're using the native IMAP functions built in to PHP 5. We also have access to the PEAR Mail module. However, we have been unable to find any examples or people doing similar tasks by searching Google.

推荐答案

之前使用IMAP将电子邮件解析为相应的部分:

I coded this method a long time ago to parse an email message into their appropriate parts using IMAP:

function Message_Parse($id)
{
    if (is_resource($this->connection))
    {
        $result = array
        (
            'text' => null,
            'html' => null,
            'attachments' => array(),
        );

        $structure = imap_fetchstructure($this->connection, $id, FT_UID);

        if (array_key_exists('parts', $structure))
        {
            foreach ($structure->parts as $key => $part)
            {
                if (($part->type >= 2) || (($part->ifdisposition == 1) && ($part->disposition == 'ATTACHMENT')))
                {
                    $filename = null;

                    if ($part->ifparameters == 1)
                    {
                        $total_parameters = count($part->parameters);

                        for ($i = 0; $i < $total_parameters; $i++)
                        {
                            if (($part->parameters[$i]->attribute == 'NAME') || ($part->parameters[$i]->attribute == 'FILENAME'))
                            {
                                $filename = $part->parameters[$i]->value;

                                break;
                            }
                        }

                        if (is_null($filename))
                        {
                            if ($part->ifdparameters == 1)
                            {
                                $total_dparameters = count($part->dparameters);

                                for ($i = 0; $i < $total_dparameters; $i++)
                                {
                                    if (($part->dparameters[$i]->attribute == 'NAME') || ($part->dparameters[$i]->attribute == 'FILENAME'))
                                    {
                                        $filename = $part->dparameters[$i]->value;

                                        break;
                                    }
                                }
                            }
                        }
                    }

                    $result['attachments'][] = array
                    (
                        'filename' => $filename,
                        'content' => str_replace(array("\r", "\n"), '', trim(imap_fetchbody($this->connection, $id, ($key + 1), FT_UID))),
                    );
                }

                else
                {
                    if ($part->subtype == 'PLAIN')
                    {
                        $result['text'] = imap_fetchbody($this->connection, $id, ($key + 1), FT_UID);
                    }

                    else if ($part->subtype == 'HTML')
                    {
                        $result['html'] = imap_fetchbody($this->connection, $id, ($key + 1), FT_UID);
                    }

                    else
                    {
                        foreach ($part->parts as $alternative_key => $alternative_part)
                        {
                            if ($alternative_part->subtype == 'PLAIN')
                            {
                                echo '<h2>' . $alternative_part->subtype . ' ' . $alternative_part->encoding . '</h2>';

                                $result['text'] = imap_fetchbody($this->connection, $id, ($key + 1) . '.' . ($alternative_key + 1), FT_UID);
                            }

                            else if ($alternative_part->subtype == 'HTML')
                            {
                                echo '<h2>' . $alternative_part->subtype . ' ' . $alternative_part->encoding . '</h2>';

                                $result['html'] = imap_fetchbody($this->connection, $id, ($key + 1) . '.' . ($alternative_key + 1), FT_UID);
                            }
                        }
                    }
                }
            }
        }

        else
        {
            $result['text'] = imap_body($this->connection, $id, FT_UID);
        }

        $result['text'] = imap_qprint($result['text']);
        $result['html'] = imap_qprint(imap_8bit($result['html']));

        return $result;
    }

    return false;
}

我从来没有测试过,我确定它有一些错误,但它可能是一个开始...适应这段代码后,您应该可以使用 $ result indexes( text html 附件)与您的转发脚本(使用SwiftMailer),而不必担心保留MIME边界不变。

I've never tested it deeply and I'm sure it has some bugs, but it might be a start... After adapting this code you should be able to use the $result indexes (text, html, attachments) with your forwarding script (using SwiftMailer for instance), without worrying about keeping the MIME boundaries intact.

这篇关于使用PHP脚本转发电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆