处理 HTML 电子邮件模板文件和 php

发布于 2024-12-22 00:27:31 字数 1476 浏览 0 评论 0原文

我创建了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恰似旧人归 2024-12-29 00:27:31

我建议如下:

您在模板上使用 .tpl 扩展名,所以我假设您正在运行 Smarty 作为模板引擎?

如果没有,那么您可以简单地使用 file_get_contents();

$template = file_get_contents('template.tpl');
$template = str_replace('{name}', 'Sean Nieuwoudt', $template);
$template = str_replace('{email}', '[email protected]', $template);
...
etc

只需使用 mail() 函数即可发送电子邮件。

另一种更可靠的方法是使用 Postmarkapp 之类的东西来发送电子邮件。它保证了 mail() 的投递可能最终出现在接收者的垃圾邮件文件夹中(特别是在共享托管环境上运行时)。

使用邮戳,您可以执行以下操作:

Mail_Postmark::compose()
    ->addTo('[email protected]', 'Jane Smith')
    ->subject('Subject')
    ->messagePlain($template)
    ->send();

查看一些免费提供的 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();

$template = file_get_contents('template.tpl');
$template = str_replace('{name}', 'Sean Nieuwoudt', $template);
$template = str_replace('{email}', '[email protected]', $template);
...
etc

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:

Mail_Postmark::compose()
    ->addTo('[email protected]', 'Jane Smith')
    ->subject('Subject')
    ->messagePlain($template)
    ->send();

Take a look at some of the freely available PHP-Postmark classes http://developer.postmarkapp.com/developer-libs.html#php-5

可是我不能没有你 2024-12-29 00:27:31

使用以下代码:

// 发送 HTML 电子邮件时始终设置内容类型

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

,您也可以包含该内容
// 更多标头

$headers .= 'From: <[email protected]>' . "\r\n";
$headers .= 'Cc: [email protected]' . "\r\n";

并使用

$body = file_get_contents();

通过邮件功能发送邮件:

mail($en['email'], $subject, $body, $headers);

Use the following code :

// Always set content-type when sending HTML email

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

and you can also include that
// More headers

$headers .= 'From: <[email protected]>' . "\r\n";
$headers .= 'Cc: [email protected]' . "\r\n";

and use

$body = file_get_contents();

and send the mail by mail function:

mail($en['email'], $subject, $body, $headers);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文