Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP mail() form sending to GMAIL spam [duplicate]

I know this problem has been addressed a few times on here. I tried following the directions for setting proper headers, I still run into problems with my emails going into the spam filter in Gmail.

If anyone can please take a look at what I've tried, I'd really appreciate it. The code below is without the headers added as explained here: http://www.velvetblues.com/web-development-blog/avoid-spam-filters-with-php-mail-emails/

Thanks in advance.

define("WEBMASTER_EMAIL", '[email protected]');
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>';
}
like image 591
user1040259 Avatar asked Aug 30 '12 00:08

user1040259


People also ask

How do I prevent mails sent through PHP mail () from going to spam?

Try changing your headers to this: $headers = "MIME-Version: 1.0" . "\r\n"; $headers . = "Content-type: text/html; charset=iso-8859-1" .

Why does mail go to spam in PHP?

If php mail is going to spam, it may be necessary to update the spam filter of the receiver, such as whitelisting the sender address. Third-party search engines such as https://mxtoolbox.com/blacklists.aspx can be used to check if the domain is listed on Real Time Blacklists (RBL).


2 Answers

Use this code :

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

    $headers = 'From: YourLogoName [email protected]' . "\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>");
  }
like image 155
Erfan Safarpoor Avatar answered Sep 20 '22 13:09

Erfan Safarpoor


I think this is your issue:

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

since you are not gmail, hotmail or your users email provider, you cannot have "From: otherdomain.com" and then deliver the mail via "mail.yourdomain.com" - this will most likely move your mail to the spam folder.

Try

 "From: YourWebsiteName <[email protected]>\r\n"
."Reply-To: ".$name." <".$email.">\r\n"

instead.

ALso: your code is very unsave and a prime spam target - google "email header injection php"!

like image 20
iHaveacomputer Avatar answered Sep 19 '22 13:09

iHaveacomputer