PHP 表单邮件不会返回带有@yahoo.com 地址的条目

Posted

技术标签:

【中文标题】PHP 表单邮件不会返回带有@yahoo.com 地址的条目【英文标题】:PHP form mail will not return entries with @yahoo.com address 【发布时间】:2021-02-09 08:13:22 【问题描述】:

表单邮件期望的结果:姓名、电子邮件地址和消息发送给网站所有者@yahoo.com。

表格问。 . . 姓名: 电子邮件: 留言:

当前状态: 如果将@yahoo 电子邮件地址放在表单的“电子邮件”字段中,(真实的电子邮件地址@yahoo.com)将不会收到电子邮件。如果“电子邮件”字段使用@gmail.com 或@godaddy.com 填写地址,则可以!下面是表单的 html 和 send.php 代码。

感谢您的帮助!

<!-- CONTACT FORM -->           
                    <div id="contact_form" class="animated fadeInDown">
                        
                        <!-- Success Message -->
                        <div class="form-success">
                            <p><i class="fa fa-check"></i>Thank you, your message has been sent!</p>
                        </div>
                        
                        <!-- Begin form -->
                        <div class="contact-form"> 
                            <form action="contact-form/send.php" method="post" class="form">    
                                <div class="name">
                                    <input class="text" type="text" name="name" placeholder="Name"> 
                                </div>
                                <div class="email">  
                                    <input class="text" type="text" name="email" placeholder="Email"> 
                                </div>
                                <div class="message">    
                                    <textarea name="message" rows="8" cols="60" placeholder="Message & Phone Number"></textarea> 
                                                                    
                                </div>
                                
                                <div class="bt-contact">
                                    <a class="btn-color btn-color-1d" id="submit" href="javascript:;"><span>SEND EMAIL</span></a>
                                    
                                </div>
                                    
                                <div class="loading"></div>
                                
                            </form> 
                        </div>
                        <!-- End form -->
                    </div>
                    <!-- END CONTACT FORM -->
<?php
/*------------------------------------
       YOUR EMAIL GOES HERE
--------------------------------------*/
$to = '<a real email address@yahoo.com>';


//Retrieve form data. 
//GET - user submitted data using AJAX
//POST - in case user does not support javascript, we'll use POST instead
$name = ($_GET['name']) ? $_GET['name'] : $_POST['name'];
$email = ($_GET['email']) ?$_GET['email'] : $_POST['email'];
$comment = ($_GET['comment']) ?$_GET['comment'] : $_POST['message'];

//flag to indicate which method it uses. If POST set it to 1
if ($_POST) $post=1;

//Include email validator
    require 'email-validator.php';
    $validator = new EmailAddressValidator();
    
//Simple server side validation for POST data, of course, you should validate the email
if (!$name) $errors[count($errors)] = 'Please enter your name.';
if (!$email) $errors[count($errors)] = 'Please enter your email.'; 
if (!$comment) $errors[count($errors)] = 'Please enter your comment.'; 

$email = strip_tags($email);

if (!$validator->check_email_address($email)) 
    $errors[count($errors)] = 'Invalid email address.'; 


//if the errors array is empty, send the mail
if (!$errors) 

    //sender
    $from = $name . ' <' . $email . '>';
    
    //Structure of the message:
    $subject = 'Message from ' . $name; 
    $message = '
    <!DOCTYPE html>
    <head></head>
    <body>
    <table>
        <tr><td>Name:</td><td>' . $name . '</td></tr>
        <tr><td>Email:</td><td>' . $email . '</td></tr>
        <tr><td>Message:</td><td>' . nl2br($comment) . '</td></tr>
    </table>
    </body>
    </html>';

    //End of the message structure
    
    
    //send the mail
    $result = sendmail($to, $subject, $message, $from);
    
    //if POST was used, display the message straight away
    if ($_POST) 
        if ($result) echo 'Thank you! We have received your message.';
        else echo 'Sorry, unexpected error. Please try again later';
        
    //else if GET was used, return the boolean value so that 
    //ajax script can react accordingly
    //1 means success, 0 means failed
     else 
        echo $result;   
    

//if the errors array has values
 else 
    //display the errors message
    for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br/>';
    echo '<a href="../contact.html">Back</a>';
    exit;



//Simple mail function with HTML header
function sendmail($to, $subject, $message, $from) 
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
    $headers .= 'From: ' . $from . "\r\n";
    
    $result = mail($to,$subject,$message,$headers);
    
    if ($result) return 1;
    else return 0;


?>
<?php

    class EmailAddressValidator 

        /**
         * Check email address validity
         * @param   strEmailAddress     Email address to be checked
         * @return  True if email is valid, false if not
         */
        public function check_email_address($strEmailAddress) 
            
            // If magic quotes is "on", email addresses with quote marks will
            // fail validation because of added escape characters. Uncommenting
            // the next three lines will allow for this issue.
            //if (get_magic_quotes_gpc())  
            //    $strEmailAddress = stripslashes($strEmailAddress); 
            //

            // Control characters are not allowed
            if (preg_match('/[\x00-\x1F\x7F-\xFF]/', $strEmailAddress)) 
                return false;
            

            // Check email length - min 3 (a@a), max 256
            if (!$this->check_text_length($strEmailAddress, 3, 256)) 
                return false;
            

            // Split it into sections using last instance of "@"
            $intAtSymbol = strrpos($strEmailAddress, '@');
            if ($intAtSymbol === false) 
                // No "@" symbol in email.
                return false;
            
            $arrEmailAddress[0] = substr($strEmailAddress, 0, $intAtSymbol);
            $arrEmailAddress[1] = substr($strEmailAddress, $intAtSymbol + 1);

            // Count the "@" symbols. Only one is allowed, except where 
            // contained in quote marks in the local part. Quickest way to
            // check this is to remove anything in quotes. We also remove
            // characters escaped with backslash, and the backslash
            // character.
            $arrTempAddress[0] = preg_replace('/\./'
                                             ,''
                                             ,$arrEmailAddress[0]);
            $arrTempAddress[0] = preg_replace('/"[^"]+"/'
                                             ,''
                                             ,$arrTempAddress[0]);
            $arrTempAddress[1] = $arrEmailAddress[1];
            $strTempAddress = $arrTempAddress[0] . $arrTempAddress[1];
            // Then check - should be no "@" symbols.
            if (strrpos($strTempAddress, '@') !== false) 
                // "@" symbol found
                return false;
            

            // Check local portion
            if (!$this->check_local_portion($arrEmailAddress[0])) 
                return false;
            

            // Check domain portion
            if (!$this->check_domain_portion($arrEmailAddress[1])) 
                return false;
            

            // If we're still here, all checks above passed. Email is valid.
            return true;

        

        /**
         * Checks email section before "@" symbol for validity
         * @param   strLocalPortion     Text to be checked
         * @return  True if local portion is valid, false if not
         */
        protected function check_local_portion($strLocalPortion) 
            // Local portion can only be from 1 to 64 characters, inclusive.
            // Please note that servers are encouraged to accept longer local
            // parts than 64 characters.
            if (!$this->check_text_length($strLocalPortion, 1, 64)) 
                return false;
            
            // Local portion must be:
            // 1) a dot-atom (strings separated by periods)
            // 2) a quoted string
            // 3) an obsolete format string (combination of the above)
            $arrLocalPortion = explode('.', $strLocalPortion);
            for ($i = 0, $max = sizeof($arrLocalPortion); $i < $max; $i++) 
                 if (!preg_match('.^('
                                .    '([A-Za-z0-9!#$%&\'*+/=?^_`|~-]' 
                                .    '[A-Za-z0-9!#$%&\'*+/=?^_`|~-]0,63)'
                                .'|'
                                .    '("[^\\\"]0,62")'
                                .')$.'
                                ,$arrLocalPortion[$i])) 
                    return false;
                
            
            return true;
        

        /**
         * Checks email section after "@" symbol for validity
         * @param   strDomainPortion     Text to be checked
         * @return  True if domain portion is valid, false if not
         */
        protected function check_domain_portion($strDomainPortion) 
            // Total domain can only be from 1 to 255 characters, inclusive
            if (!$this->check_text_length($strDomainPortion, 1, 255)) 
                return false;
            
            // Check if domain is IP, possibly enclosed in square brackets.
            if (preg_match('/^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])'
               .'(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9]))3$/'
               ,$strDomainPortion) || 
                preg_match('/^\[(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])'
               .'(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9]))3\]$/'
               ,$strDomainPortion)) 
                return true;
             else 
                $arrDomainPortion = explode('.', $strDomainPortion);
                if (sizeof($arrDomainPortion) < 2) 
                    return false; // Not enough parts to domain
                
                for ($i = 0, $max = sizeof($arrDomainPortion); $i < $max; $i++) 
                    // Each portion must be between 1 and 63 characters, inclusive
                    if (!$this->check_text_length($arrDomainPortion[$i], 1, 63)) 
                        return false;
                    
                    if (!preg_match('/^(([A-Za-z0-9][A-Za-z0-9-]0,61[A-Za-z0-9])|'
                       .'([A-Za-z0-9]+))$/', $arrDomainPortion[$i])) 
                        return false;
                    
                    if ($i == $max - 1)  // TLD cannot be only numbers
                        if (strlen(preg_replace('/[0-9]/', '', $arrDomainPortion[$i])) <= 0) 
                            return false;
                        
                    
                
            
            return true;
        

        /**
         * Check given text length is between defined bounds
         * @param   strText     Text to be checked
         * @param   intMinimum  Minimum acceptable length
         * @param   intMaximum  Maximum acceptable length
         * @return  True if string is within bounds (inclusive), false if not
         */
        protected function check_text_length($strText, $intMinimum, $intMaximum) 
            // Minimum and maximum are both inclusive
            $intTextLength = strlen($strText);
            if (($intTextLength < $intMinimum) || ($intTextLength > $intMaximum)) 
                return false;
             else 
                return true;
            
        

    

?>

【问题讨论】:

您可以编辑您的问题并添加您的代码吗?我假设您的问题是为什么没有将电子邮件发送到雅虎电子邮件。 感谢您查看此内容!该问题已被编辑并添加了代码。 也包括您的validator.php 文件。可能是问题的原因 您的电子邮件很可能以垃圾邮件的形式到达,因为您发送的域与发件人标头中的域不匹配。不是代表用户发送,而是通过 Yahoo 的 SMTP 服务器中继电子邮件 好的,我试试,谢谢! 【参考方案1】:

如果您“从”任意地址发送邮件这几乎肯定会被阻止,因为这称为“欺骗”并且很容易被检测为非法。

您需要从您拥有和控制的地址发送。

Yahoo 对 SPF 记录非常严格,对 DKIM 非常严格,因此请确保正确设置和验证这两个记录。

每当您设置电子邮件表单工具时,请绝对确定不会有人劫持此工具以将任意内容发送到任意地址。如果这是可能的,那么某人将不可避免地发现这一点并用它来发送未经请求的邮件。

【讨论】:

感谢@tadman 看到这个。我在网站上填写表格时使用了合法的 yahoo 电子邮件地址,它们不会到达在 send.php 脚本中指定的 To: yahoo 地址。来自其他域的所有其他电子邮件测试都有效。 您无法通过自己的服务器发送电子邮件From: ...@yahoo.com。这是欺骗,将被阻止。您必须从您控制且可以正确配置的域发送

以上是关于PHP 表单邮件不会返回带有@yahoo.com 地址的条目的主要内容,如果未能解决你的问题,请参考以下文章

带有单选按钮的 PHP 邮件表单使用 AJAX 发送

通过电子邮件发送带有 PHP 和 HTML 错误消息“Cannot POST mail.php”的表单

openshift 电子邮件从终端发送,但不是从 php

使用 jQuery AJAX PHP 的表单不会在电子邮件中发送字段输入

在 javascript 中检测错误输入的电子邮件地址

(Tcl) 通过 gmail 和 yahoo 邮件服务器发送电子邮件