1

Possible Duplicates:
Php mail: how to send html?
formatting email in php?

I'm using PHP to send emails, like this:

    $to = $emailAddress;
    $subject = "qwerty";
    $message = "foo" . PHP_EOL . "bar";                           
    $headers = 'From: "asdf" <email@email.com>';    
    mail($to, $subject, $message, $headers);

When I attempt to put HTML code within the message, it just sends the actual HTML as plaintext. This leads me to ask - how can I format emails as I can format webpages?

Community
  • 1
  • 1
AKor
  • 8,550
  • 27
  • 82
  • 136
  • See [example #4 for `mail()`](http://php.net/manual/en/function.mail.php#example-2843) in the PHP manual. – Marcel Apr 18 '11 at 05:03

3 Answers3

4

Make sure you're setting your headers for text/html, like so:

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

See Example #4 in the PHP Manual for the full example.

jamesmortensen
  • 33,636
  • 11
  • 99
  • 120
1

To add style to your email, you first need to add a header as mentioned by jmort253, like this:

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

Once you have added this to you header. You can do styling in the actual message, ie:

$message = "<html><body><h1>Welcome</h1><p>Thanks for the email</p></body></html>";

Make sure that you do inline CSS or put it in the header, don't put it in an external CSS file. As for images, you'll need to include full paths to your server, so that the client mail application can find the image on the web.

Hope this helps.

denislexic
  • 10,786
  • 23
  • 84
  • 128
0

I recommend not reinventing the wheel. Instead use phpmailer class or something similar. Will solve many other upcoming problems as well :)

cweinberger
  • 3,518
  • 1
  • 16
  • 31