从PHP邮件程序脚本将HTML添加到电子邮件正文中

时间:2017-09-07 20:22:24

标签: php html css ajax email

我正在尝试找到一种方法,使用我在How can I most accurately associate Google Cloud Platform project costs with App Engine activity?

上找到的现有AJAX PHP表单邮件程序将HTML代码包含在电子邮件正文中

当我将任何自己的HTML编码添加到电子邮件正文中时,它只显示标记。

以下是来自http://blog.teamtreehouse.com/create-ajax-contact-form

的原始编码
<?php
// My modifications to mailer script from:
// http://blog.teamtreehouse.com/create-ajax-contact-form
// Added input sanitizing to prevent injection

// Only process POST reqeusts.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get the form fields and remove whitespace.
    $name = strip_tags(trim($_POST["name"]));
            $name = str_replace(array("\r","\n"),array(" "," "),$name);
    $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
    $message = trim($_POST["message"]);

    // Check that data was sent to the mailer.
    if ( empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
        // Set a 400 (bad request) response code and exit.
        http_response_code(400);
        echo "Oops! There was a problem with your submission. Please complete the form and try again.";
        exit;
    }

    // Set the recipient email address.
    // FIXME: Update this to your desired email address.
    $recipient = "support@minutemenapparel.com";

    // Set the email subject.
    $subject = "New contact from $name";

    // Build the email content.
    $email_content = "Name: $name\n";
    $email_content .= "Email: $email\n\n";
    $email_content .= "Message:\n$message\n";

    // Build the email headers.
    $email_headers = "From: $name <$email>";

    // Send the email.
    if (mail($recipient, $subject, $email_content, $email_headers)) {
        // Set a 200 (okay) response code.
        http_response_code(200);
        echo "Thank You! Your message has been sent.";
    } else {
        // Set a 500 (internal server error) response code.
        http_response_code(500);
        echo "Oops! Something went wrong and we couldn't send your message.";
    }

} else {
    // Not a POST request, set a 403 (forbidden) response code.
    http_response_code(403);
    echo "There was a problem with your submission, please try again.";
}

&GT;

2 个答案:

答案 0 :(得分:0)

修改标题

$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
$headers .= "CC: susan@example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

然后在您的邮件中,包含普通的HTML标记

<html>
    <body>
        Message
    </body>
</html>

答案 1 :(得分:0)

你能尝试使用下面的例子,然后解决你的脚本。我发布的是我用于项目的那个。

    $to  = 'aidan@example.com' . ', '; // note the comma
    $to .= 'wez@example.com';

    // subject
    $subject = 'Adding html to php mail sender';

   // message
   $message = '
  <html>
  <head>
  <title>html code with php</title>
  </head>
  <body>
  <p>Here are the things to do</p>
  <table>
  <tr>
  <th>Person</th><th>Day</th><th>Month</th><th>Year</th>
  </tr>
  <tr>
  <td>xyz</td><td>3rd</td><td>September</td><td>2017</td>
  </tr>
  <tr>
  <td>Sally</td><td>17th</td><td>August</td><td>1973</td>
  </tr>
  </table>
  </body>
  </html>';

 // To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . 
"\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);