0

私はこの非常にシンプルで優れたメール連絡フォームを使用しています: http://www.saaraan.com/2011/12/making-simple-jquery-ajax-contact-form 正常に動作します!

しかし、訪問者が書いたメッセージの内容をメールボックスに太字で表示したいと思います。

これは可能ですか?

私のphpコード:

<?php
if($_POST)
{
//check if its an ajax request, exit if not
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
    die();
} 

$to_Email       = "myemail@gmail.com"; //Replace with recipient email address
$subject        = 'Ah!! My email from Somebody out there...'; //Subject line for emails

//check $_POST vars are set, exit if any missing
if(!isset($_POST["userName"]) || !isset($_POST["userEmail"]) || !isset($_POST["userPhone"]) || !isset($_POST["userMessage"]))
{
    die();
}

//Sanitize input data using PHP filter_var().
$user_Name        = filter_var($_POST["userName"], FILTER_SANITIZE_STRING);
$user_Email       = filter_var($_POST["userEmail"], FILTER_SANITIZE_EMAIL);
$user_Phone       = filter_var($_POST["userPhone"], FILTER_SANITIZE_STRING);
$user_Message     = filter_var($_POST["userMessage"], FILTER_SANITIZE_STRING);

//additional php validation
if(strlen($user_Name)<4) // If length is less than 4 it will throw an HTTP error.
{
    header('HTTP/1.1 500 Name is too short or empty!');
    exit();
}
if(!filter_var($user_Email, FILTER_VALIDATE_EMAIL)) //email validation
{
    header('HTTP/1.1 500 Please enter a valid email!');
    exit();
}
if(!is_numeric($user_Phone)) //check entered data is numbers
{
    header('HTTP/1.1 500 Only numbers allowed in phone field');
    exit();
}
if(strlen($user_Message)<5) //check emtpy message
{
    header('HTTP/1.1 500 Too short message! Please enter something.');
    exit();
}

//proceed with PHP email.
$headers = 'From: '.$user_Email.'' . "rn" .
'Reply-To: '.$user_Email.'' . "rn" .
'X-Mailer: PHP/' . phpversion();

@$sentMail = mail($to_Email, $subject, $user_Message .'  -'.$user_Name, $headers);

if(!$sentMail)
{
    header('HTTP/1.1 500 Couldnot send mail! Sorry..');
    exit();
}else{
    echo 'Hi '.$user_Name .', Thank you for your email! ';
    echo 'Your email has already arrived in my Inbox, all I need to do is Check it.';
}
}?>
4

3 に答える 3

1

可能です。通常、php のメール機能は内容をプレーンテキスト形式で送信します。HTML マークアップを有効にする HTML として送信するには、次のようにします。

// To send HTML mail, the Content-type header must be set
$headers .= "\r\n" . 'Content-type: text/html; charset=iso-8859-1';
// I have changed the above to add new lines before the string, so you can add it before the mail function

これはhttp://php.net/manual/en/function.mail.phpからの抜粋です

もちろん、文字セットを utf-8 などの優先するものに変更する必要もあります。

これは次のいずれかで変更できます。

$headers = 'From: '.$user_Email.'' . "\r\n" .
'Reply-To: '.$user_Email.'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

...に:

$headers = 'From: '.$user_Email.'' . "\r\n" .
'Reply-To: '.$user_Email.'' . "\r\n" .
'X-Mailer: PHP/' . phpversion() . "\r\n" .
'Content-type: text/html; charset=utf-8'

コメントでの質問について:

これを参照してください: @$sentMail = mail($to_Email, $subject, $user_Message .' -'.$user_Name, $headers); 3 番目のパラメーターは、送信するメッセージです。HTMLドキュメントが送信されることを知らせるヘッダーを追加したので、HTMLタグを使用できます。

そのためには、編集$user_messageして HTML ドキュメントを含めます。たとえば、単純にすべてのテキストを太字にするには、make $user_message = '<html><head></head><body><b>' . $user_message . '</b></body></html>'. Content-type: text/htmlヘッダーを追加すると$user_message、Web ページとまったく同じように動作し、ほとんどすべての html タグを使用できます。

于 2013-08-29T13:45:34.260 に答える
0

できますが、そのためにいくつかの追加ヘッダーを指定する必要があります。

以下をヘッダーに追加します。

$headers = "MIME-Version: 1.0\r\n
Content-type: text/html; charset=utf-8\r\n";

文字セットを使用するかどうかはオプションですが、データベースで特定の文字セットを使用している場合などに非常に便利です。

お役に立てれば。

于 2013-08-29T13:44:54.597 に答える