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

别等时光非礼了梦想. 提交于 2019-12-06 14:50:30

问题


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" );

}
?>

回答1:


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);


来源:https://stackoverflow.com/questions/19091847/avoid-emails-being-considered-as-spam-when-sent-via-a-php-contact-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!