处理 HTML 电子邮件模板文件和 php
我创建了一个 html 电子邮件 welcome.tpl
我必须使用什么 php 邮件方法才能将该文件作为消息正文发送出去?在此之前,我一直在使用并将 html 和文本包含在变量中,
$text_content.= "\r\n";
$text_content.= "--------------------------------\r\n";
$html_content.= "</body></html>";
$mime_boundary = 'Multipart_Boundary_x'.md5(time()).'x';
$headers = "MIME-Version: 1.0\r\n";
$headers.= "Content-Type: multipart/alternative; boundary=\"$mime_boundary\"\r\n";
$headers.= "Content-Transfer-Encoding: 7bit\r\n";
$body = "--$mime_boundary\n";
$body.= "Content-Type: text/plain; charset=\"charset=us-ascii\"\n";
$body.= "Content-Transfer-Encoding: 7bit\n\n";
$body.= $text_content;
$body.= "\n\n";
$body.= "--$mime_boundary\n";
$body.= "Content-Type: text/html; charset=\"UTF-8\"\n";
$body.= "Content-Transfer-Encoding: 7bit\n\n";
$body.= $html_content;
$body.= "\n\n";
$body.= "--$mime_boundary--\n";
$headers.= 'From: So <[email protected]>' . "\r\n";
$headers.= "X-Sender-IP: $_SERVER[SERVER_ADDR]\r\n";
$headers.= 'Date: '.date('n/d/Y g:i A')."\r\n";
$headers.= 'Reply-To: So <[email protected]>' . "\r\n";
mail($en['email'], $subject, $body, $headers);
我应该使用类似 $body = file_get_contents();
的东西,并且是 mail();
最好的方法?
I created a html email welcome.tpl
what php mail method do I have to use in order to send out that file as the body of the message? prior to this I've been using and including the html and text in variables
$text_content.= "\r\n";
$text_content.= "--------------------------------\r\n";
$html_content.= "</body></html>";
$mime_boundary = 'Multipart_Boundary_x'.md5(time()).'x';
$headers = "MIME-Version: 1.0\r\n";
$headers.= "Content-Type: multipart/alternative; boundary=\"$mime_boundary\"\r\n";
$headers.= "Content-Transfer-Encoding: 7bit\r\n";
$body = "--$mime_boundary\n";
$body.= "Content-Type: text/plain; charset=\"charset=us-ascii\"\n";
$body.= "Content-Transfer-Encoding: 7bit\n\n";
$body.= $text_content;
$body.= "\n\n";
$body.= "--$mime_boundary\n";
$body.= "Content-Type: text/html; charset=\"UTF-8\"\n";
$body.= "Content-Transfer-Encoding: 7bit\n\n";
$body.= $html_content;
$body.= "\n\n";
$body.= "--$mime_boundary--\n";
$headers.= 'From: So <[email protected]>' . "\r\n";
$headers.= "X-Sender-IP: $_SERVER[SERVER_ADDR]\r\n";
$headers.= 'Date: '.date('n/d/Y g:i A')."\r\n";
$headers.= 'Reply-To: So <[email protected]>' . "\r\n";
mail($en['email'], $subject, $body, $headers);
should i be using something like $body = file_get_contents();
and is mail();
the best method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议如下:
您在模板上使用 .tpl 扩展名,所以我假设您正在运行 Smarty 作为模板引擎?
如果没有,那么您可以简单地使用 file_get_contents();
只需使用 mail() 函数即可发送电子邮件。
另一种更可靠的方法是使用 Postmarkapp 之类的东西来发送电子邮件。它保证了 mail() 的投递可能最终出现在接收者的垃圾邮件文件夹中(特别是在共享托管环境上运行时)。
使用邮戳,您可以执行以下操作:
查看一些免费提供的 PHP-Postmark 类 http://developer.postmarkapp.com/developer-libs.html#php-5
I would suggest the following:
You are using a .tpl extension on your template, so I'm assuming you are running Smarty as a template engine?
If not, then you can simply use file_get_contents();
The simply use the mail() function to send off the email.
The alternative and somewhat more reliable way would be to use something like Postmarkapp to send emails. It guarantee's delivery where as mail() might end up in the receivers spam folder (especially if running on a shared hosting environment).
With postmark, you can do something like this:
Take a look at some of the freely available PHP-Postmark classes http://developer.postmarkapp.com/developer-libs.html#php-5
使用以下代码:
// 发送 HTML 电子邮件时始终设置内容类型
,您也可以包含该内容
// 更多标头
并使用
通过邮件功能发送邮件:
Use the following code :
// Always set content-type when sending HTML email
and you can also include that
// More headers
and use
and send the mail by mail function: