如何防止通过PHP mail()发送的邮件发送到垃圾邮件?

时间:2011-05-09 09:40:09

标签: php email spam phpmailer

我正在使用PHP的mail()函数发送电子邮件(sendmail进程正在运行)。但所有的邮件都会发送垃圾邮件(如果是gmail)。我尝试过网上发现的很多技巧,但没有一个可行,请告诉我任何可靠的技巧。

6 个答案:

答案 0 :(得分:37)

您必须添加针头:

示例代码:     

$headers = "From: myplace@example.com\r\n";
$headers .= "Reply-To: myplace2@example.com\r\n";
$headers .= "Return-Path: myplace@example.com\r\n";
$headers .= "CC: sombodyelse@example.com\r\n";
$headers .= "BCC: hidden@example.com\r\n";

if ( mail($to,$subject,$message,$headers) ) {
   echo "The email has been sent!";
   } else {
   echo "The email has failed!";
   }
?> 

答案 1 :(得分:18)

没有确定的射击技巧。您需要了解邮件被归类为垃圾邮件的原因。 SpamAssassin有一个描述Some Tips for Legitimate Senders to Avoid False Positives的页面。另请参阅Coding Horror: So You'd Like to Send Some Email (Through Code)

答案 2 :(得分:4)

试试PHP Mailer library 或者在发送之前通过SMTP发送邮件过滤它 另请尝试提供所有详细信息,例如FROMreturn-path

答案 3 :(得分:4)

<?php

$subject = "this is a subject";
$message = "testing a message";




  $headers .= "Reply-To: The Sender <sender@domain.com>\r\n"; 
  $headers .= "Return-Path: The Sender <sender@domain.com>\r\n"; 
  $headers .= "From: The Sender <sender@domain.com>\r\n";  
  $headers .= "Organization: Sender Organization\r\n";
  $headers .= "MIME-Version: 1.0\r\n";
  $headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
  $headers .= "X-Priority: 3\r\n";
  $headers .= "X-Mailer: PHP". phpversion() ."\r\n" ;



mail("reciever@domain.com", $subject, $message, $headers); 


?> 

答案 4 :(得分:1)

<?php 
$to1 = 'test@gmail.com';
$subject = 'Tester subject'; 

    // To send HTML mail, the Content-type header must be set

    $headers .= "Reply-To: The Sender <sender@sender.com>\r\n"; 
    $headers .= "Return-Path: The Sender <sender@sender.com>\r\n"; 
    $headers .= "From: sender@sender.com" ."\r\n" .
    $headers .= "Organization: Sender Organization\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset=utf-8\r\n";
    $headers .= "X-Priority: 3\r\n";
    $headers .= "X-Mailer: PHP". phpversion() ."\r\n" ;
?>

答案 5 :(得分:0)

$fromMail = 'set your from mail';
$boundary = str_replace(" ", "", date('l jS \of F Y h i s A'));
$subjectMail = "New design submitted by " . $userDisplayName;


$contentHtml = '<div>Dear Admin<br /><br />The following design is submitted by '. $userName .'.<br /><br /><a href="'.$sdLink.'"><b>Click here</b></a> to check the design.</div>';
$contentHtml .= '<div><a href="'.$imageUrl.'"><img src="'.$imageUrl.'" width="250" height="95" border="0" alt="my picture"></a></div>';
$contentHtml .= '<div>Name : '.$name.'<br />Description : '. $description .'</div>';

$headersMail = '';
$headersMail .= 'From: ' . $fromMail . "\r\n" . 'Reply-To: ' . $fromMail . "\r\n";
$headersMail .= 'Return-Path: ' . $fromMail . "\r\n";
$headersMail .= 'MIME-Version: 1.0' . "\r\n";
$headersMail .= "Content-Type: multipart/alternative; boundary = \"" . $boundary . "\"\r\n\r\n";
$headersMail .= '--' . $boundary . "\r\n";
$headersMail .= 'Content-Type: text/html; charset=ISO-8859-1' . "\r\n";
$headersMail .= 'Content-Transfer-Encoding: base64' . "\r\n\r\n";
$headersMail .= rtrim(chunk_split(base64_encode($contentHtml)));

try {
    if (mail($toMail, $subjectMail, "", $headersMail)) {
        $status = 'success';
        $msg = 'Mail sent successfully.';
    } else {
        $status = 'failed';
        $msg = 'Unable to send mail.';
    }
} catch(Exception $e) {
    $msg = $e->getMessage();
}

这对我来说很好。它包含带图像和链接的邮件,适用于各种邮件ID。线索是完美地使用所有标题。

如果您是从localhost进行测试,请在检查前设置以下内容:

如何设置从localhost xampp发送邮件:

  1. 评论D:/xampp/sendmail/sendmail.ini中的所有内容,并在

    下提及以下内容

    [sendmail的]

    smtp_server = smtp.gmail.com SMTP_PORT = 587 error_logfile = error.log中 debug_logfile =的debug.log auth_username=yourmailid@domain.com AUTH_PASSWORD =您的邮箱密码 force_sender=yourmailid@domain.com

  2. D:/xampp/php/php.ini 一个。

    [邮件功能]

    SMTP = smtp.gmail.com  smtp_port = 587

  3. 湾设置sendmail_from = yourmailid@domain.com C。取消注释sendmail_path =“\”D:\ xamp \ sendmail \ sendmail.exe \“ - t” 因此它应该如下所示

    sendmail_path = "\"D:\xamp\sendmail\sendmail.exe\" -t"
    

    d。 comment sendmail_path =“D:\ xamp \ mailtodisk \ mailtodisk.exe” 因此它应该如下所示

    ;sendmail_path="D:\xamp\mailtodisk\mailtodisk.exe"
    

    mail.add_x_header=Off