prevent php mail sent going to spam gmail [duplicate]

我的梦境 提交于 2021-02-05 12:10:16

问题


this is my complete code that I use to send emails from a contact form on a website. Despite all the best practices used (headers etc), the results are:

1) the confirmation email to users goes in gmail spam every time and it is flaged as phishing

2) the admin email is flagged as phishing

Could someone help me? Thank you!

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

?>

回答1:


You need to use headers. This worked for me:

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

Additionally you can add a TXT Record for SPF in your Registrar where you got your domain. Go to the DNS Settings and add the following TXT Record:

Type: TXT
Host: example.com
Value: v=spf1 ip4:YOUR.SERVER.IP.HERE ~all
TTL: Automatic

This is for validating that the mail hasn't been spoofed.




回答2:


You can't explicitly control if your emails get flagged as spam, but there are some things you can do to help.

  • Make sure your reverse DNS is correct.
  • Implement SPF.
  • Implement DKIM.
  • Install SpamAssassin locally and run a sample of your messages through it with verbose mode on to get an idea of why they might be getting flagged.
  • Use a service like MailChimp or MailGun instead of a local deliverer.


来源:https://stackoverflow.com/questions/43241773/prevent-php-mail-sent-going-to-spam-gmail

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