views:

226

answers:

3

I'm trying to send an email to myself that has a layout and images. What I'm I doing wrong?

<?php
 $message = $_POST['message'];
 $emailsubject = 'site.com';
 $webMaster = '[email protected]';

 $body = "
 <html>
 <body bgcolor=\"e7e7e7\">
 <style type=\"text/css\">
#body {margin: auto;border: 0;padding: 0;font-family: Georgia, 'Times New Roman',      Times, serif;font-size: 12px;}
#emailHeader {width: 500px;height: 131px;background:      url(http://www.site.com/images/image.gif) no-repeat;}
#emailContent {width: 500px;background: url(http://www.site.com/images/image2.gif) repeat-y;text-align: left;padding: 0 33px 0 6px;}
#emailFooter {width: 500px;height: 34px;background:      url(http://www.site.com/images/image3.gif) no-repeat;}
 </style>
 <table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
 <tr>
 <td valign=\"top\" align=\"center\">
 <table width=\"500\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
 <tr>
 <td id=\"emailHeader\"></td>
 </tr>
 <tr>
 <td id=\"emailContent\">
 content $message
 </td>
 </tr>
 <tr>
 <td id=\"emailFooter\"></td>
 </tr>
 </table>
 </td>
 </tr>
 </table>
 </body>
 </html>"
 $headers .= "Content-type: text/html\r\n";
 $success = mail($webMaster, $emailsubject, $body, $headers);

 if ($success) {
    echo "Your message was sent.";
} 
 else{ 
    echo "There was a error.";
}
?>
+3  A: 

You should use phpmailer instead of PHP's mail()-Function. It allows you to easily send HTML-Mails.

Besides that you can try to validate your HTML-Code to be compatible for emailing.

Best wishes, Fabian

halfdan
I will try that. Thanks.
PHPNooblet
+1  A: 

You have an error in your code:

WRONG

$headers .= "Content-type: text/html\r\n";

RIGHT

$headers = "Content-type: text/html\r\n";

The .= throws a parse error in PHP unless you previously set $headers somewhere else.

It also may depend on the email client you are testing with. Be sure to check out http://www.email-standards.org/ to check what your email client supports.

Doug Neiner
PHP is forgiving enough. You don't necessarily need to predefine it.
BalusC
A: 

You may also want to look into Zend_Mail from Zend Framework: http://framework.zend.com/manual/en/zend.mail.html

Would make dealing with headers, formats, MIME, etc. easier.

StasM