且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

php邮件功能直接进入垃圾文件夹

更新时间:2023-02-22 10:53:41

编辑


这对我有用,但最终并没有出现在垃圾邮件/垃圾邮件中,而是在我的收件箱中。

Edit

This worked for me, and did not end up in SPAM/Junk, but in my Inbox.

<?php
//mail customer
$from = 'donotreply@mysite.co.uk';
$to = "email@example.com";
$subject = 'Your message has been received.';

$headers = "MIME-Version: 1.0\r\n"; 
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
$headers .= "From: The Sending Name <$from>\r\n";

$msg = '
<html>
<head>
<link href="http://linktocss/.../etc" rel="stylesheet" type="text/css" />

</head>
<body>
formatted message...
</body>
</html>
';
mail($to, $subject, $msg, $headers)or die("mail error");

?>

脚注::我注意到您使用的是外部样式表。许多电子邮件服务(例如Google)不会提供这些服务。***使用 内嵌样式 以获得更好/期望的结果。

Footnotes: I noticed that you are using an external stylesheet. Many Email services such as Google will not render those. It's best to use inline styling to achieve better/desired results.

最有可能)原因(按照您发布的代码),导致邮件最终以垃圾邮件结尾的原因是您的标题无效。

The (most likely) reason (as per your posted code) why your mail ends up in SPAM is that your headers are invalid.

您有 rn 捆在一起,而不是使用 \r\n

You have rn bunched up together, instead of using \r\n

请改用此:

$headers = "MIME-Version: 1.0\r\n"; 
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
$headers  .= "From: contact@mysite.com\r\n"; 
mail($to, $subject, $message, $headers)or die("mail error");

或更妙的是,使用:取自> $code> PHP网站的mail()函数

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers  .= "From: contact@mysite.com\r\n"; 
mail($to, $subject, $message, $headers)or die("mail error");