2012-01-31 16 views
1

完了時に添付ファイル付きのユーザーに電子メールを送信する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); 

誰でも手伝いできますか?

+0

私はこのようにPHPを飛び出したり、飛ばしたりしないことをお勧めします。それはあなたのコードを読みにくくします。 – Travesty3

+0

このようなタスクには、既存のクラスの使用を強くお勧めします。 http://pear.php.net/manual/en/package.mail.mail-mime.addattachment.php – powtac

答えて

2

まず最初に、再発明していないホイール - 第二に、そのような梨メールとしてhttp://pear.php.net/package/Mail/redirected

をライブラリを使用し、あなたのヘッダーおよびその許可されていない間に空白があります。

メールRFCから:

コメントやFWSは 自由に挿入することができる。この標準でいくつかの場所があります。その構文に対応するために、コメントおよび/またはFWSが発生する場所に対して、「CFWS」の追加トークン が定義されています。 しかし、この規格でCFWSが発生する場合、折りたたまれたヘッダーフィールドの行がすべてWSP文字の で構成され、それ以外は何も構成されないように、 を挿入してはいけません(MUST NOT)。

敬具、

ジョン

+0

こんにちは、ありがとう、これに感謝 - 梨メールは何百万回も簡単に証明されました! – Chris

1

ここで私は働くことを使うものです。それは... HTMLバージョン対口座テキスト版にのみだけでHTMLバージョンを取ることはありませんが、おそらくそれは答えに向かってご案内します。

<?php 
    $mime_boundary = md5(uniqid(time())); 

    // to 
    $to = $_REQUEST["to"]; 
    if (is_array($to)) $to = implode(", ", $to); 

    // from 
    $header = "From:". ($_REQUEST["fromName"] == "" ? $_REQUEST["fromAddress"] : "{$_REQUEST["fromName"]} <{$_REQUEST["fromAddress"]}>") ."\r\n"; 
    $header .= "MIME-Version:1.0\r\n"; 
    $header .= "Content-Type:multipart/mixed; boundary=\"{$mime_boundary}\"\r\n\r\n"; 

    // message 
    $header .= "--{$mime_boundary}\r\n"; 
    $header .= "Content-Type:text/html; charset=\"ISO-8859-1\"\r\n"; 
    $header .= "Content-Transfer-Encoding:7bit\r\n\r\n"; 
    $header .= "<html><head><style type=\"text/css\">body { font:10pt Arial; }</style></head><body>". str_replace("\r", "", str_replace("\n", "<br />", $_REQUEST["message"])) ."</body></html>\r\n\r\n"; 

    // attachment 
    $attachments = (array)$_REQUEST["attachment"]; 
    $attachmentNames = (array)$_REQUEST["attachmentName"]; 
    for ($i = 0; $i < count($attachments); $i++) 
    { 
     if (empty($attachmentNames[$i])) 
      $attachmentNames[$i] = "attachment". ($i+1) .".txt"; 

     if (!base64_decode($attachments[$i], true)) 
      $attachments[$i] = base64_encode($attachments[$i]); 
     else 
      $attachments[$i] = str_replace("\r", "", str_replace("\n", "", str_replace(" ", "+", $attachments[$i]))); 

     $header .= "--{$mime_boundary}\r\n"; 
     $header .= "Content-Type:application/octet-stream; name=\"{$attachmentNames[$i]}\"\r\n"; 
     $header .= "Content-Transfer-Encoding:base64\r\n"; 
     $header .= "Content-Disposition:attachment; filename=\"{$attachmentNames[$i]}\"\r\n"; 
     $header .= "{$attachments[$i]}\r\n\r\n"; 
    } 

    if (mail($to, $_REQUEST["subject"], "", $header)) 
     echo "SUCCESS"; 
    else 
     echo "FAIL"; 
?> 
+0

返事をいただきありがとうございます。私はPEARメールライブラリを使用して実際に行ってきました。すべてを最初からやっているよりはるかに簡単で、テキスト/ HTMLと添付ファイルを入手できます!乾杯! – Chris

1

はたぶん、あなたはPHPmailerに見てみたいです。これは、使用したいすべての機能を提供しますが、build-in mail()関数よりもはるかにクリーンで、標準的な方法です。そこにいくつかの良いチュートリアルがあります。

+0

これはありがとうございました。私は実際にPEAR MailをJohn(上記)の提案通りに使いましたが、アイデアは同じです。すべてをゼロからコード化しようとすることを邪魔するよりもずっと簡単です! – Chris

関連する問題