jvanasco
jvanasco

Reputation: 21

html email with php not displaying correctly

Tried looking on here but wasn't able to resolve me issue. Trying to use php to send an HTML email, the e-mail sends out but when I get the e-mail all it does is display the actual html code. I tried different header code but it still will not display correctly. Any help would be awesome! I a sure its pretty simple and I am just missing something easy.

<?php
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1";
    $headers = "From: [email protected]\n";
    $headers .= "Reply-To: " . $_POST["email"] . "\n";
    $headers .= "Return-path: " . $_POST["email"];
    $sendTo = "[email protected]";
    $subject = "Pex Heat Estimate Request from Customer {$_POST['custname']}-{$_POST['ProjectName']}:";
    $message = "Here is a your Copy of Pex Heat Estimate Request from Customer {$_POST['custname']}-{$_POST['ProjectName']}";
    $message .=  '<html><body><table>';
    foreach ($_POST as $key => $value) {
        if (!is_array($value)) {
            $message .= '<tr><td>' . $key . '</td><td>' . $value . '</td></tr>';
        }
        else {
            foreach ($_POST[$key] as $itemvalue) {
                $message .= '<tr><td>' . $key . '</td><td>' . $itemvalue . '</td></tr>';
            }
        }
    }
    $message .= '</body></table></html>';

    mail($sendTo, $subject, $message, $headers);
?>

Thanks to @fred for the fix! - I was now able to start adding html tags, etc. my last question is how to I add a : after $key - Just trying to format the e-mail a bit better to distinguish between the column with the label and the actual data on the right.

<?php
    $headers = "MIME-Version: 1.0\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1";
    $headers .= "From: [email protected]\n";
    $headers .= "Reply-To: " . $_POST["email"] . "\n";
    $headers .= "Return-path: " . $_POST["email"];
    $sendTo = "[email protected]";
    $subject = "Pex Heat Estimate Request from Customer {$_POST['custname']}-{$_POST['ProjectName']}:";
    $message = "Here is a your Copy of Pex Heat Estimate Request from Customer {$_POST['custname']}-{$_POST['ProjectName']}";
    $message .=  '<html><body><p>Thank you for submitting your Estimate Request with Pexheat.com : <u></u><u></u> Please remember to fax or email a floor plan diagram to us at 631-382-8225 or <a href="mailto:[email protected]" target="_blank">[email protected]</a>.<u></u><u></u></p>
<p>- Pexheat.com Staff</p><br></br><div>
  <center><p align="center"><h3><strong>Pexheat.com Estimate Request</strong></h3><u></u><u></u></p></center><br></br>
</div><table width="80%" align="center">';
    foreach ($_POST as $key => $value) {
        if (!is_array($value)) {
            $message .= '<tr><td>' . $key . '</td><td>' . $value . '</td></tr>';
        }
        else {
            foreach ($_POST[$key] as $itemvalue) {
                $message .= '<tr><td>' . $key . '</td><td>' . $itemvalue . '</td></tr>';
            }
        }
    }
    $message .= '</table></body><div><h5 align="center">Pexheat.com, 30 South Ave, Smithtown, NY 11787, Phone 631-240-9173, Fax 631-382-8225 | email: <a href="mailto:[email protected]" target="_blank">[email protected]</a>, Website: <a href="http://www.pexheat.com/" target="_blank">www.pexheat.com</a></h5></div></html>';

    mail($sendTo, $subject, $message, $headers);
?>

Upvotes: 1

Views: 2627

Answers (2)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

You have a (concatenate) dot missing in

$headers = "From: [email protected]\n";
          ^-- there

so do

$headers .= "From: [email protected]\n";

while deleting the one in

$headers .= "MIME-Version: 1.0\n";
         ^-- delete that

so

$headers = "MIME-Version: 1.0\n"; 

what is happening is that your headers are broken and that prevents the headers from being properly "chained".

When a (chain) link is broken, it's just "broken" and doesn't "work" anymore.

<?php
    $headers = "MIME-Version: 1.0\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1";
    $headers .= "From: [email protected]\n";
    $headers .= "Reply-To: " . $_POST["email"] . "\n";
    $headers .= "Return-path: " . $_POST["email"];
    $sendTo = "[email protected]";
    $subject = "Pex Heat Estimate Request from Customer {$_POST['custname']}-{$_POST['ProjectName']}:";
    $message = "Here is a your Copy of Pex Heat Estimate Request from Customer {$_POST['custname']}-{$_POST['ProjectName']}";
    $message .=  '<html><body><table>';
    foreach ($_POST as $key => $value) {
        if (!is_array($value)) {
            $message .= '<tr><td>' . $key . '</td><td>' . $value . '</td></tr>';
        }
        else {
            foreach ($_POST[$key] as $itemvalue) {
                $message .= '<tr><td>' . $key . '</td><td>' . $itemvalue . '</td></tr>';
            }
        }
    }
    $message .= '</body></table></html>';

    mail($sendTo, $subject, $message, $headers);
?>

Plus, as Marc stated in his comment, which I quote:

"Simple: Don't build your own mime emails. Use PHPMailer or Swiftmailer. They'll reduce that mess to just a few lines of actual mail-related code, and the rest is just you feeding in html."

Links:

Upvotes: 1

fortune
fortune

Reputation: 3372

Replace the code here

$message .= '</body></table></html>';

to

$message .= '</table></body></html>';

You need to close the table first and then body

Upvotes: 0

Related Questions