-1

I'm using the code below for the messages sent via the contact form of my website. The issue is that all the messages arrive in the spam box. I suspect that this is because of this part "From:" . $_POST['email']

What are your thoughts on this issue?

Thanks,

<?php
// Email Submit
// Note: filter_var() requires PHP >= 5.2.0
if ( isset($_POST['email']) && isset($_POST['name']) && isset($_POST['message']) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ) {

  // detect & prevent header injections
  $test = "/(content-type|bcc:|cc:|to:)/i";
  foreach ( $_POST as $key => $val ) {
    if ( preg_match( $test, $val ) ) {
      exit;
    }
  }

  //send email
 mail( "XYZ****@hotmail.com", "New message from: ".$_POST['name'], $_POST['message']."\nFrom page: ". str_replace("http://", "", $_SERVER['HTTP_REFERER']), "From:" . $_POST['email'] . "\r\n" . "BCC: ABC****@gmail.com" );

}
?>
1
  • You can add an SPF to the DNSrecords, it will help.
    – Chin Leung
    Commented May 19, 2018 at 19:51

1 Answer 1

1

To Make Your PHP mail() Emails Less Spammy Use Headers

$headers .= "Reply-To: The Sender <[email protected]>\r\n";
$headers .= "Return-Path: The Sender <[email protected]>\r\n";
$headers .= "From: The Sender <[email protected]>\r\n";

But headers are good for more than just setting details about the sender. They are also important for setting the content type, the email priority, and more. Here are how some additional headers look.

$headers .= "Organization: Sender Organization\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n"

Be sure to replace the fourth parameter with the $headers variable as shown below.

mail("[email protected]", "Message", "A simple message.", $headers);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.