使用 PHP 发送电子邮件,Gmail 不起作用

Sending email with PHP, Gmail doesn't work

当我尝试通过 php 发送邮件时,GMail 不接受我的 html:

简单示例:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>

<body style="padding:0px; margin:0PX;" bgcolor="#dfdfdf">
<table width="100%" align="center" border="0" cellpadding="0" cellspacing="0" bgcolor=""  style="table-layout:fixed; margin:0 auto;">
<tr>
<td width="640" align="center" valign="top">

  Screenshot: <img src="data:image/jpeg;base64,someimginfo" />

</td>
</tr>
</table>
</body>
</html>

GMail 输出:

Gmail 编辑我的文本,例如<html> --> "<html>"

我用的headers:MIME-Version: 1.0&Content-type: text/html; charset=UTF-8

我做错了什么?

编辑 PHP代码:

<?php
$recipient = "xxxxxx@gmail.com";

$image = $_POST["image"];
$contact = $_POST["contact"];
$token = $_POST["token"];

if($token != "***"){
  exit('{"success":false, "error":"Invalid Token"}');
}

$from = (filter_var($contact, FILTER_VALIDATE_EMAIL)) ? $contact : 'no- reply@example.com';
$header = 'From: webmaster@example.com' . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";

$txt = '
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>

<body style="padding:0px; margin:0PX;" bgcolor="#dfdfdf">
<table width="100%" align="center" border="0" cellpadding="0" cellspacing="0" bgcolor=""  style="table-layout:fixed; margin:0 auto;">
<tr>
<td width="640" align="center" valign="top">

  Screenshot: <img src="data:image/jpeg;base64,' . $image . '" />

</td>
</tr>
</table>
</body>
</html>
';

if(mail($recipient, "APP Support request", $txt, $header)){
  exit('{"success":true}');
}

exit('{"success":false, "image":"' . $image . '"}');
?>

PHPmailer 是最适合 class 的工作! https://github.com/PHPMailer/PHPMailer

编辑:将 http://phpmailer.worxware.com/ 替换为 github 地址。

看起来问题出在这些行中:

$header = 'From: webmaster@example.com' . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";

// …

if(mail($recipient, "APP Support request", $txt, $header)){

您在名字 $header$headers 之间切换。因此,当您将 text/html 内容类型添加到变量 $headers 时,这不会影响实际用于发送电子邮件的 $header 变量。

您应该将所有这些变量切换为相同的名称,$headers