完了時に添付ファイル付きのユーザーに電子メールを送信するWebアプリケーションを設定しようとしています。私は電子メールをHTMLとテキスト形式の両方に入れて、誰もが自分のメール設定に関係なくそれを見ることができるようにしたい。私はそれを動作させるのに苦労しているので、誰かが私のコードで問題を見つけることができるのだろうかと思っていました。HTML /テキストPHPを使用した添付ファイル付きの電子メール
私が使用しているコードは以下の通りです。これは別のサイトのテンプレートとして取られましたが、私は読んだことがたくさんありましたので、すべての仕組みを大まかに理解しています。残念ながら、問題を解決するのに十分な理解ではありません!
$firstname = $_POST['firstname'];
$email = $_POST['email'];
//define the receiver of the email
$to = $email;
//define the subject of the email
$subject = "$firstname, thanks for using the One Minute Leveller";
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: The Xenon Group\r\nReply-To: [email protected]";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('Level3info.pdf')));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
TEXT E-MAIL GOES HERE. ACTUAL CONTENT REMOVED
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<html><h1>HTML CONTENT GOES HERE. ACTUAL CONTENT REMOVED</h1></html>
--PHP-alt-<?php echo $random_hash; ?>--
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/pdf; name="Level3info.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--
<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
mail($to, $subject, $message, $headers);
誰でも手伝いできますか?
私はこのようにPHPを飛び出したり、飛ばしたりしないことをお勧めします。それはあなたのコードを読みにくくします。 – Travesty3
このようなタスクには、既存のクラスの使用を強くお勧めします。 http://pear.php.net/manual/en/package.mail.mail-mime.addattachment.php – powtac