PHP e-mail to spam

2020-05-07 15:47发布

问题:

I'm trying to email new registered users for email verification (PHP) but i don't get it, why would an email be sent to SPAM, i already checked out similar questions and all answers are about Headers,

It seems a bit complicated for me to get known to those headers and how are they being verified,
By sender website ? lets say i sent as user@google.com and the actual server domain is domain.com, how would it know? and is it one of the main reasons why it goes to spam ?
I am using VPS, does it has anything to do with it ?
I'm just trying to understand the clear/simple reasons of why would an email be checked as spam

and what if i sent from the server IP and not the domain itself

回答1:

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.



回答2:

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.



回答3:

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.



回答4:

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.