PHP和Ajax表单发送电子邮件,但电子邮件未发送

时间:2018-11-14 14:51:47

标签: php ajax email

我使用HTML和Ajax从我的网站发送电子邮件,有时会发送该电子邮件,但90%的时间未发送。邮件甚至不在电子邮件的垃圾邮件中。这是我的代码,我在我的网站上已有大约2年的时间,但我从不认为它不起作用,请帮助:

html:

       <form id="contact-form" class="contact-form" action="#">
            <p class="contact-name">
                <input id="contact_name" type="text" placeholder="Full Name" value="" name="name" />
            </p>
            <p class="contact-email">
                <input id="contact_email" type="text" placeholder="Email Address" value="" name="email" />
            </p>
            <p class="contact-message">
                <textarea id="contact_message" placeholder="Your Message" name="message" rows="10" cols="40"></textarea>
            </p>
            <p class="contact-submit">
                <a id="contact-submit" class="submit" href="#">Send Your Email</a>
            </p>

            <div id="response">

            </div>
        </form>

Ajax(fichier main.js):

$("#contact-submit").on('click',function() {
    $contact_form = $('#contact-form');

    var fields = $contact_form.serialize();

    $.ajax({
        type: "POST",
        url: "http://musamr.com/wp-content/themes/musamr/include/php/contact.php",
        data: fields,
        dataType: 'json',
        success: function(response) {

            if(response.status){
                $('#contact-form input').val('');
                $('#contact-form textarea').val('');
            }

            $('#response').empty().html(response.html);
        }
    });
    return false;
});

contact.php

<?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 = 'monemail@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();

?>

1 个答案:

答案 0 :(得分:0)

邮件功能极其不可靠。您应该考虑使用SMTP服务器和类似PHPMAILER的库来简化集成。

phpmailer:https://github.com/PHPMailer/PHPMailer

邮件功能将尝试发送邮件,但是多次发送到用户的垃圾邮件文件夹中,否则目的地可能会拒绝它。

也可以使用该正则表达式代替该正则表达式 $db