首页 > 解决方案 > wordpress wp_mail 抛出错误:无法实例化邮件功能

问题描述

我的网站上有两个表格。两者都工作正常。但突然现在他们不工作了。电子邮件不是来自这两种形式。

// 当用户点击提交按钮时这个函数被调用

    <script type="text/javascript">
      var $ = jQuery;
            function sendMessage(){
            var name = $('[name="name"]').val();
            var email = $('[name="email"]').val();
            var phone = $('[name="phone"]').val();
            var message = $('[name="message"]').val();
            var filter = /^[7-9][0-9]{9}$/;
            var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

            if(name == '')
            {
                $('.name-error').show();
            }
            else
            {
                $('.name-error').hide();
            }
            if(email == '')
            {
                $('.email-error').show();
            }
            else
            {
                $('.email-error').hide();
            }
            if(phone == '')
            {
                $('.phone-error').show();
            }
            else
            {
                $('.phone-error').hide();
            }
            if(message == '')
            {
                $('.message-error').show();
            }
            else
            {
                $('.message-error').hide();
            }

            if(name != '' && email != '' && phone != '' && message != ''){
                if(emailReg.test(email)){
                    if(filter.test(phone)){
                        var data = {
                            url:        '<?php echo admin_url('admin-ajax.php');?>',
                            type:       'POST',
                            action:     'send_mails',
                            formdata: $("#contact").serialize()
                        };

                        var myRequest = jQuery.post(data.url, data, function(response){
                            console.log(response)
                        });

                        myRequest.done(function(){
                            $('#msg').html("<p class='msg' style='background-color:#dff0d8; border-color: #d6e9c6; color:#3c763d; padding:10px; border-radius:4px;'>The Email has been sent successfully!</p>").fadeIn('slow');
                            $('#msg').delay(5000).fadeOut('slow');
                        });
                        $("#contact").trigger("reset");
                        $('#msg').html("<p class='msg' style='background-color:#dff0d8; border-color: #d6e9c6; color:#3c763d; padding:10px; border-radius:4px;'>The Email has been sent successfully!</p>").fadeIn('slow');
                            $('#msg').delay(5000).fadeOut('slow');
                    }
                    else
                    {
                        alert("Provide valid mobile number");
                    }
                }
                else
                {
                    alert("Provide valid Email Id");
                }
            }
            else
            {
                // alert("Kindly enter all the details");
                return false;
            }

        } 
    </script>

这是functions.php文件中的函数。

    function send_mails()
    {
        $formData = $_POST['formdata'];
        $data = array();
        parse_str($formData, $data);

        $name = $data['name'];
        $email = $data['email'];
        $phone = $data['phone'];
        $message = $data['message'];

        $msg = "A new enquiry has been posted on the website\n";
        $msg.= "Name: ".$name."\n";
        $msg.= "Email: ".$email."\n";
        $msg.= "Phone: ".$phone."\n";
        $msg.= "Message: ".$message."\n";

        $headers = array('Content-Type: text/html; charset=UTF-8');

        //echo $msg;

        $admin_email = get_option( 'admin_email' );
        //  print_r($admin_email);
        $res = wp_mail('email',"Enquiry",$msg,$headers);
        if($res)
        {
            echo "Mail has been sent";

        }
        else
        {
            echo "Could not send mail";
            debug_wpmail($res);
        }

        die();
    }

    add_action('wp_ajax_send_mails', 'send_mails');
    add_action('wp_ajax_nopriv_send_mails', 'send_mails');

debug_wpmail 返回错误“无法实例化邮件功能”。我在谷歌上没有找到任何东西。

if ( ! function_exists('debug_wpmail') ) :
    function debug_wpmail( $result = false ) {
        if ( $result )
            return;
        global $ts_mail_errors, $phpmailer;
        if ( ! isset($ts_mail_errors) )
            $ts_mail_errors = array();
        if ( isset($phpmailer) )
            $ts_mail_errors[] = $phpmailer->ErrorInfo;
        print_r('<pre>');
        print_r($ts_mail_errors);
        print_r('</pre>');
    }
endif;

标签: wordpressemail

解决方案


你能修改这部分吗:

$res = wp_mail('email',"Enquiry",$msg,$headers);

$res = wp_mail($admin_email,'Enquiry',$msg,$headers);

推荐阅读