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条)

报告相同问题?

悬赏问题

  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题