php电子邮件 - 如何避免邮件最终进入垃圾邮件箱 [英] php email - how to avoid mail ending up in spam box

查看:27
本文介绍了php电子邮件 - 如何避免邮件最终进入垃圾邮件箱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站上有一个联系表格,它确实有效:D问题是,电子邮件最终会出现在垃圾邮件文件夹中.我试图对标题部分做一些事情,但似乎没有任何效果.

I have a contact form on my website, which actually works :D The problem is, that the email ends up in the spam folder. I have tried to do stuff with the header section, but nothing seems to work.

谁能帮我解决这个问题?

Can anyone help me with this?

谢谢

<?php
    ini_set("display_errors", "0");
    $post_data = filter_input_array( INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS );

    $name = $post_data["name"];
    $email = $post_data["email"];
    $phone = $post_data["phone"];
    $website = $post_data["website"];
    $message = $post_data["message"];



    # select data that needs validation
    $validate = array(
        'required'  => array($name,$email,$message),
        'validEmail'    => array($email),
        'validNumber'   => array($phone),
        'validAlpha'    => array($name)
    );
    $formcontent = "Name: $name 
E-Mail: $email 
Phone: $phone 
Website: $website 
Message: $message 
";
    $formcontent = wordwrap($formcontent, 70, "
", true);

    $recipient = "thomas.teilmann@gmail.com"; 
    $subject = "Contact Messenbrink.eu"; 

    /*$mailheader = "From: $email 
";**/

    $mailheader .= "Reply-To: $name <$email>
"; 
    $mailheader .= "Return-Path: $name <$email>
"; 
    $mailheader .= "Content-Type: text/plain
"; 
    $mailheader .= "Organization: Sender Organization
";
    $mailheader .= "MIME-Version: 1.0
";
    $mailheader .= "Content-type: text/plain; charset=iso-8859-1
";
    $mailheader .= "X-Priority: 3
";
    $mailheader .= "X-Mailer: PHP". phpversion() ."
";
    $mailheader .= "From: $name <$email>
";  

    function sendMail() {
        global $formcontent, $recipient, $subject, $mailheader;
        mail($recipient, $subject, $formcontent, $mailheader);
    }

    # error messages
    $errorsMsgs = array(
        'required'  => 'Please fill out all required fields.',
        'validEmail'    => 'is an invalid email address.',
        'validNumber'   => 'is an invalid number.',
        'validAlpha'    => 'contains invalid characters. This field only accepts letters and spaces.'
    );

    $errorMarkup    = "<h1>We found a few errors :-(</h1><h2>Please fix these errors and try again</h2><ol>";
    $errorMarkupEnd = "</ol>";
    $successMarkup  = "<h1>Success!</h1><h2>Your form was sent successfully.</h2>";
    $backMarkup     = "<a href="" . $_SERVER['HTTP_REFERER'] . "">Back to form</a>";

    # begin state
    $valid = true;

    # loop through fields of error types
    foreach ($validate as $type => $fields) {
        # loop through values of fields to be tested
        foreach ($fields as $value) {
            # throw error if value is required and not entered
            if ($type === 'required' && strlen($value) === 0) {
                $errorMarkup .= "<li>$errorsMsgs[$type]</li>";
                $valid = false;
                break;
            }
            else if (
                $type === 'validEmail'  && !filter_var($value, FILTER_VALIDATE_EMAIL) ||
                $type === 'validNumber' && !preg_match('/^[0-9 ]+$/', $value) ||
                $type === 'validAlpha'  && !preg_match('/^[a-zA-Z ]+$/', $value)
            ) {
                if (strlen($value) === 0) {break;} # skip check if value is not entered
                $errorMarkup .= "<li>"$value" $errorsMsgs[$type]</li>";
                $valid = false;
                continue;
            }
        }
    }

    if ($valid) {
        sendMail();
        $body = $successMarkup . $backMarkup;
        $title = "Form sent";
    } else {
        $body = $errorMarkup . $errorMarkupEnd . $backMarkup;
        $title = "Form errors";
    }

    # write html ouput
    echo "<!DOCTYPE html><head><title>$title</title><style type="text/css">body{margin:100px;font:16px/1.5 sans-serif;color:#111}h1{font-size:32px;margin:0;font-weight:bold}h2{font-size:18px;margin:0 0 20px 0}ol,li{list-style-position:inside;padding-left:0;margin-left:0}</style></head><body>$body</body></html>";
?>

推荐答案

它与 PHP 代码无关,但要确保电子邮件是从托管在服务器上的域发送的.这意味着使用您的 gmail 地址通过 PHP 发送将是一个坏主意.每封离开服务器的电子邮件都需要签名并需要有 SPF 记录

It has nothing to do with the PHP code but make sure that the email are being sent from a domain that is hosted on the server. That means it would be a bad idea to use your gmail address for sending though PHP. Every email that leaves the server needs to be signed and needs to have SPF records

这通常是因为在 DNS 中没有设置正确的 SPF 记录.这可以轻松完成,具体取决于您使用的软件.如果您使用 cPanel,那么这几乎是一个两步过程.

This is usually because in the DNS a correct SPF record has not been setup. This can be easily done depending on the software you use. If you use cPanel then this is pretty much a two step process.

一些链接:http://www.emailquestions.com/help-desk/2418-why-do-my-own-emails-go-into-spam.htmlhttp://www.google.co.uk/search?sourceid=chrome&ie=UTF-8&q=spf+recordshttp://www.techtalkpoint.com/articles/setting-up-spf-records-in-cpanel-whm-and-namecheap/

Some links: http://www.emailquestions.com/help-desk/2418-why-do-my-own-emails-go-into-spam.html http://www.google.co.uk/search?sourceid=chrome&ie=UTF-8&q=spf+records http://www.techtalkpoint.com/articles/setting-up-spf-records-in-cpanel-whm-and-namecheap/

这篇关于php电子邮件 - 如何避免邮件最终进入垃圾邮件箱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