php和电子邮件问题

时间:2014-10-27 17:15:04

标签: php sendmail

最近我发现我不能再使用我在“发件人:”标题中选择的任何电子邮件发送电子邮件,除非该域名具有匹配的TXT / SPF记录。否则,一些垃圾邮件代理机构会将您的IP#标记为顽皮。

所以,这就是我的想法 - 有一个功能可以检查电子邮件地址域名是否具有匹配的共享相同IP号码的TXT记录。

基本上,我们正在寻找这个TXT记录:

> v=spf1 include:_spf.google.com ip4:199.###.184.26 ~all

并试图查看该IP号码是否与我们的号码匹配。

所以结果可能如下所示:

  

来自:mark@matchingIpNumber.com

     

来自:marky edwards \< mark@matchingIpNumber.com>

     

发件人:apache@serverDomainName.com \ r \ n回复:mark@noMatchingIpNumber.com

     

来自:marky edwards \< apache@serverDomainName.com> \ r \ n回复:标记爱德华兹   \< mark@noMatchingIpNumber.com>

我的问题:有没有其他人遇到过这个问题,如果有的话,它是如何处理的?

这是我正在考虑解决这个问题的php函数:

function checkForDomainEmailMatch()     {
                /*      usage:  emailAddress
                                emailAddress name(s)

                        returns:
                                From: mark@edwardsmark.com
                                -- or --
                                From: Marky Edwards <mark@edwardsmarkf.com>
                                ReplyTo: <mark@edwardsmark.com

                */
        $input = func_get_args();

        $emailAddr      = null          ;
        $personName     = null          ;
        $ipNbr          = null          ;
        $returnResult   = 'From: '      ;

                // did we get one parameter or several ?
        if  ( is_array($input)  )       {
                $emailAddr      = array_shift($input);
                $personName     = trim(implode (' ', $input));
        } else {
                $emailAddr      = $input        ;
        }

                // get the IP number
        if (  array_key_exists('SERVER_ADDR', $_SERVER) )       {
                        // are we running locally or through apache?
                $ipNbr = $_SERVER['SERVER_ADDR'];
        } else {
                $ipNbr = gethostbyname(gethostname());
        }

        $domain = preg_replace('/^.*@/','',$emailAddr); 
            // strip out email prefix, just use domain name

        $ipNbrMatch = '/' . $ipNbr . '/' ;
        $dnsRecords =  dns_get_record($domain, DNS_TXT) ;
                        // EXAMPLE: v=spf1 include:_spf.google.com ip4:199.231.184.26 ~all
        $dnsRecordIpNbr = null;

        foreach ( $dnsRecords as $dnsRecord )     {
                if  ( array_key_exists('txt', $dnsRecord ))     {
                        $dnsRecordIpNbr = $dnsRecord['txt']     ;
                        break;
                }
        }


        if  ( $personName )
                $returnResult   .=      $personName
                                .       '<'
                                ;

        if  ( $dnsRecordIpNbr && preg_match($ipNbrMatch, $dnsRecordIpNbr))      {
                        // our TXT IP number matches our server
                $returnResult   .= $emailAddr ;
                if  ( $personName )
                        $returnResult   .=      '>';
        } else {
                        // our TXT IP number does not match our server
                $domainName = null;
                                // looking for edwardsmarkf.info   ;-)
                $pattern[] = $ipNbrMatch;
                $pattern[] = '/ /';
                $domainName     = preg_replace($pattern, '', exec('head -1 /etc/hosts')) ;

                        // default email address
                $returnResult   .=      get_current_user()
                                .       '@'
                                .       $domainName
                                ;

                if  ( $personName )
                        $returnResult   .=      '>';

                $returnResult   .=      "\r\n"
                                .       'ReplyTo: '
                                ;

                if  ( $personName )     {
                        $returnResult   .=      $personName
                                        .       '<'
                                        ;
                }


                $returnResult   .=      $emailAddr      ;

                if  ( $personName )
                        $returnResult   .=      '>';

                $returnResult   .=      "\r\n"  ;
        }

        return $returnResult;
}

