30

I am facing problem in sending mail to my inbox (gmail account) but everytime it goes to spam folder.

Here is the code snippet

     //$ticketDetail is array which contain required information to send.
       sendOwnershipEmail('dineshnagarscriet@gmail.com', $ticketDetail);
    
       function sendOwnershipEmail($email, $ticketDetail) {
            $param = new stdClass();
   

$param->content = "<div>
    <div><b>".$ticketDetail[0]['ticket_number']."</b></div><br/>
    <div><img src='".$ticketDetail[0]['image_path']."'/></div><br/>
    <div>Ticket with ticket number ".$ticketDetail[0]['ticket_number']." has been requested for tranfer from <div/>
    <div>".$ticketDetail[0]['oldDepartment']." to ".$ticketDetail[0]['newDepartment']." Department <div/>
  </div>";
            
            $param->sendTo = $email;
            $param->subject = "Request for Department transfer";
            
        sendMailFunction($param);
    }
    
    
    function sendMailFunction($param) {
            $to = $param->sendTo;
            $subject = $param->subject;
            $headers = 'MIME-Version: 1.0' . "\r\n";
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
            $headers .= 'From: successive.testing@gmail.com' . "\r\n";
            $message = "<html><head>" .
                   "<meta http-equiv='Content-Language' content='en-us'>" .
                   "<meta http-equiv='Content-Type' content='text/html; charset=windows-1252'>" .
                   "</head><body>" .$param->content.        
                   "<br><br></body></html>";
          mail($to, $subject, $message, $headers);
    }

And I have tried a lot like setting headers as Reply-To, Return-Path etc but every time it goes to spam.

Can you please figure out whats the problem?

Dinesh Nagar
  • 768
  • 2
  • 11
  • 23
  • 2
    As I know, Mail providers like Yahoo! send emails with PHP to spam folder. Also, I would be good to know a liitle about http://en.wikipedia.org/wiki/Sender_Policy_Framework – undone Aug 14 '13 at 10:35
  • HTML parts are valid when its param values are double quoted: `` like this. – Lenin Aug 14 '13 at 10:57

6 Answers6

33

The problem is simple that the PHP-Mail function is not using a well configured SMTP Server.

Nowadays Email-Clients and Servers perform massive checks on the emails sending server, like Reverse-DNS-Lookups, Graylisting and whatevs. All this tests will fail with the php mail() function. If you are using a dynamic ip, its even worse.

Use the PHPMailer-Class and configure it to use smtp-auth along with a well configured, dedicated SMTP Server (either a local one, or a remote one) and your problems are gone.

https://github.com/PHPMailer/PHPMailer

dognose
  • 20,360
  • 9
  • 61
  • 107
16

Try changing your headers to this:

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= "From: successive.testing@gmail.com" . "\r\n" .
"Reply-To: successive.testing@gmail.com" . "\r\n" .
"X-Mailer: PHP/" . phpversion();

For a few reasons.

  • One of which is the need of a Reply-To and,

  • The use of apostrophes instead of double-quotes. Those two things in my experience with forms, is usually what triggers a message ending up in the Spam box.

You could also try changing the $from to:

$from = "successive.testing@gmail.com";


EDIT:

See these links I found on the subject https://stackoverflow.com/a/9988544/1415724 and https://stackoverflow.com/a/16717647/1415724 and https://stackoverflow.com/a/9899837/1415724

https://stackoverflow.com/a/5944155/1415724 and https://stackoverflow.com/a/6532320/1415724

  • Try using the SMTP server of your ISP.

    Using this apparently worked for many: X-MSMail-Priority: High

http://www.webhostingtalk.com/showthread.php?t=931932

"My host helped me to enable DomainKeys and SPF Records on my domain and now when I send a test message to my Hotmail address it doesn't end up in Junk. It was actually really easy to enable these settings in cPanel under Email Authentication. I can't believe I never saw that before. It only works with sending through SMTP using phpmailer by the way. Any other way it still is marked as spam."

