-2

I'm trying to figure out a way to include HTML code in email body with this existing AJAX PHP Form Mailer I found on: http://blog.teamtreehouse.com/create-ajax-contact-form

When I add any of my own HTML coding into the email body, it just displays the tags.

Here's the Original Coding from 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.";
}

?>

2 Answers2

0

Modify your headers

$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";

Then in your message, include normal HTML tags

<html>
    <body>
        Message
    </body>
</html>
tdoggy
  • 196
  • 1
  • 9
0

Can you try using the below as a example and then work over your script.The one I am posting is what I had used for my project.

    $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);
Chetan_Vasudevan
  • 2,414
  • 1
  • 13
  • 34
  • 1
    Actually, it seems that you (may have) borrowed from [this answer](https://stackoverflow.com/a/11239069/1415724) in the duplicate the question was closed with and from the manual on the `mail()` function http://php.net/manual/en/function.mail.php – Funk Forty Niner Sep 08 '17 at 00:32