简体   繁体   中英

Mails from mail function is in spam

I am using following code to send email but unfortunately emails is going in spam.check code and need suggestion...I am using php mail() function to send mail.. what is mistake done by me? It is correct code or not? Mail is sending but going in spam rather in inbox. So that i need correction in my code..

<?php
class mail
{
    function setInput($data)
    {
        $data = trim($data);
        $data = addslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }
    public function send($data)
    {
            $email_to = "xyz@gmail.com";
        $email_subject=$this->setInput($data['Subject']);

        $phone = $this->setInput($data['Phone']); // required
        $email = $this->setInput($data['Email']); // required
        $name = $this->setInput($data['Name']); // not required
        $message = $this->setInput($data['Message']); // required

         $error_message = "";
         $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

         if(!preg_match($email_exp,$email)) {
        $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
        }
 
        $string_exp = "/^[A-Za-z .'-]+$/";
 
       if(!preg_match($string_exp,$name)) {
         $error_message .= 'The First Name you entered does not appear to be valid.<br />';
         }
 
        if(strlen($message) < 2) {
         $error_message .= 'The Comments you entered do not appear to be valid.<br />';
         }
 
        if(strlen($error_message) > 0) {
           died($error_message);
         }
 
        $email_message = "Form details below.\n\n";
 
     
         function clean_string($string) {
           $bad = array("content-type","bcc:","to:","cc:","href");
          return str_replace($bad,"",$string);
         }

        $email_message .= "Subject: ".clean_string($email_subject)."\n";
        $email_message .= "Email: ".clean_string($email)."\n";
        $email_message .= "Name: ".clean_string($name)."\n";
        $email_message .= "Comments: ".clean_string($message)."\n";

        $headers = 'From: '.$email."\r\n".
       'Reply-To: '.$email."\r\n" .
       'X-Mailer: PHP/' . phpversion();
        @mail($email_to, $email_subject, $email_message, $headers); 

        echo "<script>
          alert('Thank you for contacting us. We will be in touch with you very soon.');
           window.location.href='contact.php';
           </script>";

    }
}
$objclass=new mail();

if(isset($_REQUEST['page_action']))
{
   $page_action=$_REQUEST['page_action'];
   switch($page_action)
   {
      case "email-msg";
      $objclass->send($_POST);
      break;
   }
}

?>

Please check

  1. whether your server (running this PHP script) has a reverse DNS record (PTR record)

  2. whether the from email ($email) has been set to have a SPF record, specifying this server IP to be a legitimate source from which the email originate. (eg with a TXT record in your domain DNS record like v=spf1 ip4:xxxx a mx ptr ~all, where xxxx is the IP of the server running the PHP script)

You also need to check whether the IP of this server has been blacklisted as spam source. But please check 1 and 2 above first.

An alternative solution is to send your mail thru a SMTP server. You may use the PhpMailer to help you do it ( https://github.com/PHPMailer/PHPMailer ). If your SMTP server is configured correctly (and not blacklisted as spam source), the mail sent out will arrive in the recipient inbox but not in spam folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM