0

Im using a mailing form but the mail cannot be sent to gmail inbox !!

here is the code i'm using:

<?php
if (isset($_POST['send'])) {
    $name    = $_POST['name'];
    $email   = $_POST['email'];
    $message = $_POST['message'];       

    $emailTo = 'example@gmail.com';
    $subject = 'Sujet:  '.$name;

    $body = "Full Name: $name \n\n  Message: $message \n\n Sent by : $email";
    $headers = 'De: ' .' <'.$email.'>' . "\r\n";

    if(mail($emailTo, $subject, $body, $headers)){  
         echo   "Success";

    }else{  
          echo "Error";

    }
}

?>

Mariem
  • 39
  • 1
  • 1
  • 10

2 Answers2

0

If you want to send emails from localhost directly, you need to install a Mail Transport Agent (MTA), or if you like, a SMTP service.

IIS provides one. You can otherwise find some others on Google.

You can also change your php.ini mail settings. This won't use localhost per say to send emails, but a relay host that will allow you to send emails from a PHP script.

Source

Community
  • 1
  • 1
Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117
0

Try This code:

$name    = $_POST['name'];
$email   = $_POST['email'];
$message = $_POST['message'];

$email_to = "example@email.com";
$email_subject = "Email for: ".$name;

$headers = $headers = 'From: '.$email."\r\n".
       'BCC: '.$email_to_bcc."\r\n" .
       'X-Mailer: PHP/' . phpversion() . "\r\n" .
       "MIME-Version: 1.0\r\n" .
       "Content-Type: text/html; charset=utf-8\r\n" .
       "Content-Transfer-Encoding: 8bit\r\n\r\n";

if (mail($email_to, $email_subject, $message, $headers)) {
    echo "Mail sent";
}

Of course your server has to be able to send emails through PHP mail function. Best to set up sending mails through SMTP: Use this script: https://github.com/Synchro/PHPMailer Easy to set up and works great.

J.T.
  • 135
  • 11