如何使用邮件功能php发送格式正确的html邮件

时间:2016-09-18 19:14:33

标签: php html email

你好我试图在php中使用邮件功能发送电子邮件,从我的搜索iv读取发送html enabld电子邮件我们需要传递一个参数到标题,声明存在的html标签将被传递,我我正在使用这个

$headers .= "Content-Type: text/html; charset=UTF-8\r\n";

我已将此信息发送到我的邮件标题,但我的电子邮件中,我需要显示动态内容的表格和超链接未格式化,它是如此分散。

这是我的完整代码

<?php 
$dbhost = 'localhost'; 
$dbuser = '###'; 
$dbpass = '###'; 
$dbname = '###'; 

$conn = mysql_connect($dbhost, $dbuser, $dbpass); 
if(!$conn) { 
die('Failed to connect to server: ' . mysql_error()); 
} 
mysql_select_db($dbname); 

$sql="SELECT Insured_Name,Expiry_Date FROM transactions  
WHERE MONTH(Expiry_Date) = MONTH(CURDATE()) and YEAR(Expiry_Date) = YEAR(CURDATE())"; 
$result = mysql_query($sql)or die("Cannot query orders_products data" . mysql_error()); 



$email = "###############"; 
$emailto = "############"; 
$subject = "List of policies to expire this month"; 
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";

$headers = "From: $email"; 

// prepare email body text 
$body .= "Here Are A List Of policies to expire this month\n\n";
$body .= "<a href='expired.php'>Download complete list</a>\n\n";
$body .='<table width="200" border="1">';
$body .='<tr>';
 $body .='<td>Insured name</td>';
    $body .='<td>Expiry date</td>';
  $body .='</tr>';


while($row=mysql_fetch_array($result)) { 
$body .='<tr>';
 $body .='<td>'.$row['Insured_Name'] .'</td>';
 $body .='<td>'.$row['Expiry_Date'] .'</td>';

  $body .='</tr>';

} 
$body .='</table>';
$body .= "This is an automated mail from the KBL insurance portal\n\n";
// send email 
$send = mail($emailto, $subject, $body, $headers); 

mysql_close($conn); 

// redirect to success page 

?>

请问我在这里做错了什么吗?

1 个答案:

答案 0 :(得分:0)

来自PHP Manual

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";