下面是一段摘自W3school关于php mail函数的栗子,经过测试发现两个问题。

<?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=iso-8859-1" . "\r\n";

// 更多报头
$headers .= 'From: <webmaster@example.com>' . "\r\n";
$headers .= 'Cc: myboss@example.com' . "\r\n";

mail($to,$subject,$message,$headers);
?>

首先是 编码问题。 测试发现中文乱码,改为 utf-8即可

第二是回车换行符的问题,经过测试发现,headers信息并没有生效,而是被原样输出了。经过谷歌找到原因将回车换行改为PHP_EOL

// 当发送 HTML 电子邮件时,请始终设置 content-type
$headers = "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type:text/html;charset=iso-8859-1" . PHP_EOL;

// 更多报头
$headers .= 'From: <webmaster@example.com>' . PHP_EOL;
$headers .= 'Cc: myboss@example.com' . PHP_EOL;

其实出现这个问题是因为windows下回车换行和linux下不一致导致的,PHP有内置解决方案。这样就搞定啦~

相关文章:

  • 2022-12-23
  • 2021-12-11
  • 2022-01-14
  • 2021-12-10
  • 2021-12-16
  • 2022-01-19
  • 2021-12-03
  • 2021-12-06
猜你喜欢
  • 2021-11-22
  • 2021-12-06
  • 2021-11-17
  • 2021-08-25
  • 2021-06-22
  • 2021-11-07
  • 2021-12-13
相关资源
相似解决方案