0

我创建了一个小的 PHP 脚本,目的是转发 MIME 消息并添加一些文本。

使用下面的代码,它会成功发送带有添加文本的消息。我遇到的问题是消息被发送了两次,并且转发的消息在两条消息中都显示为附件(每个消息部分一个附件)。添加的文本确实显示在两条消息的消息本身中。我在想这意味着我的标题在某处有问题。

这是我的完整代码

功能 forwardmail($mailConnection, $messageIndex, $subject, $recipient, $newMessage, $fromAddr) {

/* Parameters:
    $mailConnection - result an executed imap_open command imap_open($email_mailbox_server."INBOX", $email_username, $email_password) or die('Cannot connect to email: ' . imap_last_error());
    $messageIndex - message index number of the message to be forwarded
    $subject - subject line to be used on the created message
    $recipient - email addr to send the message to
    $newMessage - Text to be included above the forwarded message
    $fromAddr - address to use as the from address on the created message
*/

$sourcestructure = imap_fetchstructure($mailConnection, $messageIndex); //
$type = $sourcestructure->type;

$subtype = strtolower($sourcestructure->subtype);
$parameters = $sourcestructure->parameters;
//$attribute = $parameters[0]->attribute;
$boundaryValue = $parameters[0]->value;
$myBoundary = "----=_NextPart_999_0BR2_JNED79D.52489";
// parts must start with --BoundaryValue followed by header followed by blank line

//new body will be inserted just before the first occurance of boundary value in the existing message body
//white space required after : 

$myEOL = "\r\n";

//my boundary is used in message header as main boundary to encapsulate original message
//start a new body section with the custom boundary, plain text to be added so no
//header content sections needed
$body2 = "--".$myBoundary.$myEOL.$myEOL.$newMessage.$myEOL.$myEOL;

//Start next section which encapsulates the original message
$body2.="--".$myBoundary.$myEOL; //new boundary for existing next section

//make new header using original sub type and boundary value from original message
$body2.="Content-Type: multipart/".$subtype.";".$myEOL;
$body2.="\tboundary=\"" . $boundaryValue . "\"".$myEOL;
$body2.="Content-Disposition: inline". $myEOL;
$body2.= $myEOL . $myEOL;

$body =imap_body($mailConnection, $messageIndex);

file_put_contents("body.txt", $body);   //output to a file for debugging

$firstBoundary = strpos($body,"--");      //find the first escape sequence 

$body = substr_replace($body,$body2,$firstBoundary,0);  //insert the new body just before the fist escape for the first bounday
$body.= "--".$myBoundary."--".$myEOL ;//make the final terminator boundary the modified boundary

file_put_contents("body2.txt",$body);   //output to a file for debugging

$headers ="MIME-Version: 1.0\r\n";
$headers .="From: " . $fromAddr . "\r\n";
$headers.="Reply-To: ".$fromAddr . "\r\n";
$headers .="Date: " . date("r") . "\r\n";
$headers .="Content-Type: multipart/mixed; ".$myEOL."\t boundary=\"" . $myBoundary . "\"".$myEOL . $myEOL;
$sendmail = imap_mail($recipient, $subject, $body, $headers);

我使用 WinMerge 比较了两个文件转储,以确保我没有更改现有消息中的任何内容。我还使用记事本++ 来查找控制字符等。我所有的行尾都有完整的 CR LF。每个标题部分后都有空行。

我确定我在这里遗漏了细微差别。有人介意指出我正确的方向吗?

肖恩

4

0 回答 0