Think before you speak, read before you think.

email的html排版

by

in

用程序生成好一个邮件标题和内容,之后想要完美的在收件箱里接收,需要编写针对于邮件的html。

美观可以放在第二位,但是需要让邮箱认出是html来,否则内容会以原码显示。

在邮件headers中加入正确的Content-type和charset即可。

email html中的布局基本是用table,css样式尽量直接写在html标签里,以下分别是用原生php和php-mail(SMTP)来发邮件。

<?php

$message='

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>title</title>
</head>
<body>
<table>
<tr>
<th>three days forcast</th>
</tr>
</table>
</body>
</html>';

$to = 'youmail@gmail.com';

$subject =send mail using php;
$from = "yourmail@163.com";
$headers = "From: $from\n";
$headers .= "Reply-To: $from\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1";

mail($to, $subject, $message,$headers);
echo "Mail Sent.";

?>

使用pear mail(SMTP)发送邮件

<?php

//require apt-get install php-mail, reference: http://php.net/manual/en/function.mail.php
include("Mail.php");

$message='

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>title</title>
</head>
<body>
<table>
<tr>
<th>three days forcast</th>
</tr>
</table>
</body>
</html>';

$to = 'youmail@gmail.com';

$subject =send mail using php;

$headers["From"] = "yourmail@163.com";
$headers["TO"] = "yourmail@gmail.com";
$headers["Subject"] = "$subject";
$headers["MIME-Version"] = "1.0";
$headers["Content-type"] = "text/html";
$headers["charset"] = "iso-8859-1";

$params["host"] = "smtp.163.com";
$params["port"] = "25";
$params["auth"] = true;
$params["username"] = "yourmail@163.com";
$params["password"] = "yourpassword";

$mail_object = & Mail::factory("smtp", $params);

$mail_object->send($to, $headers, $message);

//echo $message;
echo "Mail Sent.";

?>

参考:http://johndoesdesign.com/blog/2012/php/getting-your-php-html-email-to-render-in-gmail/

http://email.about.com/od/emailprogrammingtips/qt/PHP_Email_SMTP_Authentication.htm


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *