[英]How to forward an email downloaded by PHP and IMAP?
I'm writing a script to read email and distribute it to different email addresses depending on the TO address.我正在编写一个脚本来读取电子邮件并根据收件人地址将其分发到不同的电子邮件地址。 All email comes to one address, but I cannot seem to figure out how to forward an email, or create a new email and include all of the relevant message body.
所有电子邮件都发送到一个地址,但我似乎无法弄清楚如何转发电子邮件,或创建新电子邮件并包含所有相关的邮件正文。
Right now I'm downloading the email using IMAP and using imap_fetchbody to get the email contents;现在我正在使用 IMAP 下载电子邮件并使用 imap_fetchbody 来获取电子邮件内容;
$email_body = imap_fetchbody($imapconnex, $msgnumber, 1);
Some email bodies don't display correctly, and others contain short lines with an =
symbol at the end, causing the content to not display correctly.某些电子邮件正文无法正确显示,而其他电子邮件正文包含末尾带有
=
符号的短行,从而导致内容无法正确显示。 Also, I'm seeing random A0
printed in my email bodies randomly.另外,我看到我的电子邮件正文中随机打印了随机
A0
。
My plan was to use PHP's mail() function to send the email to where I want it to go and change the From address to the real From address.我的计划是使用 PHP 的 mail() 函数将电子邮件发送到我希望它去的地方,并将发件人地址更改为真正的发件人地址。 That would work for my needs, but I cannot seem to figure out how to retrieve the correct body, format it and send it.
这将满足我的需求,但我似乎无法弄清楚如何检索正确的正文,对其进行格式化并发送。
Here's my code that I'm using to send the email:这是我用来发送电子邮件的代码:
$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);
The headers seem to be forwarding fine, but the main body of the message is still displaying with =
and A0
symbols.标题似乎可以正常转发,但消息的主体仍然显示为
=
和A0
符号。
Marc马克
Nice answer and provides a simple solution.很好的答案并提供了一个简单的解决方案。 It can also handle plain text with some minor changes.
它还可以通过一些小的更改处理纯文本。
$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);
Today the mostly messages are multipart.今天,大多数消息是多部分的。 I use this and its works.
我使用这个和它的作品。
$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);
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.