2012-08-24 20 views
5

)PHPを使用してかなり簡単なHTMLメールを送信しようとしています。私は最後の3日間、良い解決策を見つけようとしていましたが、私がそれを見つけたと思っていますが、私がそれをテストすると、正しく送信されません。私が参照していたチュートリアルの1つからこのコードを借りました。次のようにテストコードは次のとおりです。PHPメールでmultipart/alternativeを送信する(

<?php 
//define the receiver of the email 
$to = '[email protected]'; 
//define the subject of the email 
$subject = 'Test HTML email'; 
//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: [email protected]\r\nReply-To: [email protected]"; 
//add boundary string and mime type specification 
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"".$random_hash."\""; 
//define the body of the message. 
ob_start(); //Turn on output buffering 
?> 
--<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit 

Hello World!!! 
This is simple text email message. 

--<?php echo $random_hash; ?> 
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit 

<h2>Hello World!</h2> 
<p>This is something with <b>HTML</b> formatting.</p> 

--<?php echo $random_hash; ?>-- 
<? 
//copy current buffer contents into $message variable and delete current output buffer 
$message = ob_get_clean(); 
//send the email 
$mail_sent = @mail($to, $subject, $message, $headers); 
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed"; 
?> 

問題はそれだけで罰金、電子メールを送信しますが、それは、プレーンテキスト、またはGmailで空白のメッセージとして送信のどちらか、ということです。なぜどんなアイデア?

答えて

7

私はもちろん、このような迅速として、このためのライブラリを使用するためにあなたを伝える必要がありますが、私はそれと間違って何を教えてくれますので、私はこれは良い運動だと思います:)

あなたの行末ためです間違っている。特記のない限り、電子メールの行末はCRLF(\r\n)ですが、ob_start()ブロックはLF(\n)のみを行区切りとして使用する可能性があります。

これは、Gmailに電子メールメッセージを誤解させ、代わりに何も表示しません。私の場合は、空のダウンロードファイルを表示します;)

+0

空のダウンロードファイルは、まさに私が得ていたものでした!私は別のチュートリアルを探して最初からやり直すことになりました。しかし、迅速な返信をありがとう! – Bryan

+0

絶対に。図書館は不可欠です。自分のRFCに準拠した電子メールをPHPでまとめることは、私の最悪の敵に望むものではありません。あなたの電子メールが完全に準拠していない場合は、迷惑メールとして表示される可能性が非常に高いことがよくあります。それが間違っているリスクは単に高すぎます。 – tadman

+0

私は '\ r \ n'を使用していた問題がありました.GmailはOutlookにメッセージを表示しませんでした。私はここで反対を行い、それを修正するためだけに '\ n'を使いました。奇妙なことに、どこでもCRLFを使うと言われていたからです。 – James

関連する問題