1

I am using this code for my contact form in a html website but mail is not coming in Gmail Inbox.

Can any one help me i am trying to solve this issue but i don't have any guide.

<?php
session_cache_limiter( 'nocache' );
$subject = $_REQUEST['subject']; // Subject of your email
$to = "iamuser@gmail.com";  //Recipient's E-mail

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= "From: " . $_REQUEST['name'].'<'.$_REQUEST['email'] .'>'. "\r\n"; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

$message  = 'Name: ' . $_REQUEST['name'] . "<br>";
$message .= 'Company: ' . $_REQUEST['company'] . "<br>";
$message .= $_REQUEST['message'];

if (@mail($to, $subject, $message, $headers))
{
    // Transfer the value 'sent' to ajax function for showing success message.
    echo 'sent';
    // header('Location: ../index.html');
}
else
{
    // Transfer the value 'failed' to ajax function for showing error message.
    echo 'failed';
}
?>
Enzo
  • 19
  • 3

2 Answers2

1

There are some problem in setting the header. And most important is that you need to define the correct and valid Email Id in the From section because google generally used to validate the domain, the mail coming from.

If it is not white-listed at google end then it wills end the mail to Spam automatically, that is happening now as I think.

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

You can try the below code.

$headers = "From: myplace@example.com\r\n";
$headers .= "Reply-To: myplace2@example.com\r\n";
$headers .= "Return-Path: myplace@example.com\r\n";
$headers .= "CC: sombodyelse@example.com\r\n";
$headers .= "BCC: hidden@example.com\r\n";

Reference Link. https://github.com/PHPMailer/PHPMailerPHPMailer/PHPMailerPHPMailer

There is another code you can try:

function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
 $file = $path.$filename;
 $file_size = filesize($file);
 $handle = fopen($file, "r");
 $content = fread($handle, $file_size);
 fclose($handle);
 $content = chunk_split(base64_encode($content));
 $uid = md5(uniqid(time()));
 $header = "From: ".$from_name." <".$from_mail.">\r\n";
 $header .= "Reply-To: ".$replyto."\r\n";
 $header .= "MIME-Version: 1.0\r\n";
 $header .= "Content-Type: multipart/mixed;boundary=\"".$uid."\"\r\n\r\n";
 $header .= "This is a multi-part message in MIME format.\r\n";
 $header .= "--".$uid."\r\n";
 $header .= "Content-type:text/plain;charset=iso-8859-1\r\n";
 $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
 $header .= $message."\r\n\r\n";
 $header .= "--".$uid."\r\n";
 $header .= "Content-Type: application/octet-stream;name=\"".$filename."\"\r\n";
 // use different content types here$header .= "Content-Transfer-Encoding: base64\r\n";
 $header .= "Content-Disposition: attachment;filename=\"".$filename."\"\r\n\r\n";
 $header .= $content."\r\n\r\n";
 $header .= "--".$uid."--";
 if (mail($mailto, $subject, "", $header)) {echo "mail send ... OK";
  // or use booleans here} else {echo "mail send ... ERROR!";
 }
}
Aman Garg
  • 3,122
  • 4
  • 24
  • 32
0

If you are developing this program in local server. The emails will not be sent to your gmail account.

If you want to test your code on a local machine please install Test Mail Server Tool.

No email will be delivered while running on local machine but you will get an idea how the email will look like.

When you run the same on web hosting server, the email will be delivered to the email id specified in $to field.

Amol
  • 145
  • 3
  • 9