PHP邮件格式

时间:2017-02-07 10:26:51

标签: php phpmailer

我首先要说的是我根本不是PHP编码器(我通常使用Coldfusion),但是我已经被要求修复这个问题。旧项目。

触发此操作的表单工作正常,电子邮件确实发送,但格式化已关闭。这是我们现在拥有的PHP(contact.php);

<?php

if ($_POST) {
$to_Email = "me@domain.com"; //Replace with recipient email address
$subject = 'Contact via Domain'; //Subject line for emails
//check if its an ajax request, exit if not
if (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {

    //exit script outputting json data
    $output = json_encode(
            array(
                'type' => 'error',
                'text' => 'Request must come from Ajax'
    ));

    die($output);
}

//check $_POST vars are set, exit if any missing
if (!isset($_POST["userName"]) || !isset($_POST["userEmail"]) || !isset($_POST["userTelephone"]) ||!isset($_POST["userMessage"])) {
    $output = json_encode(array('type' => 'error', 'text' => 'Input fields are empty!'));
    die($output);
}

//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_Message = filter_var($_POST["userMessage"], FILTER_SANITIZE_STRING);
$user_Telephone = filter_var($_POST["userTelephone"], FILTER_SANITIZE_STRING);

//additional php validation
if (strlen($user_Name) < 4) { // If length is less than 4 it will throw an HTTP error.
    $output = json_encode(array('type' => 'error', 'text' => 'Name is too short or empty!'));
    die($output);
}
if (!filter_var($user_Email, FILTER_VALIDATE_EMAIL)) { //email validation
    $output = json_encode(array('type' => 'error', 'text' => 'Please enter a valid email!'));
    die($output);
}
if (strlen($user_Message) < 5) { //check emtpy message
    $output = json_encode(array('type' => 'error', 'text' => 'Too short message! Please enter something.'));
    die($output);
}

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

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

if (!$sentMail) {
    $output = json_encode(array('type' => 'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
    die($output);
} else {
    $output = json_encode(array('type' => 'message', 'text' => 'Hi ' .     $user_Name . ' Thank you for your email'));
    die($output);
}
}
?>

发送的邮件是这样的;

tst 4 - 测试 - 123456

我需要做的是改变它,看起来像这样;

Name: tst 4
Email: sender@domain.com
Phone: 123456

Message: Test

电子邮件地址行并不重要,因为邮件已经通过&#39;来自&#39;。

这个PHP文件的设置和发送邮件的方式与我在网上找到的任何样本都不匹配,因此我被卡住了!

2 个答案:

答案 0 :(得分:2)

PHP mail()功能:

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

最后一个参数(标题)对于函数是可选的,但是发送HTML电子邮件是必需的,因为这是我们能够传递Content-Type声明的地方,告诉电子邮件客户端将电子邮件解析为HTML。

  

Content-Type:text / html;字符集= UTF-8

事实上,标题区域让我们有机会完成许多重要的电子邮件功能。这是我们可以设置发件人:和答复:设置,如果需要,以及CC和BCC其他收件人

<?php

if ($_POST) {
    $to_Email = "me@domain.com"; //Replace with recipient email address
    $subject = 'Contact via Domain'; //Subject line for emails
//check if its an ajax request, exit if not
    if (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {

        //exit script outputting json data
        $output = json_encode(
            array(
                'type' => 'error',
                'text' => 'Request must come from Ajax'
            ));

        die($output);
    }

//check $_POST vars are set, exit if any missing
    if (!isset($_POST["userName"]) || !isset($_POST["userEmail"]) || !isset($_POST["userTelephone"]) ||!isset($_POST["userMessage"])) {
        $output = json_encode(array('type' => 'error', 'text' => 'Input fields are empty!'));
        die($output);
    }

//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_Message = filter_var($_POST["userMessage"], FILTER_SANITIZE_STRING);
    $user_Telephone = filter_var($_POST["userTelephone"], FILTER_SANITIZE_STRING);

//additional php validation
    if (strlen($user_Name) < 4) { // If length is less than 4 it will throw an HTTP error.
        $output = json_encode(array('type' => 'error', 'text' => 'Name is too short or empty!'));
        die($output);
    }
    if (!filter_var($user_Email, FILTER_VALIDATE_EMAIL)) { //email validation
        $output = json_encode(array('type' => 'error', 'text' => 'Please enter a valid email!'));
        die($output);
    }
    if (strlen($user_Message) < 5) { //check emtpy message
        $output = json_encode(array('type' => 'error', 'text' => 'Too short message! Please enter something.'));
        die($output);
    }

//proceed with PHP email.
    $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\r\n';
    $msg    =   <<<MSG
Name: {$user_name} <br />
Email: {$user_Email} <br />
Phone: {$user_Telephone} <br /> <br />

Message: {$user_Message}

MSG;

    $sentMail = @mail($to_Email, $subject, $msg, $headers);

    if (!$sentMail) {
        $output = json_encode(array('type' => 'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
        die($output);
    } else {
        $output = json_encode(array('type' => 'message', 'text' => 'Hi ' .     $user_Name . ' Thank you for your email'));
        die($output);
    }
}
?>

答案 1 :(得分:1)

问题是您传递给mail()功能的第三个参数,即您的电子邮件的内容。目前,您只是提供一个连字符分隔列表,因此您需要创建一个类似于下面的新变量:

$email_content = "
Name: $user_Name
Email: $user_Email
Phone: $user_Telephone

Message: $user_Message";

然后将mail()来电更新为:

$sentMail = @mail($to_Email, $subject, $email_content, $headers);