简体   繁体   中英

Bootstrap Contact Form - How to send html email

I am using a bootstrap contact form. The form works fine but whatever I do I cannot get it to send html emails. I have added headers and css inlined to different parts of the script but it never sends the email with any html formatting. I am banging my head against the wall and would really appreciate any help. Where do I put the headers variable and where do I put the html?

Here is the php code.

<?php
    if ($_POST["submit"]) {
        $name = $_POST['name'];
        $email = $_POST['email'];
                $message = $_POST['message'];
        $human = intval($_POST['human']);
        $from = 'Mywebsite'; 
        $to = 'me@email.com'; 
        $subject = 'Contact Email ';
$headers = "";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "To: ". $to. "\r\n";
$headers .= "From: ". $from;


         $body = " $headers, From: $name\n E-Mail: $email\n Message: $message";


        // Check if name has been entered
        if (!$_POST['name']) {
            $errName = 'Please enter your name';
        }

        // Check if email has been entered and is valid
        if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
            $errEmail = 'Please enter a valid email address';
        }

        //Check if content has been entered
        if (!$_POST['message']) {
            $errMessage = 'Please enter your message';
        }
        //Check if simple anti-bot test is correct
        if ($human !== 5) {
            $errHuman = 'Your anti-spam is incorrect';
        }

// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage) {
    if (mail ($to, $subject, $body, $from)) {
        $result='<div class="alert alert-success">Thank You! </div>';
    } else {
        $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
    }
}
    }
?>

And here is the HTML.

<div style="padding:10px; background:#000; font-size:46px; font-weight: 900; color:#fff; ">
        My Website
      </div>
      <div style="padding:24px; font-size:17px; background:#DCDCDC  ; ">
        This is a message from My website.
        <br>

        <br>
        <br>
        <a href="http://www.mywebsite.com">Login in to your account</a>
        <br>
        <br>
      </div>
      <br>
      <br>
    </div>

replace mail($to, $subject, $body, $from); with mail($to, $subject, $body, $headers);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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