0

I am trying to send mail from the server not from localhost, it give status code as 200, but still i am not receiving any mails.

Response Image: Response Image

Php Code to send message:

 $name = $_POST['name'];
 $email = $_POST['email'];
 $message = $_POST['message'];
 $from = 'From: example@gmail.com'; 
 $to = 'example@gmail.com'; 
 $subject = 'Customer Inquiry';
 $body = "From: $name\n E-Mail: $email\n Message:\n $message";

 $headers .= "MIME-Version: 1.0\r\n";
 $headers .= "Content-type: text/html\r\n";
 $headers = 'From: example@gmail.com' . "\r\n" .
 'Reply-To: example@gmail.com' . "\r\n" .
 'X-Mailer: PHP/' . phpversion();

 mail($to, $subject, $message, $headers);
Mukul Aggarwal
  • 1,515
  • 20
  • 16

2 Answers2

0

There might be more things wrong but let's start with the obvious:

$to = 'example@gmail.com'; 

This should contain your email address if you want to receive something.

Can you fix that and test again? If it doesn't work yet we can go a step further.


EDIT AFTER YOUR ANSWER: I have done that already i am messaging from my gmail account to one of my friend gmail account. Is I am making any mistake?

With the code you made php will try to send the email with the mail server which is in your configurations: to send an email with GMAIL you need to fix your code. This is an example of how you can use phpmailer to send it:

//include the file
require_once('class.phpmailer.php');

$phpmailer          = new PHPMailer();


$phpmailer->IsSMTP(); // telling the class to use SMTP
$phpmailer->Host       = "ssl://smtp.gmail.com"; // SMTP server
$phpmailer->SMTPAuth   = true;                  // enable SMTP authentication
$phpmailer->Port       = 465;          // set the SMTP port for the GMAIL server; 465 for ssl and 587 for tls
$phpmailer->Username   = "yourname@yourdomain"; // Gmail account username
$phpmailer->Password   = "yourpassword";        // Gmail account password

$phpmailer->SetFrom('name@yourdomain.com', 'First Last'); //set from name

$phpmailer->Subject    = "Subject";
$phpmailer->MsgHTML($body);

$phpmailer->AddAddress($to, "To Name");

if(!$phpmailer->Send()) {
  echo "Mailer Error: " . $phpmailer->ErrorInfo;
} else {
  echo "Message sent!";
} 

from: https://stackoverflow.com/a/16022357/2042240

I really suggest you to use phpmailer (http://phpmailer.worxware.com/) since it is very powerful and easy to use. When you have downloaded the library just import it as in the example i showed you and you're ready to go.

EDIT 2: if you still prefer using the mail() function here you can see how you can enable GMAIL with it: https://www.digitalocean.com/community/tutorials/how-to-use-gmail-or-yahoo-with-php-mail-function

Community
  • 1
  • 1
nowhere
  • 1,558
  • 1
  • 12
  • 31
0

code for sent mail

    $to='comments@mydomain.com';
    $from = $_POST['email'];
    $name = $_POST['name'];
    $subject="comments from : ".$name;
    $message = "Comment from  :  ".$from."\r\n";
    $message.=$_POST['comments'];
    $message=wordwrap($message,70,"<br>");
    $message=str_replace("\n.","\n..",$message);
    $headers='From: info@mydomain.com';
    $mail=mail($to, $subject, $message , $headers);
    if ($mail==true)
       echo "success";
noushid p
  • 1,453
  • 5
  • 16
  • 29