1

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);
?>
7
  • 4
    You have a (concatenate) dot missing in $headers = "From: [email protected]\n"; so do $headers .= "From: [email protected]\n"; while deleting the one in $headers .= "MIME-Version: 1.0\n"; so $headers = "MIME-Version: 1.0\n"; Commented May 23, 2014 at 19:18
  • 4
    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. Commented May 23, 2014 at 19:18
  • 1
    Thanks @Fred-ii- that fixed my issue! I knew it would be something simple that I was missing. Commented May 23, 2014 at 19:35
  • "Thanks @fred that fixed my issue - I have another question now. How would I implement the following html code in my above PHP code?" - As per your edit, that would need to be another question. Your original question has been answered - Do not modify your original question and add something after the fact. Commented May 23, 2014 at 19:44
  • 1
    In response to your new question, yes. You should be able to add your additional markup between lines 13 and 14. You should probably post this as a new question though. Commented May 23, 2014 at 19:58

2 Answers 2

1

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:

Sign up to request clarification or add additional context in comments.

Comments

0

Replace the code here

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

to

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

You need to close the table first and then body

1 Comment

fred's comment is the reason it is not working as html. This is just correcting bad markup but it would still try to render.

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.