开发者

Sending emails with styling and formatting [duplicate]

This question already has answers here: Closed 11 years ago.

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?


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