php 使用php邮件功能防止发送的电子邮件被视为垃圾邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/746809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:44:41  来源:igfitidea点击:

Prevent sent emails treated as junk mails using php mail function

phpheaderemailemail-spam

提问by Peter

I wrote a PHP script to send emails.

我写了一个 PHP 脚本来发送电子邮件。

My script is like this:

我的脚本是这样的:

$headers =  'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: [email protected]' . "\r\n";

// Email Variables
$toUser  = "[email protected]"; // recipient
$subject = "testing"; // subject
$body    = "<html><body><p>
             Example of including an image via html \<img\> tag:
             <br>
             <img src='../images/profile.jpg'>
             <br>
             My new picture
             <br></p></body></html>"; // content

if (mail($toUser,$subject,$body,$headers)) {
    echo "sent";
} else {
    echo "failed";
}

Well, of course I use a valid email address for sender and receiver. I did receive the email, but it goes to junk mail. So I went for google research. Is it because of my "header" script problem? If it isn't, then what could cause my script to send a junk mail? Any solution?

嗯,当然,我使用有效的电子邮件地址作为发件人和收件人。我确实收到了电子邮件,但它变成了垃圾邮件。所以我去谷歌研究。是因为我的“标题”脚本问题吗?如果不是,那么什么会导致我的脚本发送垃圾邮件?有什么解决办法吗?

回答by Anju

Please try this:

请试试这个:

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

mail($to,$subject,$body,$headers,"-f$from");

回答by Rad

Perhaps the problem is that yahoo uses domainkeysverification, which will likely fail for your application given that the mail is not actually coming from yahoo's servers.

也许问题在于雅虎使用域密钥验证,鉴于邮件实际上并非来自雅虎的服务器,因此您的应用程序可能会失败。

回答by markus

When I've once had a similar problem I looked at the headers and found out that my host uses SpamAssassin. So I googled for 'SpamAssassin score' and found a multitude of information on how to incorrectly (and thus correctly) form an email.

当我曾经遇到过类似的问题时,我查看了标题并发现我的主机使用了 SpamAssassin。所以我在谷歌上搜索“SpamAssassin score”并找到了大量关于如何错误地(从而正确地)形成电子邮件的信息。

For example: SpamAssassin score list

例如:SpamAssassin 评分表

回答by StackKrish

1. Check mail content

1.查看邮件内容

As others have hinted it is probably marked as spam because your mail looks like spam.

正如其他人所暗示的那样,它可能被标记为垃圾邮件,因为您的邮件看起来像垃圾邮件。

I am not sure if you the script that you have posted is the actual one that you are testing.

我不确定您发布的脚本是否是您正在测试的实际脚本。

If it has the actual mail body & headers, then running this message through a standard installation of SpamAssassin gives it a spam score of 4.9

如果它具有实际的邮件正文和标题,则通过 SpamAssassin 的标准安装运行此邮件会使其垃圾邮件评分为 4.9

X-Spam-Status: No, score=4.9 required=5.0 tests=BAYES_50,HTML_IMAGE_ONLY_04,
        HTML_MESSAGE,MIME_HTML_ONLY,NO_DNS_FOR_FROM,NO_RELAYS autolearn=no
        version=3.2.5

Since the email body has only HTML it has a greater chance of being handled with suspect by most anti-spam solutions.

由于电子邮件正文只有 HTML,因此大多数反垃圾邮件解决方案被怀疑的可能性更大。

2. Mail server's IP

2.邮件服务器的IP

Another aspect worth checking will be the IP address of your mail server. Any mail originating from dynamic IP addresses will potentially be considered as SPAM.

另一个值得检查的方面是您的邮件服务器的 IP 地址。任何来自动态 IP 地址的邮件都可能被视为垃圾邮件。

3. Blocklists

3. 黑名单

Also check if your IP address is listed in one of the block lists. To start with please check your IP address with http://www.spamhaus.org/lookup.lasso.