任何想法或建议都将不胜感激。

1 个答案:

答案 0 :(得分:0)

这似乎工作得很好 - 它适用于gmail。

 function checkForDomainEmailMatch()     {
                /*      checkForDomainEmailMatch        2014-10-27

                        usage:  emailAddress
                                emailAddress name(s)

                        returns:
                                From: mark@edwardsmark.com
                                -- or --
                                From: Marky Edwards <mark@edwardsmarkf.com>
                                Reply-To: <mark@edwardsmark.com

                */

        define('DEFAULT_EMAIL'   ,       'info@comptonpeslonline.com'    );

        $input = func_get_args();

        $emailAddr      = null          ;
        $personName     = null          ;
        $ipNbr          = null          ;
        $returnResult   = 'From: '      ;

                // did we get one parameter or several ?
        if  ( is_array($input)  )       {
                $emailAddr      = array_shift($input);
                $personName     = trim(implode (' ', $input));
        } else {
                $emailAddr      = $input        ;
        }

                        // get the IP number
        if (  array_key_exists('SERVER_ADDR', $_SERVER) )       {
                        // are we running locally or through apache?
                $ipNbr = $_SERVER['SERVER_ADDR'];
        } else {
                $ipNbr = gethostbyname(gethostname());
        }

        $domain = preg_replace('/^.*@/','',$emailAddr);
                        // strip out email prefix, just use domain name

        $dnsRecords =  dns_get_record($domain, DNS_TXT) ;
                        // EXAMPLE: v=spf1 include:_spf.google.com ip4:199.231.184.26 ~all

        $dnsRecordIpNbr = null;
        foreach ( $dnsRecords as $dnsRecord )     {
                if  ( array_key_exists('txt', $dnsRecord ))     {
                        $dnsRecordIpNbr = $dnsRecord['txt']     ;
                        break;
                }
        }

        $ipNbrMatch = '/' . $ipNbr . '/' ;
        if  ( $dnsRecordIpNbr && preg_match($ipNbrMatch, $dnsRecordIpNbr))      {
                        // our TXT IP number matches our server
                if  ( $personName )
                        $returnResult   .=      $personName
                                        .       ' <'
                                        ;
                $returnResult   .= $emailAddr ;
                if  ( $personName )
                        $returnResult   .=      '>'             ;
        } else {
                if  ( $personName )     {
                        $returnResult   .=      $personName     ;
                } else {
                        $returnResult   .=      $emailAddr      ;
                }
                $returnResult           .=      ' <'             ;

                        // our TXT IP number does not match our server
                $domainName = null;
                                // looking for edwardsmarkf.info   ;-)
                $pattern[] = $ipNbrMatch;
                $pattern[] = '/ /';
                $domainName     = preg_replace($pattern, '', exec('head -1 /etc/hosts')) ;

                        // default email address
                if  ( constant('DEFAULT_EMAIL') )        {
                        $returnResult   .=      constant('DEFAULT_EMAIL')        ;
                } else {
                        $returnResult   .=      get_current_user()
                                        .       '@'
                                        .       $domainName
                                        ;
                }

                $returnResult   .=      '>';


                $returnResult   .=      "\r\n"
                                .       'Reply-To: '
                                ;

                $returnResult   .=      $emailAddr      ;

                $returnResult   .=      "\r\n"  ;
        }

        return $returnResult;
}

请注意,我只是尝试向自己发送一张贝宝发票作为测试,这就是我所看到的:

  

To:mark edwards \&lt; mark_f_edwards@oldEmailAddress.com>

     

来自:&#34; marksPaypalAccount.com LLC通过PayPal&#34; \&LT; member@paypal.com>

     

回复:info@marksActualDomain.com