0

I need to send email to gmail with php, i tried the code below with other emails and it works fine, but when i set my gmail account it doesn't work.

<?php
$errors = '';
$myemail = 'example@gmail.com';//<-----email address here.
if(empty($_POST['Email'])  ||
   empty($_POST['Message']));
$theEmail = $_POST['Email']; 
$Message = $_POST['Message'];  
?>
<?php  
$to = $myemail; 
$email_subject = "Contact form submission:"; 
$email_body = "You have received a form submission. ". 
"Here are the details:\nEmail: $theEmail \n". 
"Message: $Message\n"; 
$headers = "From: The Restaurant Website\n"; 
mail($to,$email_subject,$email_body,$headers);
header('Location: index.html'); 
?>
Mr-montasir
  • 132
  • 1
  • 1
  • 8
  • Perhaps it goes in your spam? Or perhaps gmail's filters reject it as not from a legitimate source, maybe because the "from" header doesn't even contain a proper email address. Some mail servers may not be quite so picky. Try to get stricter about how you send emails. I also highly recommend using PHPMailer instead of mail(), it helps to avoid some basic mistakes. – ADyson Oct 19 '22 at 01:07
  • Required reading: [PHP mail function doesn't complete sending of e-mail](https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail) – ADyson Oct 19 '22 at 01:08
  • Thanks for your help! I'm not experienced in php, i just copied and pasted this code, then replaced it with my informations, but ok i will try to read the page you sent perhaps it helps. – Mr-montasir Oct 19 '22 at 01:13
  • I removed the "from" then i submit the form and it works, but i found it in the spam, is there anyway to avoid sending it to spam?? – Mr-montasir Oct 19 '22 at 01:19
  • 2
    Try using a valid from _address_ (not name) corresponding to the domain of the server from which you're sending this mail. If you're sending it from localhost, that might be tricky. But apart from that, there are also a lot of other things which can lead to emails failing, or being treated as spam - which is why I sent you a link which lists them all. Unless you have some experiencing administering a mail server, or deparately want to learn it the hard way, I would suggest getting rid of mail(), and using PHPMailer to send emails via a public SMTP server. – ADyson Oct 19 '22 at 01:25
  • I will get rid of mail() and start using the PHPMailer from now, thanks! – Mr-montasir Oct 19 '22 at 14:10

0 Answers0