防止php邮件发送到垃圾邮件gmail

时间:2017-04-05 21:16:11

标签: php email spam

这是我用来从网站上的联系表单发送电子邮件的完整代码。 尽管使用了所有最佳实践(标题等),但结果如下:

1)向用户发送的确认电子邮件每次都会收到垃圾邮件,并且会被视为网络钓鱼

2)管理员电子邮件被标记为网上诱骗

有人可以帮助我吗?谢谢!

<?
// -----------------------------------------------------------------------------
// Website info

    $SiteName = "www.example.com";
    $SiteWork = "Enterprise Name";
    $SiteMin = "Ent. name";
    $SiteEmail = "info@example.com";
    $SecondaryEmail = "admin@example.com";
    $ThankYouMessage = "$SiteMin - Message sent!";
    $SiteTel = "Tel (+39) 0XX.XXXXXXX";
    $SiteFax = "Fax (+39) 0XX.XXXXXXX";
    $SiteSocial = "#...";
    $SiteAddress = "...";

// -----------------------------------------------------------------------------
// Retrieve contents

    $UserSubject = $_POST['UserSubject'];
    $UserName = $_POST['UserName'];
    $UserCity = $_POST['UserCity'];
    $UserEmail = $_POST['UserEmail'];
    $UserComments = $_POST['UserComments'];
    $UserAuth = $_POST['UserAuth'];

// -----------------------------------------------------------------------------
// Set up user message

    $UserMessage = "<html><head></head><body>";
    $UserMessage .= "<font size='6px'>";
    $UserMessage .= "Ciao $UserName,";
    $UserMessage .= "</font><br><br>";
    $UserMessage .= "<font size='4px'>";
    $UserMessage .= "abbiamo ricevuto il tuo messaggio.<br>";
    $UserMessage .= "Grazie per averci scritto, ti risponderemo al più presto.<br><br>";
    $UserMessage .= "</font><br>";
    $UserMessage .= "<img src='http://www.piazzaimpianti.it/img/logo.svg' width='60px'><br><br>";
    $UserMessage .= "$SiteWork $SiteAddress<br>";
    $UserMessage .= "$SiteTel - $SiteName - $SiteSocial<br>";
    $UserMessage .= "$SiteName<br>";
    $UserMessage .= "</body></html>";

    $UserHeaders = "From: $SiteEmail\r\n";
    $UserHeaders .= "Reply-To: $SiteEmail\r\n";
    $UserHeaders .= "Return-Path: $SiteEmail\r\n";
    $UserHeaders .= "CC:\r\n";
    $UserHeaders .= "BCC:\r\n";
    $UserHeaders .= "MIME-Version: 1.0\r\n";
    $UserHeaders .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
    $UserHeaders .= "X-Priority: 3\r\n";
    $UserHeaders .= "X-Mailer: PHP". phpversion() ."\r\n";

// -----------------------------------------------------------------------------
// Set up admin message

    $AdminMessage = "Messaggio:\n";
    $AdminMessage .= "\n";
    $AdminMessage .= "$UserComments\n";
    $AdminMessage .= "\n\n";
    $AdminMessage .= "-------------------------------------------------\n";
    $AdminMessage .= "Dati utente:\n";
    $AdminMessage .= "-------------------------------------------------\n";
    $AdminMessage .= "$UserName \n";
    $AdminMessage .= "$UserEmail \n";
    $AdminMessage .= "$UserCity \n";
    $AdminMessage .= "-------------------------------------------------\n";
    $AdminMessage .= "Autorizzi il trattamento dei dati ai sensi del D.lgs.196/03 (*)? $UserAuth \n";
    $AdminMessage .= "-------------------------------------------------\n";
    $AdminMessage .= "Puoi rispondere al messaggio di $UserName\n";
    $AdminMessage .= "scrivendo all'indirizzo: $UserEmail\n\n";

// -----------------------------------------------------------------------------
// Send confirmation to contact page

    $array['Sent'] = array('payload' => 'sent');
    echo json_encode($array);

// -----------------------------------------------------------------------------
// Send the emails

// confirmation email to user
    mail($UserEmail, $ThankYouMessage, $UserMessage, $UserHeaders); 

// email to admin
    mail("$SiteEmail", "$UserSubject", $AdminMessage, "From: $UserEmail"); 

?>

2 个答案:

答案 0 :(得分:2)

您需要使用标头。这对我有用:

$to = "mail@example.com";
$subject = "your subject";
$body = "<p>Your Body</p>";
$headers  = "From: Sender Name <mail2@example.com>" . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
mail($to, $subject, $body, $headers);

此外,您可以在注册商处为您的域名添加TXT Record SPF。转到DNS设置并添加以下TXT记录:

输入:TXT
主持人:example.com
价值:v=spf1 ip4:YOUR.SERVER.IP.HERE ~all
TTL:Automatic

这用于验证邮件是否已被欺骗。

答案 1 :(得分:1)

您无法明确控制您的电子邮件是否被标记为垃圾邮件,但您可以采取一些措施来提供帮助。

  • 确保您的反向DNS正确无误。
  • 实施SPF
  • 实施DKIM
  • 在本地安装SpamAssassin并通过详细模式运行您的邮件样本,以了解他们可能会被标记的原因。
  • 使用MailChimpMailGun等服务代替本地送货员。