【问题标题】:How do I prevent mails sent through PHP mail() from going to spam? [duplicate]如何防止通过 PHP mail() 发送的邮件进入垃圾邮件? [复制]
【发布时间】:2011-08-21 13:29:12
【问题描述】:

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

【问题讨论】:

  • 您的电子邮件是垃圾邮件吗?
  • 没有..但是电子邮件进入了垃圾邮件文件夹

标签: php email spam phpmailer


【解决方案1】:

您必须添加针头:

示例代码:

$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!";
   }
?> 

【讨论】:

  • 什么是“针头”?您是指返回路径标头还是回复标头 - 或两者兼而有之?
  • 我们如何在 wordpress 中添加这些标题?
  • 我们有一个 wordpress 网站,我们的查询邮件正在发送到垃圾邮件文件夹。你能建议我们可以做什么吗?
  • This article 表示返回路径不应由发件人设置。它还说 “从 PHPMailer 版本 5.2.8 开始,在创建的消息中不再设置 Return-Path 标头。”
【解决方案2】:

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

【讨论】:

    【解决方案3】:
    <?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); 
    
    
    ?> 
    

    【讨论】:

    • 删除 .从第一个 $header $headers = "Reply-To: The Sender &lt;sender@domain.com&gt;\r\n";
    【解决方案4】:

    试试PHP Mailer library
    或通过 SMTP 发送邮件在发送之前对其进行过滤。
    同时尝试提供所有详细信息,例如FROMreturn-path

    【讨论】:

      【解决方案5】:
      $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中的所有内容并在下面提及

        [发送邮件]

        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

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

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

      d。评论 sendmail_path="D:\xamp\mailtodisk\mailtodisk.exe" 因此它应该如下所示

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

      e。 mail.add_x_header=Off

      【讨论】:

      • 这是正确的方式。干得好!
      【解决方案6】:
      <?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" ;
      ?>
      

      【讨论】:

        猜你喜欢
        • 2013-04-27
        • 2018-12-18
        • 2017-08-31
        • 2013-08-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多