还要检查您的 IP 地址是否列在阻止列表之一中。首先,请通过http://www.spamhaus.org/lookup.lasso检查您的 IP 地址。

回答by Gerhard

Use mxtoolbox.comto check the servers IP to be blacklisted or not. As well this website can help you with a couple of email related checks.

使用mxtoolbox.com检查服务器 IP 是否被列入黑名单。该网站还可以帮助您进行一些与电子邮件相关的检查。

Of course there are a long list of checks running inside spam filters. As already suggested, check the email headers for details about the spam filters rating of the spam email.

当然,在垃圾邮件过滤器中运行着一长串检查。正如已经建议的那样,检查电子邮件标题以了解有关垃圾邮件过滤器评级的详细信息。

Hope that helps!

希望有帮助!

回答by Jismon Thomas

                   **This Works Perfectly fine for me**     
                        $to="[email protected]";
                        $subject="This is Your Message";
                        $from = 'Sender <[email protected]>';
                        $body='Hi '.$name.', <br/><br>Now You can See Yor main in inbox';
                        $headers = "From: " .($from) . "\r\n";
                        $headers .= "Reply-To: ".($from) . "\r\n";
                        $headers .= "Return-Path: ".($from) . "\r\n";;
                        $headers .= "MIME-Version: 1.0\r\n";
                        $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
                        $headers .= "X-Priority: 3\r\n";
                        $headers .= "X-Mailer: PHP". phpversion() ."\r\n";
                        mail($to,$subject,$body,$headers);

回答by Masti Khan

I was having the same problem:

我遇到了同样的问题:

The problem is that when you specify content-type before the "From:" part , the mail comes as a spam.

问题是,当您在“From:”部分之前指定 content-type 时,邮件会作为垃圾邮件出现。

But if you specify "From:" before the content part it comes as a normal mail and makes you smile and curious.

但是如果您在内容部分之前指定“发件人:”,它会作为普通邮件出现,让您微笑和好奇。

回答by Sashi

Remove the Content-type: text/htmland add $headers .= "X-Priority: 2\nX-MSmail-Priority: high";to get rid of Spam. This method has been tried and tested.

删除Content-type: text/html并添加$headers .= "X-Priority: 2\nX-MSmail-Priority: high";以摆脱垃圾邮件。这种方法已经过尝试和测试。

回答by Strae

As schnalle said, one problem surely is that the smtp server that you use to send the email and the one thet you specify as From, is different.. the from's domain whould be the same that the server youre running on.

正如 schnalle 所说,一个问题肯定是你用来发送电子邮件的 smtp 服务器和你指定为 From 的那个服务器是不同的.. from 的域应该与你正在运行的服务器相同。

So, you can use the yahoo server to send the email (check if they allow the smtp remote connection, but i guess they do) connecting by smtp, and this will solve 1 problem.

因此,您可以使用雅虎服务器发送电子邮件(检查他们是否允许 smtp 远程连接,但我猜他们允许)通过 smtp 连接,这将解决 1 个问题。

Another one is the html contents without the alternative plain text contents, but, this one is less important.

另一种是没有替代纯文本内容的 html 内容,但是,这一点不太重要。

I suggest you phpMailer, free and open-source php class to send email, easly to use (i use it event o send mail through gmail server)

我建议你phpMailer,免费和开源的 php 类来发送电子邮件,易于使用(我使用它事件 o 通过 gmail 服务器发送邮件)

回答by Strae

  1. On your server try to sort your SPF (Sender Policy Framework, Google for SPF record) record out.
  2. Make sure you send your e-mails from an existing account on your server/domain.
  3. Make sure you have the reply-to address in your header.
  1. 在您的服务器上尝试将您的 SPF(发件人策略框架,Google for SPF 记录)记录排序。
  2. 确保您从服务器/域上的现有帐户发送电子邮件。
  3. 确保您的标题中有回复地址。

These are the basic things you can try.

这些是您可以尝试的基本操作。