如何转发PHP和IMAP下载的邮件?

问题描述 投票:0回答:3

我正在编写一个脚本来读取电子邮件并根据收件人地址将其分发到不同的电子邮件地址。所有电子邮件都发送到一个地址,但我似乎无法弄清楚如何转发电子邮件,或创建一封新电子邮件并包含所有相关的邮件正文。

现在我正在使用 IMAP 下载电子邮件并使用 imap_fetchbody 获取电子邮件内容;

$email_body = imap_fetchbody($imapconnex, $msgnumber, 1);

某些电子邮件正文无法正确显示,而另一些电子邮件正文包含末尾带有

=
符号的短行,导致内容无法正确显示。另外,我看到随机
A0
随机打印在我的电子邮件正文中。

我的计划是使用 PHP 的 mail() 函数将电子邮件发送到我想要的地方,并将发件人地址更改为真实的发件人地址。这可以满足我的需求,但我似乎无法弄清楚如何检索正确的正文、格式化并发送它。

这是我用来发送电子邮件的代码:

$header = imap_fetch_overview($imapconnex, $search[$i]);
$email_subject = $header[0]->subject;
$email_head = imap_fetchbody($imapconnex, $search[$i], 0); 
$email_body = imap_fetchbody($imapconnex, $search[$i], 1); 
mail("[email protected]", $email_subject, $email_body, $email_head);

标头似乎转发正常,但邮件正文仍显示

=
A0
符号。

php email imap forwarding
3个回答
2
投票

马克

很好的答案并提供了一个简单的解决方案。它还可以处理纯文本,并进行一些细微的更改。

$mtype = array('text', 'multipart', 'message', 'application', 'audio', 
               'image', 'video', 'model', 'other');
$mailstructure = imap_fetchstructure($mbox, $msgno, FT_UID); //
$type = $mailstructure->type;
if ($type == 1 && $mailstructure->ifparameters == 1) {
    $parameters = $mailstructure->parameters;
    $attribute = $parameters[0]->attribute;
    $value = $parameters[0]->value;
    echo $attribute . "//" . $value . "<br />";
}
# prepare the mail
$to="";
$subject="";
//   line below (may) not (be) needed
//   $body="\r\nThis is a multipart message in MIME format.\r\n";
$body = imap_body($inbox, $uid, FT_UID);
$headers ="From:" . $from . "\r\n";
$headers .="Date: " . date("r") . "\r\n";
$headers .="MIME-Version: 1.0\r\n";
$headers .="Content-Type: " . $mtype[$mailstructure->type] . '/'
         . strtolower($mailstructure->subtype) . ";\r\n";
if ($type == 1) { // multipart
    $headers .= "\t boundary=\"" . $value . "\""."\r\n";
}
$sendmail = imap_mail($to, $subject, $body, $headers);

1
投票

如今,大多数消息都是多部分的。我使用这个及其作品。

$mailstructure = imap_fetchstructure($inbox, $uid, FT_UID); //
$type = $mailstructure->type;
if ($type == 1 && $mailstructure->ifparameters == 1) {
    $subtype = strtolower($mailstructure->subtype);
    echo "SubType is" . $subtype . "<br />";
    $parameters = $mailstructure->parameters;
    $attribute = $parameters[0]->attribute;
    $value = $parameters[0]->value;
    echo $attribute . "//" . $value . "<br />";
    # prepare the mail
$to="";
$subject="";
    $body="\r\nThis is a multipart message in MIME format.\r\n";
    $body.=imap_body($inbox, $uid, FT_UID);
    $headers ="From:" . $from . "\r\n";
    $headers .="Date: " . date("r") . "\r\n";
    $headers .="MIME-Version: 1.0\r\n";
    $headers .="Content-Type: multipart/" . $subtype . ";
        \t boundary=\"" . $value . "\""."\r\n";
   $sendmail = imap_mail($to, $subject, $body, $headers);

}


0
投票

我正在尝试使用 IMAP 接收电子邮件并使用一些邮件列表重定向它。不过,我想要发送的地址托管在其他地方,因此我想使用外部 SMTP 服务器来发送邮件。据我所知,imap_mail 只能使用本地主机上的 SMTP 服务器。因此,我想使用 phpMailer 发送我使用 php_IMAP 检索到的电子邮件。有没有一种简单的方法可以在整个邮件正文(html/纯文本/附件等)不变的情况下转发?我真的必须解析整个消息正文然后重建它吗?如果是这样,我只是在寻找 html、纯文本部分和附件吗?还是我遗漏了一些东西?

© www.soinside.com 2019 - 2024. All rights reserved.