0

I've no idea why this script sends email into the spam folder. I tried to fix it using SMTP but it was useless, it only works using Hotmail. I'm using MAMP for local hosting because my work is not online yet.

Why? How can I fix this problem?

Script:

<?php

require("PHPMailer.php");
session_cache_limiter( 'nocache' );
header( 'Expires: ' . gmdate( 'r', 0 ) );
header( 'Content-type: application/json' );


$to         = 'myEmail';  // put your email here

$email_template = 'simple.html';

$subject    = strip_tags($_POST['subject']);
$email       = strip_tags($_POST['email']);
$name       = strip_tags($_POST['name']);
$message    = nl2br( htmlspecialchars($_POST['message'], ENT_QUOTES) );
$result     = array();



$mail = new PHPMailer();

$mail->IsSMTP();



$mail->Host = "smtp.gmail.com";

$mail->SMTPAuth = true;

$mail->SMTPSecure = 'ssl';

$mail->Port = 465;


// gmail account data

$mail->Username = "myAccountGmail";

$mail->Password = "myPasswordGmail";


if(empty($name)){

    $result = array( 'response' => 'error', 'empty'=>'name',    'message'=>'<strong>Error!</strong>&nbsp; Name is empty.' );
    echo json_encode($result );
    die;
} 

if(empty($email)){

    $result = array( 'response' => 'error', 'empty'=>'email', 'message'=>'<strong>Error!</strong>&nbsp; Email is empty.' );
    echo json_encode($result );
    die;
} 

if(empty($message)){

     $result = array( 'response' => 'error', 'empty'=>'message', 'message'=>'<strong>Error!</strong>&nbsp; Message body is empty.' );
     echo json_encode($result );
     die;
}



$headers  = "From: " . $name . ' <' . $email . '>' . "\r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";


$templateTags =  array(
    '{{subject}}' => $subject,
    '{{email}}'=>$email,
    '{{message}}'=>$message,
    '{{name}}'=>$name,
    '{{phone}}'=>$phone
    );


$templateContents = file_get_contents( dirname(__FILE__) . '/email-templates/'.$email_template);

$contents =  strtr($templateContents, $templateTags);

if ( mail( $to, $subject, $contents, $headers ) ) {
    $result = array( 'response' => 'success', 'message'=>'<strong>Thank You!</strong>&nbsp; Your email has been delivered.' );
} else {
    $result = array( 'response' => 'error', 'message'=>'<strong>Error!</strong>&nbsp; Cann\'t Send Mail.'  );
}

echo json_encode( $result );

die;
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Andrea
  • 67
  • 1
  • 6

1 Answers1

0

Google rewrites the From and Reply-To headers in messages you send via it's SMTP service to values which relate to your gmail account.

The SMTP feature of gmail isn't intended to be an open or relay service. If it allowed any values for the From header, it would significantly dilute Google's standing with spam services, as there would be no way to verify the credentials of the sender.

So if you are setting a different value in the Form field of the header, most of the recipient mail servers will consider it as spam.

Amar Pratap
  • 1,000
  • 7
  • 20