【问题标题】:How to forward an email downloaded by PHP and IMAP?如何转发通过 PHP 和 IMAP 下载的电子邮件?
【发布时间】:2023-03-20 07:24:02
【问题描述】:

我正在编写一个脚本来读取电子邮件并将其分发到不同的电子邮件地址,具体取决于收件人地址。所有电子邮件都发送到一个地址,但我似乎不知道如何转发电子邮件,或创建新电子邮件并包含所有相关的邮件正文。

现在我正在使用 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("me@mydomain.com", $email_subject, $email_body, $email_head);

标题似乎可以正常转发,但邮件的主体仍显示有=A0 符号。

【问题讨论】:

  • 听起来您正在将原始邮件的正文复制到新邮件中,但您缺少标题Content-Transfer-Encoding,这将导致您看到的=A0 症状。如果您要重新发送邮件,您可能应该从原始邮件中复制大部分(如果不是全部)标题。如果要转发,则应将原始邮件及其所有标头作为 MIME 正文部分完整地包含在新邮件中。
  • 感谢您的帮助。内容传输编码很有趣。
  • 我已经修复了我的代码标题(感谢 Celada),但在显示 =A0 的正文方面仍然存在问题。我注意到使用我的代码我没有在原始标题中得到Content-Transfer-Encoding
  • 我尝试将Content-Transfer-Encoding: 8bit 添加到我的标题中,但它仍然是相同的输出。
  • 如果你在消息的原始正文中有=A0 和类似的东西,你几乎可以肯定在原始消息中有一个Content-Transfer-Encoding,即quoted-printable。也许您没有得到原始标题的忠实版本。我忘记了下载原始电子邮件的 IMAP 语法,但也许您需要使用它而不是 imap_fetch_overview。您需要复制所有的相关标题(Content-TypeMIME-Version 等...)才能正常工作。事实上,最简单、最正确的做法就是复制原始电子邮件中的所有标头。

标签: php email imap forwarding


【解决方案1】:

马克

很好的答案并提供了一个简单的解决方案。它还可以处理一些小改动的纯文本。

$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);

【讨论】:

    【解决方案2】:

    今天大多数消息都是多部分的。我使用它及其作品。

    $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);
    

    }

    【讨论】:

      猜你喜欢
      • 2011-09-06
      • 1970-01-01
      • 2010-12-06
      • 2012-06-24
      • 2011-06-11
      • 2012-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多