如何使用PHP将HTML模板作为邮件发送?

时间:2022-06-04 06:38:51

I am trying to send a mail with html template. The mail was sent. but i can't find the design there in that mail. How to send the mail with the design.

我正在尝试使用html模板发送邮件。邮件已发送。但我在那封邮件中找不到那里的设计。如何使用设计发送邮件。

The mail should exactly look like what i view in my html template.

邮件应该看起来像我在html模板中查看的内容。

How to do this? kindly advice.

这该怎么做?好心劝告。

Thanks in advance.

提前致谢。

My code:

$subject = "Hi this is subject";
$email   = "x@x.com";
$message = "<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Super Qpon </title>
</head>
<link rel="stylesheet" href="../stylesheet/common.css" type="text/css">
<body>

<div class="mainbodycontainer">
<div class="gift">
<div class="gft-top"></div>
<div class="gft-bg">
<div class="">
<h1 class="title">Gift Coupon</h1>
<div class="toppm">
<div class="coupon">
<div class="cop-top"></div>
<div class="cop-bg">
<div>
<table width="165" align="right" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="81" class="t5">Coupon No :</td>
<td width="84">SQ548555</td>
</tr>
</table>
<div class="spacer"></div>
</div>
<div class="toppm">
<table width="500" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="99" height="30">From</td>
<td width="15" align="center">:</td>
<td width="386">test</td>
</tr>
<tr>
<td height="30">Amount</td>
<td align="center">:</td>
<td>$560</td>
</tr>
</table>
<div class="spacer"></div>
</div>
<div class="toppm">
<table width="165" align="right" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="81" class="t5">CODE NO :</td>
<td width="84">SQ548555</td>
</tr>
</table>
</div>
<div class="spacer"></div>
</div>
<div class="cop-btm"></div>
<div class="spacer"></div>
</div>
<div class="spacer"></div>
</div>
<div class="spacer"></div>
</div>
<div class="spacer"></div>
</div>
<div class="gft-btm"></div>
<div class="spacer"></div>
</div>
<div class="spacer"></div>
</div>

</body>
</html>";


$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: SuperQpon <admin@superQpon.com>' . "\r\n";
mail($email,$subject,$message,$headers );

3 个解决方案

#1


2  

It would be more better if you define all your styles in the tag's attributes. like this (style="background-color:#aefd34;").

如果在标签的属性中定义所有样式会更好。像这样(style =“background-color:#aefd34;”)。

I faced the similar problem but when I defined the CSS in the style attribute all went fine.

我遇到了类似的问题,但是当我在style属性中定义CSS时,一切都很顺利。

#2


1  

Your problem may lie here:

你的问题可能在于:

<link rel="stylesheet" href="../stylesheet/common.css" type="text/css">

This stylesheet needs to be linked into the email if you want that to work. The relative path refers to where it lies on your computer/server - which will be inaccesible from the email when received.

如果您希望这样做,则需要将此样式表链接到电子邮件中。相对路径指的是它在您的计算机/服务器上的位置 - 收到时将无法从电子邮件中找到。

EDIT

You can simplify your life a lot by using http://postageapp.com/

使用http://postageapp.com/可以大大简化您的生活

#3


0  

You can use any templating system you like (e.g. Smarty) to produce email content, though you need to be aware that email is not the web, and differences between email clients' HTML rendering are far greater than web browsers. Take a look at www.email-standards.org for more detail on that. I suggest you use a library to send your rendered messages though, such as PHPMailer, SwiftMailer, Zend_Mail etc. To do automated conversion from linked to inline styles (plus critique of email compatibility of your content), take a look at premailer

您可以使用您喜欢的任何模板系统(例如Smarty)来制作电子邮件内容,但您需要注意电子邮件不是网络,电子邮件客户端的HTML呈现之间的差异远大于Web浏览器。请访问www.email-standards.org了解更多详细信息。我建议您使用库来发送您呈现的消息,例如PHPMailer,SwiftMailer,Zend_Mail等。要从链接到内联样式进行自动转换(加上对内容的电子邮件兼容性的批评),请查看预制器

#1


2  

It would be more better if you define all your styles in the tag's attributes. like this (style="background-color:#aefd34;").

如果在标签的属性中定义所有样式会更好。像这样(style =“background-color:#aefd34;”)。

I faced the similar problem but when I defined the CSS in the style attribute all went fine.

我遇到了类似的问题,但是当我在style属性中定义CSS时,一切都很顺利。

#2


1  

Your problem may lie here:

你的问题可能在于:

<link rel="stylesheet" href="../stylesheet/common.css" type="text/css">

This stylesheet needs to be linked into the email if you want that to work. The relative path refers to where it lies on your computer/server - which will be inaccesible from the email when received.

如果您希望这样做,则需要将此样式表链接到电子邮件中。相对路径指的是它在您的计算机/服务器上的位置 - 收到时将无法从电子邮件中找到。

EDIT

You can simplify your life a lot by using http://postageapp.com/

使用http://postageapp.com/可以大大简化您的生活

#3


0  

You can use any templating system you like (e.g. Smarty) to produce email content, though you need to be aware that email is not the web, and differences between email clients' HTML rendering are far greater than web browsers. Take a look at www.email-standards.org for more detail on that. I suggest you use a library to send your rendered messages though, such as PHPMailer, SwiftMailer, Zend_Mail etc. To do automated conversion from linked to inline styles (plus critique of email compatibility of your content), take a look at premailer

您可以使用您喜欢的任何模板系统(例如Smarty)来制作电子邮件内容,但您需要注意电子邮件不是网络,电子邮件客户端的HTML呈现之间的差异远大于Web浏览器。请访问www.email-standards.org了解更多详细信息。我建议您使用库来发送您呈现的消息,例如PHPMailer,SwiftMailer,Zend_Mail等。要从链接到内联样式进行自动转换(加上对内容的电子邮件兼容性的批评),请查看预制器