PHP e-mail to spam

不想你离开。 提交于 2019-12-02 12:17:09

1) Check headers. You could use any email sending library such as PHPMailer (http://code.google.com/a/apache-extras.org/p/phpmailer/wiki/PHPMailer#Documentation_and_Resources)

2) Check hosting server. If your is using shared hosting then most probably it has been blacklisted by the email domain.

Most of the mail servers will do Reverse DNS lookup to prevent people from domain.com pretending to be from otherdomain.com. It will check if the IP address from which the email was sent resolves to the same domain name of the email sender. Yahoo and other big companies will also use DKIM to verify you.

Often your message can end up in Bulk/Spam if it doesn't have much content, or if you sent a lot of the same content to one server.

Here's a good article about what web developers should know about sending email that might help you understand the subject.

Configure an email address on your domain, replace me@mydomain.com with your newly created email address on your domain andid@hotmailOrgmail.com with your Hotmail/Gmail id in the following script.

Also replace Your Name with your name in the following script and test it on your server:

<?php

    $myName = "Your Name";
    $myEmailAddressonDomain = "me@mydomain.com";
    $myPreferredEmailAddresson  = "id@hotmailOrgmail.com";
    $mail = $_POST['email_field'];
    $clientName = $_POST['name_field'];
    $subject = $_POST['subject_field'];
    $text = $_POST['message_field'];

    $headers = 'From: "$name" <$yourEmailAddressonDomain>'.PHP_EOL.'Reply-To: '.$_POST['mail'].PHP_EOL;
    $to = '"$yourname" <$myPreferredEmailAddresson>';
    $message = $text.PHP_EOL.PHP_EOL."---".PHP_EOL."From: ".$name." <".$mail.">";

    /* Server-side form validations */
    $err = "Error with ";


    if (!checkLen($name)) {
        $err.='Name';
    } else if (!checkLen($mail) && !checkEmail($mail)) {
        $err.='Email';
    } else if (!checkLen($subject)) {
        $err.='Subject';
    } else if (!checkLen($text)) {
        $err.='Message';
    }

    if (strlen($err)>11) {
        echo $err.' field';
        exit;
    }
    /* end validations */

    elseif (mail($to, $subject,$message, $headers)) {
        echo "<span style='color: #336600'>Your message has been sent.</span>";
    } else {
        echo "An error occurred, please try again.";
    }

    function checkLen($str,$len=1)
    {
        return isset($str) && mb_strlen(strip_tags($str),"utf-8") > $len;
    }

    function checkEmail($str)
    {
        return preg_match("/^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $str);
    }


?>

The email will land on your Hotmail/Gmail inbox (or any non-spam) folder via your domain's email address.

Note: Clicking Reply in the received email would show you the client's email address (as we have set in Reply-To header above)

Make appropriate changes and you are good to go.

as you are operating VPS, you may consider setting up DKIM and SPF on your server, they are used by mail services like Gmail to classify your server as a legitimate server.

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