MIME メッセージを転送してテキストを追加する目的で、小さな PHP スクリプトを作成しました。
以下のコードでは、テキストが追加されたメッセージが正常に転送されます。私が直面している問題は、メッセージが 2 回送信され、転送されたメッセージが両方のメッセージに添付ファイル (メッセージ部分ごとに 1 つの添付ファイル) として表示されることです。追加されたテキストは、両方のメッセージのメッセージ自体に表示されます。これは、ヘッダーのどこかに問題があることを意味すると考えています。
これは私の完全なコードです
関数 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 を使用して 2 つのファイル ダンプを比較し、既存のメッセージを変更していないことを確認しました。また、メモ帳++を使用して制御文字などを探しました。すべての行末にはCR LFがそのまま含まれています。すべてのヘッダー セクションの後に空白行があります。
ここでニュアンスが欠けていると確信しています。誰かが私を正しい方向に向けてくれませんか?
ショーン