0

I am able to send plain text emails from my contact form using php, but I am not able to send the content as HTML. Thought I had added the headers correctly, but apparently there is still a problem.

This is the script I am using:

<?php


    $to = '[email protected]'; 
    $subject = 'From your Web';
    $email = $_REQUEST['email'] ;


    if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
    //if "email" is filled out, send email
        if (!trim($_REQUEST['name']) == "" ) 
        {
            if (!trim($_REQUEST['message']) == "" ) 
            {

                //send email
                $name = $_REQUEST['name'] ;
                $message = $_REQUEST['message'] ;
                $mail = '
                    <html>
                        <body>
                             <table border=1>
                            <tr>
                                <td>Name:</td>
                                <td>'.$name.' </td>
                            </tr>
                            <tr>
                                <td>Email:</td>
                                <td>'.$email.'</td>
                            </tr>
                            <tr>
                                <td>Msg:</td>
                                <td>'.$message.'</td>
                            </tr>
                        </table>
                        </body>
                        </html>
                ';

                $headers  = 'MIME-Version: 1.0' . "\r\n";
                $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

                mail($to, $subject, $mail, "From:" . $email, $headers);
                //mail($to, $subject, "From: " . $name . "/" . $email . " Msg: ".$message, "From:" . $email);
                echo 'Thank you!';

            }
            else{

                echo 'No empty msg';

            }   
        }
        else{

            echo 'This is not a name';
        }
    }
    else{

        echo 'No correct email';
    }

    ?>
3
  • what does it do exactly...? Commented Jan 11, 2015 at 4:49
  • php site lists the format as... mail($to, $subject, $message, $headers); but it seems like... you have too many things. they include the from in their headers section... so... does that code error? or not? seems like it should error... with too many arguments for the mail function. but idk. Commented Jan 11, 2015 at 4:51
  • ohhh it wouldn't error... b/c there's option params, too. no error. yeah. so follow the format and your code should work. take out the "From:" . $email, part completely. test it. then add the from in your headers following the format on the documentation page. Commented Jan 11, 2015 at 4:55

1 Answer 1

2

try this. code is un-tested. and it's been eons since i've written in php so i could have easily mangled some syntax in there.

$tacos   = "crunchy";

$to      = "[email protected]"; 
$subject = "tacos"; 
$mail    = "the tacos are $tacos today."; 

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: [email protected]' . "\r\n";

$success = mail($to, $subject, $mail, $headers); 

if ($success) echo "tacos were sent successfully"; 
         else echo "tacos fell on the floor"; 

http://php.net/manual/en/function.mail.php

from the page...

When sending mail, the mail must contain a From header. This can be set with the additional_headers parameter, or a default can be set in php.ini.

<?php
$to      = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, to send email as HTML, you have to use the proper headers, otherwise best case, you'll just end up with a mangled email.
arguably... to do anything, you must use proper code or you'll probably get distorted results. or errors. usually errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.