问题:
在使用linux进行邮件发送时,有时候希望发送html格式的邮件,如果发现发送的html格式邮件,可是邮箱里收到的还是纯txt格式的内容。
原因:
linux使用mail函数发送需要添加 header参数,将发送内容指定为txt/html
解决:
1. 使用命令行发送邮件测试
在linux命令行执行以下代码即可发送邮件
echo "<b><div style='color:red'>HTML Message goes here</div></b>" | mail -s "$(echo -e "This is the subject\nContent-Type: text/html")" 332800462@qq.com
<?php
$to = "somebody@example.com, somebodyelse@example.com";
$subject = "HTML email";
$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
</tr>
</table>
</body>
</html>
";
// 当发送 HTML 电子邮件时,请始终设置 content-type
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=utf8" . "\r\n";
// 更多报头
$headers .= 'From: <webmaster@example.com>' . "\r\n";
$headers .= 'Cc: myboss@example.com' . "\r\n";
mail($to,$subject,$message,$headers);
?>