【问题标题】:wordpress wp_mail throws error: could not instantiate mail functionwordpress wp_mail 抛出错误:无法实例化邮件功能
【发布时间】:2019-12-02 05:25:13
【问题描述】:

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

// 当用户点击提交按钮时调用该函数

    <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;

【问题讨论】:

  • 能否请您检查您的网络是否有 AJAX 响应?
  • @TejasGajjar 我在网络选项卡中得到这个 - 无法发送邮件
    数组([0] => 无法实例化邮件功能。)
  • 能否请您将此行更改为 url:“”,您在其中传递 ajax url。
  • 然后,您可以尝试使用所有其他停用的插件进行测试,看看它是否开始工作。
  • 也许您应该尝试使用简单的 PHP 文件发送虚拟邮件来检查您是否收到。如果您收到该邮件,则表示您的邮件功能有效,如果发生任何错误,则表示有一些 PHP mail();功能错误。那么你应该尝试使用 SMTP 插件来发送和接收电子邮件。

标签: wordpress email


【解决方案1】:

你能修改这部分吗:

来自

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

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

【讨论】:

  • 我尝试使用 $admin_email 并硬编码电子邮件 ID。但没有用。
【解决方案2】:

发生这种情况是因为您的网站尚未配置电子邮件服务提供商,例如 Google/SendGrid 或 SMTP,或者它只是被删除了。示例配置如下:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-19
    • 1970-01-01
    • 2017-09-07
    • 2019-07-24
    • 2011-08-08
    • 2017-10-04
    • 2015-01-17
    • 1970-01-01
    相关资源
    最近更新 更多