简体   繁体   中英

Avoid emails being considered as spam when sent via a PHP contact form

I'm using the code below to send emails via a contact form. Issue is that the emails go to the spam box every time (in outlook, gmail, etc). I suspect that this due to the fact that there's a url (the web page URL) in the body of the e-mail. Therefore I was wondering if there's some workaround (apart for tagging these emails as non-spam in gmail and outlook) in order to keep the URL (I want to keep it) but have the emails not considered as spam. Maybe by re-constructing the URL so that it does not look like a URL? Surely big companies have tips & tricks for that? Many 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( "dsfds@sfss.com", "Nouveau message de: ".$_POST['name'], $_POST['message'] ."\n From site: ". $_SERVER['HTTP_REFERER']., "From:" . $_POST['email'] . "\r\n" . "BCC: dsfds@gmail.com" );

}
?>

Send headers with it, like this:

$to      = 'example@example.com';
$header = "From: noreply@example.com\r\n"; 
$header.= "MIME-Version: 1.0\r\n"; 
$header.= "Content-Type: text/html; charset=utf-8\r\n"; 
$header.= "X-Priority: 1\r\n"; 

mail($to, $subject, $message, $header);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM