简体   繁体   English

PHP 邮件格式

[英]PHP Mail Formatting

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.我会说,我根本不是一个PHP编码器开始(我平时使用的ColdFusion),但我一直在负责解决这一问题的一个老项目。

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 (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);
}
}
?>

The mail that is sent comes through like this;发送的邮件是这样通过的;

tst 4 - Test - 123456 tst 4 - 测试 - 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!这个 PHP 文件的设置和邮件发送方式与我可以在网上找到的任何示例都不匹配,因此我被卡住了!

PHP mail() function: PHP邮件()函数:

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

The last parameter, the headers, are optional for the function but required for sending HTML email, as this is where we are able to pass along the Content-Type declaration telling email clients to parse the email as HTML.最后一个参数,标头,对于函数来说是可选的,但对于发送 HTML 电子邮件是必需的,因为这是我们能够传递 Content-Type 声明的地方,告诉电子邮件客户端将电子邮件解析为 HTML。

Content-Type: text/html;内容类型:文本/html; charset=UTF-8字符集=UTF-8

In fact, the headers area gives us the opportunity to do lots of important email functions.事实上,标题区域让我们有机会执行许多重要的电子邮件功能。 This is where we can set the From: and Reply To: settings if need be, as well as CC and BCC other recipients如果需要,我们可以在此处设置发件人:和回复:设置,以及抄送和密件抄送其他收件人

<?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);
    }
}
?>

The problem is the third parameter you are passing to the mail() function which is the content for your email.问题是您传递给mail()函数的第三个参数,它是您电子邮件的内容。 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:然后将您的mail()调用更新为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM