【问题标题】:PHP mail() form sending to GMAIL spam [duplicate]PHP mail()表单发送到GMAIL垃圾邮件[重复]
【发布时间】:2012-08-24 16:15:11
【问题描述】:

我知道这个问题已经在这里解决了几次。我尝试按照设置正确标题的说明进行操作,但我的电子邮件进入 Gmail 中的垃圾邮件过滤器时仍然遇到问题。

如果有人可以看看我的尝试,我将不胜感激。下面的代码没有添加标题,如下所述:http://www.velvetblues.com/web-development-blog/avoid-spam-filters-with-php-mail-emails/

提前致谢。

define("WEBMASTER_EMAIL", 'myName@mydomain.com');
if($post)
{
    $name    = stripslashes($_POST['name']);
    $email   = trim($_POST['email']);
    $subject = trim($_POST['subject']);
    $message = stripslashes($_POST['message']);

    $error = '';

    // Check name
    if(!$name)
        $error .= 'Name required! ';

    // Check email
    if(!$email)
        $error .= 'E-mail required! ';

    if($email && !ValidateEmail($email))
        $error .= 'E-mail address is not valid! ';

    // Check message
    if(!$message)
        $error .= "Please enter your message!";

    if(!$error)
    {

        $mail = mail(WEBMASTER_EMAIL, $subject, $message,
            "From: ".$name." <".$email.">\r\n"
            ."Reply-To: ".$email."\r\n"
            ."X-Mailer: PHP/" . phpversion());

        if($mail)
            echo 'OK';
    }
    else
        echo '<div class="errormsg">'.$error.'</div>';
}

【问题讨论】:

  • 我不知道为什么会发生这种情况,但我有一个想法。以正常方式向自己发送电子邮件,检查其原始内容,然后检查您使用 php.ini 发送的电子邮件的内容。可能缺少某些东西。
  • 这很可能只是您的信息的性质。没有神奇的标题可以使电子邮件永远不会被标记为垃圾邮件。
  • 错误的标头并不是邮件被归档为垃圾邮件的唯一原因。其他问题,如内容、在发送主机上执行的反向 DNS 查找以及其他所有问题。
  • 使用 PHPMailer 和 Gmail 帐户来使用 SMTP,假设邮件内容不是垃圾邮件,这应该避免垃圾邮件过滤器。 forums.digitalpoint.com/showthread.php?t=871893

标签: php gmail html-email


【解决方案1】:

使用此代码

 $to = Email;
 $subject = subject ;
 $body = "<div> hi hi .. </div>";

    $headers = 'From: YourLogoName info@domain.com' . "\r\n" ;
    $headers .='Reply-To: '. $to . "\r\n" ;
    $headers .='X-Mailer: PHP/' . phpversion();
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";   
if(mail($to, $subject, $body,$headers)) {
  echo('<br>'."Email Sent ;D ".'</br>');
  } 
  else 
  {
  echo("<p>Email Message delivery failed...</p>");
  }

【讨论】:

  • 在发布时可能会很好。不幸的是,今天这已被视为 Gmail 垃圾邮件。
  • 直接进入垃圾箱
  • 所以你总是设置Reply-To: 标头吗?为了什么?
  • 为我工作!注意:br 用作空标签!所以 html 将是:&lt;p&gt;your message&lt;/p&gt;&lt;br /&gt;
【解决方案2】:

我认为这是你的问题:

 "From: ".$name." <".$email.">\r\n"

由于您不是 gmail、hotmail 或您的用户电子邮件提供商,您不能使用“From: otherdomain.com”然后通过“mail.yourdomain.com”发送邮件 - 这很可能会将您的邮件移至垃圾邮件文件夹。

试试

 "From: YourWebsiteName <noreply@yourwebsite.com>\r\n"
."Reply-To: ".$name." <".$email.">\r\n"

改为。

另外:您的代码非常不可靠并且是主要的垃圾邮件目标 - 谷歌“电子邮件标头注入 php”!

【讨论】:

  • 为什么在这里使用reply-to
【解决方案3】:

Google 倾向于不仅对网站进行处罚,而且对过去有许多用户在网络上发送垃圾邮件的服务提供商进行处罚。 如果您注册了谷歌识别为垃圾邮件发送者的这些服务提供商之一,这可能是您的 php mail() 邮件进入 gmail 垃圾邮件箱的原因。尝试与您的服务器提供商讨论此问题。

在这种情况下,您会在“垃圾邮件”消息中收到来自 google 的警告:

“为什么这封邮件会出现在垃圾邮件中?我们发现很多来自 home.pl 是垃圾邮件。了解更多”

【讨论】:

    【解决方案4】:

    我知道这个问题很久以前就有人问过了,但我想我会在此处放弃 2020 年的答案,以便它可能对新访客有所帮助。

    请注意:

    • 此答案是通用答案,需要您根据所使用的表单输入来编辑一些详细信息。
    • 您还需要将标题等中的电子邮件地址更新为与您的域相关联的地址。
    • 此解决方案假定您使用的是 Google Recaptcha。如果没有,那就删掉关于“Google recapthca”的部分。
    • 此脚本添加了不应删除的安全性和验证功能。
    • 如果您要使用 Sweet Alert,则应通过 CDN or NPM 将其安装到您的网站/应用中。

    一些 Javascript 来创建自定义Sweet Alert 警报,在邮件发送时触发:

    // Custom SweetAlert alert that gets triggered on email send
    function enquirySent() {
        swal({
          title: "Email sent!",
          text: "Thank you for your email. We'll be in contact ASAP.",
          icon: "success",
          button: "Okay",
        });
    }
    function enquiryNotSent() {
        swal({
          title: "Oops!",
          text: "Your email was NOT sent due to an error.",
          icon: "error",
          button: "Okay",
        });
    };
    

    发送邮件的 PHP 脚本:

    <?php
        if (isset($_POST['submit'])) {
    
            // For the Google recaptcha
            $curlx = curl_init();
            curl_setopt($curlx, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
            curl_setopt($curlx, CURLOPT_HEADER, 0);
            curl_setopt($curlx, CURLOPT_RETURNTRANSFER, 1); 
            curl_setopt($curlx, CURLOPT_POST, 1);
            $post_data = [
                'secret' => 'YOUR CAPTCHA SECRET KEY',
                'response' => $_POST['g-recaptcha-response']
            ];
            curl_setopt($curlx, CURLOPT_POSTFIELDS, $post_data);
            $resp = json_decode(curl_exec($curlx));
            curl_close($curlx);
            // Google recaptcha end
    
            // Form details (sanitized)
            $name = htmlspecialchars($_POST['name']);
            $surname = htmlspecialchars($_POST['surname']);
            $email = htmlspecialchars($_POST['email']);
            $message = htmlspecialchars($_POST['message']);
    
            // Mail headers and details
            $email_from = 'youremail@yourdomain.com';
            $email_body = "You have received a new message from the user $name $surname.\nHere is the message:\n\n".$message;
    
            $headers = "From: $email_from \r\n";
            $headers .= "Reply-To: ".$email."\r\n";
            $headers .= "Return-Path: ".$email."\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" ;
    
            $error = false;
    
            // Some more input validation/sanitizing
            if (!preg_match("/^[a-zA-Z ]*$/",$first_name) && $first_name!="") {
                $error = true; 
            }
            if (!preg_match("/^[a-zA-Z ]*$/",$last_name) && $last_name!="") {
                $error = true; 
            }
            if (!filter_var($email, FILTER_VALIDATE_EMAIL) && $email!="") {
                $error = true;
            }
    
            function IsInjected($str) {
                $injections = array('(\n+)',
                       '(\r+)',
                       '(\t+)',
                       '(%0A+)',
                       '(%0D+)',
                       '(%08+)',
                       '(%09+)'
                       );
                           
                $inject = join('|', $injections);
                $inject = "/$inject/i";
                
                if (preg_match($inject,$str)) {
                  return true;
                } else {
                  return false;
                }
            }
    
            if (IsInjected($visitor_email)) {
                echo "Bad email value!";
                exit;
            }
    
            // Sending the email
            if ($error == false) {
                $to = "youremail@yourdomain.com";
                $subject = "Enquiry from website";
                mail($to, $subject, $email_body, $headers);
    
                // Calling the email sent / not sent alerts
                echo '<script type="text/javascript">',
                    'enquirySent()',
                    '</script>';
            } else {
                echo '<script type="text/javascript">',
                    'enquiryNotSent()',
                    '</script>';
            }
        }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-31
      • 1970-01-01
      • 2015-05-01
      • 2015-01-30
      • 1970-01-01
      • 2019-07-19
      • 2013-01-06
      • 1970-01-01
      相关资源
      最近更新 更多