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); 第三个参数是你发送的消息。由于我们添加了一个标头,通知将发送 HTML 文档,因此我们可以使用 HTML 标记。

为此,请编辑$user_message以包含 HTML 文档。例如,要简单地将所有文本加粗,请使用 $user_message = '<html><head></head><body><b>' . $user_message . '</b></body></html>'. 添加Content-type: text/html标题时,其$user_message行为与网页完全一样,您几乎可以使用所有 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 回答
-1
@$sentMail = mail($to_Email, $subject, $user_Message .'  -'.$user_Name, $headers);

改成

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

应该这样做虽然没有意义,因为用户输入的消息是出现在您的邮件正文中的唯一内容

于 2013-08-29T13:43:05.003 回答