简体   繁体   中英

Contact form HTML mail styling

I'm currently trying to style incoming emails that are sent via my self made php contact form.

There is only one slight problem, when i receive the e-mail in my inbox, the html is displayed as static text instead of code.

Anyone who can help me out?

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: info@epicconcepts.nl'; 
$to = 'info@epicconcepts.nl'; 
$subject = 'Contact formulier bericht';
$human = $_POST['human'];

$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
$headers .= "CC: quincynorbert@live.nl\r\n";
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

$message = '<!DOCTYPE html>
            <html>
            <head>
            <style type="text/css">
                body{background: #EDEBEA;}
                #naam{background:#222222;border:4px solid #DDD;width:650px;}
                #mail{background:#222222;border:4px solid #DDD;width:650px;}
                #message{background:#222222;border:4px solid #DDD;width:650px;}
            </style>
            </head>
            <body>
            <div id="naam">'.$name.'</div>
            <div id="mail">'.$email.'</div>
            <div id="message">'.$message.'</div>
            </body>
            </html>';

if ($_POST['submit']) {
    if ($name != '' && $email != '') {
        if ($human == '4') {                 
            if (mail ($to, $subject, $message, $from)) { 
            echo '<p class="correct-message">Your message has been sent!</p>';
        } else { 
            echo '<p class="correct-message">Something went wrong, go back and try again!</p>'; 
        } 
    } else if ($_POST['submit'] && $human != '4') {
        echo '<p class="correct-message">You answered the anti-spam question incorrectly!</p>';
    }
    } else {
        echo '<p class="correct-message">You need to fill in all required fields!</p>';
    }
}
?>

Thanks for in the advance

Your headers are contained in the string $headers but you don't pass that to the mail function, you are passing $from so the HTML and MIME headers are never sent which is why you get a plain text email:

if (mail ($to, $subject, $message, $from)) {
//                                 ^ should be $headers

Change to:

if (mail ($to, $subject, $message, $headers)) {

Also, your MIME header erases the current list of headers because you didn't include the concatenation . character, change to:

$headers .= 'MIME-Version: 1.0' . "\r\n";
//       ^ don't forget this

Finally, you should avoid using <style> CSS blocks in HTML emails, not all email clients will read CSS blocks, some will only read the CSS if it's defined inline with a style="" attribute directly on the element.

Edit: to hardcode your From header just use:

$headers = "From: info@mycompany.com\r\n";

Be sure to fix the concatenation error that I pointed out above, otherwise your From header will be overwritten and not sent.

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