PHPmailer sending mail to spam in hotmail. how to fix http://pastebin.com/QdQUrfax

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Hi Fred tried your suggested changes but didn't solve problem. any other idea? – Dinesh Nagar Aug 14 '13 at 10:53
  • If by apostrophes you meant single quotes: http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php What would the `$from` do in that script? – Lenin Aug 14 '13 at 10:54
  • @Lenin I call them apostrophes. For example I've just used one now. As for 'single quotes' are the same character on my/the keyboard. – Funk Forty Niner Aug 14 '13 at 10:56
  • ok i just added $from variable for testing purpose like passing fifth parameter in php mail function. – Dinesh Nagar Aug 14 '13 at 10:57
  • Hi @DineshNagar. Very strange. I used single quotes in headers before and they always landed in the Spam box. When I switched to double quotes, all was fine. – Funk Forty Niner Aug 14 '13 at 10:58
  • Please read the thread I shared. Single quotes do not parse strings. He rather used them correctly in most places. – Lenin Aug 14 '13 at 10:58
  • @DineshNagar there's no place for fifth parameter if you do not enclose it with the necessary HTML in `mail()` http://php.net/manual/en/function.mail.php – Lenin Aug 14 '13 at 11:00
  • @Lenin Yes I am familiar with that same post/article. However and contrary to popular belief, is not an exact science, so I beg to differ on that subject, yet I am not denying it neither. Many modify headers to their liking, skipping certain steps etc. With the forms I build, I always follow the same steps and never have I had one end up in the Spam box. That is by "my" own experience of course. However that article does not mention anything about `mail()` and is a completely different "animal". – Funk Forty Niner Aug 14 '13 at 11:01
  • SO answers should not come up with just opinions. Single quotes used correctly does the job well. He rather used the double quotes wrong in the following: ` "" . "" ` – Lenin Aug 14 '13 at 11:05
  • @DineshNagar I just noticed something. Try it again, yet make sure there are dots to concatenate the `headers` and that the first one has none before the `=` sign, but all others will have `.=` I missed a dot in the second one in my answer. – Funk Forty Niner Aug 14 '13 at 11:05
  • @Lenin Those are not part of the headers Lenin. That is completely seperate. (meta tags) and they can be used. – Funk Forty Niner Aug 14 '13 at 11:06
  • Fred i already added .= for headers that u missed in your answer but problem still exist. – Dinesh Nagar Aug 14 '13 at 11:18
  • @DineshNagar Hmm... well I noticed something that may be breaking something. You have a few `
    ` that need to be changed to `` try that. I've seen broken DIVs halt scripts altogether, last week actually.
    – Funk Forty Niner Aug 14 '13 at 11:20
  • @DineshNagar I'm curious about this `$param->content = "
    ` how is that being used?
    – Funk Forty Niner Aug 14 '13 at 11:25
  • $param->content = "
    ....
    " just for creating message along with image and finally embeded this in sendMailFunction() function's message variable.
    – Dinesh Nagar Aug 14 '13 at 11:43
  • @DineshNagar I added a few links for you to look into. Look under my **EDIT**, I hope you will be able to find some answers in there. I am at a loss now. – Funk Forty Niner Aug 14 '13 at 11:46
  • @DineshNagar So, any luck with the added info I included? – Funk Forty Niner Aug 14 '13 at 19:33
  • Ahh pain only still goes to spam :( – Dinesh Nagar Aug 16 '13 at 06:47
5

If you are sending this through your own mail server you might need to add a "Sender" header which will contain an email address of from your own domain. Gmail will probably be spamming the email because the FROM address is a gmail address but has not been sent from their own server.

Patrick
  • 472
  • 2
  • 8
2

What we usually do with e-mail, preventing spam-folders as the end destination, is using either Gmail as the smtp server or Mandrill as the smtp server.

Emile
  • 153
  • 4
0

One thing that I have observed is likely the email address you're providing is not a valid email address at the domain. like Nobody@gmail.com. The email should be existing at Google Domain. I had alot of issues before figuring that out myself... Hope it helps.

Nazehs
  • 478
  • 1
  • 10
  • 19
0

Be careful with your tests. If you in your form you put the same email as the email address which must receive it will be directly in spam :)

Valentin
  • 405
  • 2
  • 6
  • 17