0

I want to send an email on my gmail on submission of form. Success message-"Email send successfully is displayed" but I did not receive any email on my gmail-

Following is the code that I used to send email on form submission.

if(isset($_POST["submit"])){
//Checking for blank Fields..
if($_POST["vname"]==""||$_POST["vemail"]==""||$_POST["sub"]==""||$_POST["msg"]==""){
echo "Fill All Fields..";
}else{
// Check if the "Sender's Email" input field is filled out
$email=$_POST['vemail']; 

 // Sanitize e-mail address
 $email =filter_var($email, FILTER_SANITIZE_EMAIL);

 // Validate e-mail address
 $email= filter_var($email, FILTER_VALIDATE_EMAIL);

 if (!$email){
echo "Invalid Sender's Email";
 }
 else{
 $subject = $_POST['sub'];

 $message = $_POST['msg'];

 $headers = 'From:'. $email2 . "\r\n"; // Sender's Email
 $headers .= 'Cc:'. $email2 . "\r\n"; // Carbon copy to Sender

 // message lines should not exceed 70 characters (PHP rule), so wrap it
 $message = wordwrap($message, 70);

 // Send mail by PHP Mail Function
 mail("preetkaurpaik@gmail.com", $subject, $message, $headers);
 echo "Your mail has been sent successfuly ! Thank you for your feedback";
 }
}
}
?> 
Gurpreet Kaur
  • 77
  • 1
  • 3
  • 10

4 Answers4

0

You may want to check your spam/junk folders.

Sometimes when testing out on a new server your email service provider will auto-filter out those emails.

If that was the issue, just whitelist the sender/add to approved senders.

Daniel Dees
  • 182
  • 2
  • 14
0

I'd suggest using a proper SMTP client. Relying on mail() is not a best practice, as you can see this shows even during the development.

fabrik
  • 14,094
  • 8
  • 55
  • 71
0

check your spam folder mail goes there in Gmail. Did you use this code in the local system? so use PHP Mailer It's best practices for you. https://github.com/PHPMailer/PHPMailer

Subhash Patel
  • 674
  • 7
  • 16
0

I think the problem might be because you don’t set the variable $email2 please check if it has an valid email in it.

Maybe try something like this, which tells the MUA the from and Reply-To address:

<?php
$to      = $emailOfUser;
$subject = 'the subject';
$message = 'hello';
$headers = 'From: preetkaurpaik@gmail.com' . "\r\n" .
    'Reply-To: preetkaurpaik@gmail.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
O.S.Kaya
  • 108
  • 2
  • 8