添付ファイル付きのSMTPを使用して電子メールを送信するPHP関数を作成しようとしています。本文にHTM形式のテキストと添付するファイルを含めたいので、MIMEヘッダーを作成するのに苦労しています。非常に古いバージョンのPHP(4.3.8)を使用していますが、これが唯一の方法です。それは機能しています。PEARを試しましたが、SMTPを正しく認証しません。
これは私がこれまでに持っているものです:私は今メッセージを正しく受け取るようにこのコードを編集しましたが、ファイルは壊れています。ファイルをテキストファイルに変更したところ、最初は問題なく起動しましたが、ゴミのテキストがたくさん表示されます。
$newLine = "\r\n";
$attachment="myFile.zip";
$message = "<br><h1><center>TEST MESSAGE</center></h1>" ;
//Body
$headers .= "Content-Type: multipart/mixed;" . $newLine;
$headers .= " boundary=\"_e9e06aa5-1550-464d-ace4-e85b575d1899_\"" . $newLine . $newLine;
$newLine . $newLine;
$headers .= "--_e9e06aa5-1550-464d-ace4-e85b575d1899_" . $newLine;
$headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
$headers .= "Content-Transfer-Encoding: quoted-printable" . $newLine;
$headers .= " boundary=\"_7fdd1316-6f68-41bb-93f7-134933fc9aad_\"" . $newLine . $newLine;
$headers .= $message . $newLine;
//$headers .= "--_7fdd1316-6f68-41bb-93f7-134933fc9aad_--" . $newLine; // If I leave this line it appears in the end of the message area.
//Attachment
$headers .= "--_e9e06aa5-1550-464d-ace4-e85b575d1899_" . $newLine;
$headers .= "Content-Transfer-Encoding: base64" . $newLine;
$headers .= "Content-Type: text/plain;" . $newLine;
$headers .= "Content-Disposition: attachment;" . $newLine;
$headers .= " filename=" . $attachment . $newLine . $newLine;
$handle = fopen($attachment, "rb");
$contents = '';
while (!feof($handle)) {
$contents .= fread($handle, filesize($attachment));
}
fclose($handle);
$contents = base64_encode($contents);
$headers .= $contents . $newLine;
$headers .= "--_e9e06aa5-1550-464d-ace4-e85b575d1899_--" . $newLine;