doumeng1143 2017-02-07 10:26
浏览 71
已采纳

PHP邮件格式

I'll start by saying that I'm not a PHP coder at all (I usually use Coldfusion), however I've been tasked to fix this for an old project.

The form that triggers this works fine, and the email does send, however the formatting is off. This is the PHP we have right now (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 . '' . "
" .
        'Reply-To: ' . $user_Email . '' . "
" .
        '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);
}
}
?>

The mail that is sent comes through like this;

tst 4 - Test - 123456

What I need to do is change it so it looks like this;

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

Message: Test

The email address line isn't critical as the mails already come through with that in the 'from'.

The way this PHP file is setup and mail is sent doesn't match up with any samples I can find online, hence me being stuck!

  • 写回答

2条回答 默认 最新

  • douben7493 2017-02-07 10:34
    关注

    The problem is the third parameter you are passing to the mail() function which is the content for your email. Currently, you are just providing a hyphen seperated list so you need to create a new variable that looks something like the below:

    $email_content = "
    Name: $user_Name
    Email: $user_Email
    Phone: $user_Telephone
    
    Message: $user_Message";
    

    Then update your mail() call to this:

    $sentMail = @mail($to_Email, $subject, $email_content, $headers);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式