Php mail form going to junk in outlook Can anyone help Me

<?php
   if(isset($_POST['submit'])){
	$first_name = $_POST['first_name'];
        $last_name = $_POST['last_name'];//last_name
        $email = $_POST['email'];
        $company = $_POST['company'];
        $telephone = $_POST['telephone'];
        $msg = $_POST['msg'];
	$contactnumber = $_POST['contactnumber']; 
	$to = "name@example.com";
	$subject = "client request";
	$body = $first_name . " " . $last_name . "\n\n" . $email . "\n" . $company . "\n" . $telephone . "\n" . $msg;
	$headers = "from: ".$email;
	if(mail($to,$subject,$body,$headers)){
		echo "message sent! <a href = 'index.html'>Click me</a> to send another email";
          }
        else{
	   echo "Something went wrong";
	}
    }
?>

It’s probably not your PHP code that’s the problem but the contents of the message that Outlook thinks is spam.

You really should be performing some sort of validation and/or sanitising of the $_POST variables before passing them on - they could contain anything…

Sending an email using the form-fillers email address as the “from” address isn’t a good thing to do, that’s basically “spoofing”. Some email servers won’t allow it.

1 Like

Additionally, if the server does send it, some Email clients will flag it as spam because of this.

1 Like

Sometimes this happens, either your mail is sent to junk or is considered as spam. Try this code and if this doesn’t work let me know. I have used the same code in my website and its perfect.

<?php

$errorMSG = "";

// NAME
if (empty($_POST["name"])) {
    $errorMSG = "Name is required ";
} else {
    $name = $_POST["name"];
}

// EMAIL
if (empty($_POST["email"])) {
    $errorMSG .= "Email is required ";
} else {
    $email = $_POST["email"];
}

// MSG SUBJECT
if (empty($_POST["msg_subject"])) {
    $errorMSG .= "Subject is required ";
} else {
    $msg_subject = $_POST["msg_subject"];
}


// MESSAGE
if (empty($_POST["message"])) {
    $errorMSG .= "Message is required ";
} else {
    $message = $_POST["message"];
}


$EmailTo = "hello@graygrids.com";
$Subject = "New Message Received";

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Subject: ";
$Body .= $msg_subject;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";

// send email
$success = mail($EmailTo, $Subject, $Body, "From:".$email);

// redirect to success page
if ($success && $errorMSG == ""){
   echo "success";
}else{
    if($errorMSG == ""){
        echo "Something went wrong :(";
    } else {
        echo $errorMSG;
    }
}

?>

This still puts the user’s email address as the “from” address, so it’s still spoofing the server and could have the same problem.

There are a few issues with your code @netfluxtechnologies the main thing being that you perform no validation on user input.

1 Like

validations is declared . You can see this in if-else loop.

What you’re doing there is checking for empty fields. That is NOT validation. Someone could enter all sorts of garbage in those fields.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.