Dinesh Nagar Dinesh Nagar - 5 months ago 40
PHP Question

sending email via php mail function goes to spam

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><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/>

$param->sendTo = $email;
$param->subject = "Request for Department transfer";


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.
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 u please figure out whats the problem?


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.