2013-08-29 36 views
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  = "[email protected]"; //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.'; 
} 
}?> 
+0

你需要,如果你想格式化文本 – Anigel

+0

我會完成發送HTML電子郵件:發送電子郵件作爲HTML和簡單的添加'粗體文字':) –

+0

見http://stackoverflow.com/a/ 10329132/1438393 –

回答

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); 第三個參數是消息你發送。由於我們添加了一個通知HTML文檔將被髮送的標頭,因此我們可以使用HTML標籤。

爲此,請編輯$user_message以包含HTML文檔。例如,要簡單地將所有文本設爲粗體,請使用 $user_message = '<html><head></head><body><b>' . $user_message . '</b></body></html>'。當您添加Content-type: text/html標頭時,$user_message的行爲與網頁完全相同,您幾乎可以使用所有的html標籤。

+0

感謝Stormblack,但是如何將消息內容以粗體轉換? – Bonjour

+0

看到這個:'@ $ sentMail = mail($ to_Email,$ subject,$ user_Message。' - '。$ user_Name,$ headers);' 在發生這種情況之前,編輯'$ user_message'來包含HTML文檔。例如,要簡單地將所有文本加粗,請製作 '$ user_message =''。 $ user_message。 '''。當你添加Content-type:text/html頭時,'$ user_message'的行爲與網頁完全相同,你幾乎可以使用所有的html標籤。 – SteveB

+0

再次感謝您的寶貴幫助!但是這個信息並沒有出現在我的電子郵箱裏,這段代碼沒問題:? '@ $已經寄出郵件=($ to_Email,$主題,$ USER_MESSAGE = '' $ USER_MESSAGE '' .'By:」 $ USER_NAME,$頭);' – Bonjour

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

變化

@$sentMail = mail($to_Email, $subject, "<strong>".$user_Message.'</strong> -'.$user_Name, $headers); 

應該這樣做雖然因爲用戶輸入的消息是沒有意義的是,出現在郵件正文反正

唯一
0

你可以,但你必須指定一些額外的標題。

以下添加到您的標題:

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

具有字符集有可選的,但如果您例如數據庫使用特定的字符集是非常有用的。

希望這會有所幫助。

相關問題