首页 文章

PHP邮件功能无法正常工作[重复]

提问于
浏览
2

这个问题在这里已有答案:

<?php
/*
* Contact Form Class
*/


header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');

$admin_email = 'sidvangala@gmail.com'; // Your Email
$message_min_length = 5; // Min Message Length


class Contact_Form{
    function __construct($details, $email_admin, $message_min_length){

        $this->name = stripslashes($details['name']);
        $this->email = trim($details['email']);
        $this->subject = 'Contact from Your Website'; // Subject 
        $this->message = stripslashes($details['message']);

        $this->email_admin = $email_admin;
        $this->message_min_length = $message_min_length;

        $this->response_status = 1;
        $this->response_html = '';
    }


    private function validateEmail(){
        $regex = '/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i';

        if($this->email == '') { 
            return false;
        } else {
            $string = preg_replace($regex, '', $this->email);
        }

        return empty($string) ? true : false;
    }


    private function validateFields(){
        // Check name
        if(!$this->name)
        {
            $this->response_html .= '<p>Please enter your name</p>';
            $this->response_status = 0;
        }

        // Check email
        if(!$this->email)
        {
            $this->response_html .= '<p>Please enter an e-mail address</p>';
            $this->response_status = 0;
        }

        // Check valid email
        if($this->email && !$this->validateEmail())
        {
            $this->response_html .= '<p>Please enter a valid e-mail address</p>';
            $this->response_status = 0;
        }

        // Check message length
        if(!$this->message || strlen($this->message) < $this->message_min_length)
        {
            $this->response_html .= '<p>Please enter your message. It should have at least '.$this->message_min_length.' characters</p>';
            $this->response_status = 0;
        }
    }


    private function sendEmail(){
        $mail = mail($this->email_admin, $this->subject, $this->message,
             "From: ".$this->name." <".$this->email.">\r\n"
            ."Reply-To: ".$this->email."\r\n"
        ."X-Mailer: PHP/" . phpversion());

        if($mail)
        {
            $this->response_status = 1;
            $this->response_html = '<p>Thank You!</p>';
        }
    }


    function sendRequest(){
        $this->validateFields();
        if($this->response_status)
        {
            $this->sendEmail();
        }

        $response = array();
        $response['status'] = $this->response_status;   
        $response['html'] = $this->response_html;

        echo json_encode($response);
    }
}


$contact_form = new Contact_Form($_POST, $admin_email, $message_min_length);
$contact_form->sendRequest();

?>

邮件功能是发送邮件,如果我删除“从:” . $ this-> name . “<” . $ this-> email . “> \ r \ n” . “回复:” . $ this- >电子邮件 . “\ r \ n” . “X-Mailer:PHP /” . phpversion());但我也需要这些,我在哪里犯错误 .

2 回答

  • 0

    我认为需要改变你的 Headers 如下

    <?php
    /*
    * Contact Form Class
    */
    
    
    header('Cache-Control: no-cache, must-revalidate');
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
    header('Content-type: application/json');
    
    $admin_email = 'sidvangala@gmail.com'; // Your Email
    $message_min_length = 5; // Min Message Length
    
    
    class Contact_Form{
        function __construct($details, $email_admin, $message_min_length){
    
            $this->name = stripslashes($details['name']);
            $this->email = trim($details['email']);
            $this->subject = 'Contact from Your Website'; // Subject 
            $this->message = stripslashes($details['message']);
    
            $this->email_admin = $email_admin;
            $this->message_min_length = $message_min_length;
    
            $this->response_status = 1;
            $this->response_html = '';
        }
    
    
        private function validateEmail(){
            $regex = '/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i';
    
            if($this->email == '') { 
                return false;
            } else {
                $string = preg_replace($regex, '', $this->email);
            }
    
            return empty($string) ? true : false;
        }
    
    
        private function validateFields(){
            // Check name
            if(!$this->name)
            {
                $this->response_html .= '<p>Please enter your name</p>';
                $this->response_status = 0;
            }
    
            // Check email
            if(!$this->email)
            {
                $this->response_html .= '<p>Please enter an e-mail address</p>';
                $this->response_status = 0;
            }
    
            // Check valid email
            if($this->email && !$this->validateEmail())
            {
                $this->response_html .= '<p>Please enter a valid e-mail address</p>';
                $this->response_status = 0;
            }
    
            // Check message length
            if(!$this->message || strlen($this->message) < $this->message_min_length)
            {
                $this->response_html .= '<p>Please enter your message. It should have at least '.$this->message_min_length.' characters</p>';
                $this->response_status = 0;
            }
        }
    
    
        private function sendEmail(){
            /*$mail = mail($this->email_admin, $this->subject, $this->message,
                 "From: ".$this->name." <".$this->email.">\r\n"
                ."Reply-To: ".$this->email."\r\n"
            ."X-Mailer: PHP/" . phpversion());*/
    
            $headers  = "MIME-Version: 1.0\r\n";
            $headers .= "Content-type: text/html; charset=utf-8\r\n";
            $headers .= "To: ".$this->email_admin." <".$this->email_admin.">\r\n";
            $headers .= "From: ".$this->name." <".$this->email.">\r\n";
            $mail = mail($this->email_admin, $this->subject, $this->message,$headers);
    
            if($mail)
            {
                $this->response_status = 1;
                $this->response_html = '<p>Thank You!</p>';
            }
        }
    
    
        function sendRequest(){
            $this->validateFields();
            if($this->response_status)
            {
                $this->sendEmail();
            }
    
            $response = array();
            $response['status'] = $this->response_status;   
            $response['html'] = $this->response_html;
    
            echo json_encode($response);
        }
    }
    
    
    $contact_form = new Contact_Form($_POST, $admin_email, $message_min_length);
    $contact_form->sendRequest();
    
    ?>
    
  • 0

    我已经检查了你的代码 . 实际上邮件功能正在运行但是邮件被发送到垃圾邮件文件夹 . 添加如下$ $ from =“Name Surname”;并尝试使用smtp邮件功能,以避免垃圾邮件..

相关问题