Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sending email via php mail function goes to spam [duplicate]

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('[email protected]', $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: [email protected]' . "\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?

like image 708
Dinesh Nagar Avatar asked Aug 14 '13 10:08

Dinesh Nagar


People also ask

How do I stop emails going to spam in PHP?

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

Why does my sent email keep going to spam?

If the person who you're trying to email previously marked your emails as spam, new emails that you send will also likely end up in the spam folder as well. Additionally, if you're sending out bulk emails, too many people marking your emails as spam can negatively affect your reputation as a sender.

How does PHP mail send email?

PHP makes use of mail() function to send an email. This function requires three mandatory arguments that specify the recipient's email address, the subject of the the message and the actual message additionally there are other two optional parameters. mail( to, subject, message, headers, parameters );


2 Answers

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

like image 134
dognose Avatar answered Sep 22 '22 07:09

dognose


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: [email protected]" . "\r\n" . "Reply-To: [email protected]" . "\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 = "[email protected]"; 


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

like image 37
Funk Forty Niner Avatar answered Sep 20 '22 07:09

Funk Forty Niner