2011-12-19 87 views
2

我创建了一个html电子邮件welcome.tpl我必须使用什么PHP邮件方法才能将该文件作为邮件正文发送出去?在此之前,我一直在使用,并包括变量处理HTML电子邮件模板文件和php

$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); 

HTML和文字,我应该使用类似$body = file_get_contents();,是mail();最好的方法是什么?

+0

的看看?你可以显示welcome.tpl和邮件功能的重要部分吗? – Jomoos 2011-12-19 11:21:07

+0

@Jomoos我已经添加了我目前的邮件()函数的片段...至于welcome.tpl它只是基本的HTML使用所需的电子邮件只能接受的HTML/CSS – acctman 2011-12-19 16:26:46

回答

2

使用下面的代码:

//发送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); 
2

我建议如下:

你正在使用你的模板.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_Postmark::compose() 
    ->addTo('[email protected]', 'Jane Smith') 
    ->subject('Subject') 
    ->messagePlain($template) 
    ->send(); 

一些免费提供的PHP-邮戳类http://developer.postmarkapp.com/developer-libs.html#php-5您正在使用哪个模板库