Mic*_*yen 8 php email mime-mail
我们有一个cron'ed PHP脚本,每十分钟检查一次收件箱.此脚本的目的是为我们提供的SMS通知服务处理"STOP to quit"功能.如果脚本在电子邮件的开头找到任何带有"STOP"字样的电子邮件,我们会从通知数据库中删除该用户.
为了覆盖我们的基础,我们希望将任何不符合上述标准的电子邮件转发到另一个电子邮件地址(这是一个别名),这是多个人每小时收到和检查的.但是,我们遇到了从这个PHP脚本转发电子邮件的问题.
知道mailPHP 的功能如何工作,很明显我们需要在邮寄之前重新插入标头.但是,MIME多部分电子邮件总是作为一堆文本发送,包括障碍和任何base64编码的附件.
有没有人知道一种简单的方法来接收电子邮件并使用PHP脚本正确转发它?
我们正在使用PHP 5内置的本机IMAP功能.我们还可以访问PEAR Mail模块.但是,我们无法通过搜索Google找到任何示例或执行类似任务的人员.
我很久以前编写了这种方法,使用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;
}
Run Code Online (Sandbox Code Playgroud)
我从来没有深入的检验它,我敢肯定,它有一些错误,但也可能是一个开始......适应此代码后,你应该能够使用$result索引(text,html,attachments与您的转发脚本)(使用SwiftMailer为实例),不必担心保持MIME边界不变.