[英]Form contact mail php in bold
我使用了這個非常簡單而出色的郵件聯系表: 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.';
}
}?>
有可能的。 通常,php mail函數以純文本形式發送其內容。 要將其發送為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);
第三個參數是您發送的消息。 由於我們添加了一個標題,用於通知將發送HTML文檔,因此可以使用HTML標簽。
為此,編輯$user_message
以包含HTML文檔。 例如,僅使所有文本$user_message = '<html><head></head><body><b>' . $user_message . '</b></body></html>'
粗體,請使$user_message = '<html><head></head><body><b>' . $user_message . '</b></body></html>'
$user_message = '<html><head></head><body><b>' . $user_message . '</b></body></html>'
$user_message = '<html><head></head><body><b>' . $user_message . '</b></body></html>'
。 添加Content-type: text/html
標頭時, $user_message
行為與網頁完全相同,幾乎可以使用所有html標簽。
您可以,但是您必須為此指定一些其他標題。
將以下內容添加到標題中:
$headers = "MIME-Version: 1.0\r\n
Content-type: text/html; charset=utf-8\r\n";
擁有字符集是可選的,但是在例如您的數據庫使用特定字符集的情況下非常有用。
希望這可以幫助。
@$sentMail = mail($to_Email, $subject, $user_Message .' -'.$user_Name, $headers);
改成
@$sentMail = mail($to_Email, $subject, "<strong>".$user_Message.'</strong> -'.$user_Name, $headers);
盡管用戶輸入的消息是您郵件正文中唯一出現的內容,但應該毫無意義
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.