2016-10-04 57 views
0

我在woocommerce中有一個簡單的註冊表單,它應該向註冊用戶發送電子郵件,但這不會發生。這裏是發送代碼的形式:Die()阻止wp_mail發送郵件

$(".submitbtn").click(function() { 
    $('#result').html('<img src="'+ajax_script.loading_img+'" class="loader" />').fadeIn(); 
    var input_data = $('#wp_signup_form').serialize(); 
    input_data += '&action=et_register_action'; 
    $.ajax({ 
     type: "GET", 
     dataType: "JSON", 
     url: ajax_script.ajax_url, 
     data: input_data, 
     success: function(response){ 
      response = JSON.parse(response); 
      console.log(response); 
      $('.loader').remove(); 
      if(response.status == 'error') { 
       var msgHtml = '<span class="error">' + response.msg + '</span>'; 
       $('<div>').html(msgHtml).appendTo('div#result').hide().fadeIn('slow'); 

      } else { 
       var msgHtml = '<span class="success">' + response.msg + '</span>'; 
       $('<div>').html(msgHtml).appendTo('div#result').hide().fadeIn('slow'); 
       $('#wp_signup_form').find("input[type=text], input[type=password], textarea").val(""); 
      } 
     } 
    }); 
    return false; 
}); 

這是和登記處理的提取物:

if (is_wp_error($status)) { 
    $return['status'] = 'error'; 
    $return['msg'] = __("Username already exists. Please try another one.", ETHEME_DOMAIN); 
    echo json_encode($return); 
} 
else { 
    $from = get_bloginfo('name'); 
    $from_email = get_bloginfo('admin_email'); 
    $headers = 'From: ' . $from . " <" . $from_email . ">\r\n"; 
    $headers .= "MIME-Version: 1.0\r\n"; 
    $headers .= "Content-Type: text/html; charset=UTF-8\r\n"; 
    $subject = __("Registration successful", ETHEME_DOMAIN); 
    $message = et_registration_email($username); 
    wp_mail($email, $subject, $message, $headers); 
    $return['status'] = 'success'; 
    $return['msg'] = __("Please check your email for login details.", ETHEME_DOMAIN); 
    echo json_encode($return); 
} 
die(); 

模具()是推動我瘋了!

  • 如果我保留它,頁面加載完美,用戶被添加到列表中,但沒有郵件到達。
  • 如果我刪除它,添加用戶,郵件到達正確,但'j'字符串附加'0',所以我不能讀取它。

我發現wp_ajax.php以die(0);結尾,因此在任何ajax輸出結尾附加'0'。一個人應該把die()放在自己的代碼中,但是怎麼做呢?我也嘗試使用usleep(800000),但仍然無法正常工作。

郵件正常工作,所以我不想安裝任何第三方插件。 任何幫助是高度讚賞。

編輯: 幾個小時後,一個消息在發送郵件地址到達:

host outbound.mailspamprotection.com [96.127.176.250] 
SMTP error from remote mail server after end of data: 
550 A URL in this email (8theme . com) is listed on https://spamrl.com/. Please resolve and retry 

這怎麼可能,如果我刪除die()此郵件不被識別爲垃圾郵件?

回答

0

嘗試用wp_die()代替。這可能是其他一些鉤子或處理程序需要觸發,並使用wp的die包裝允許他們這樣做。

WP食品入口:https://codex.wordpress.org/Function_Reference/wp_die

摘自wp-includes/functions.php.

/** 
* Kill WordPress execution and display HTML message with error message. 
* 
* This function complements the `die()` PHP function. The difference is that 
* HTML will be displayed to the user. It is recommended to use this function 
* only when the execution should not continue any further. It is not recommended 
* to call this function very often, and try to handle as many errors as possible 
* silently or more gracefully. 
* 
* As a shorthand, the desired HTTP response code may be passed as an integer to 
* the `$title` parameter (the default title would apply) or the `$args` parameter. 
* 
* @since 2.0.4 
* @since 4.1.0 The `$title` and `$args` parameters were changed to optionally accept 
*    an integer to be used as the response code. 
* 
* @param string|WP_Error $message Optional. Error message. If this is a {@see WP_Error} object, 
*         the error's messages are used. Default empty. 
* @param string|int  $title Optional. Error title. If `$message` is a `WP_Error` object, 
*         error data with the key 'title' may be used to specify the title. 
*         If `$title` is an integer, then it is treated as the response 
*         code. Default empty. 
* @param string|array|int $args { 
*  Optional. Arguments to control behavior. If `$args` is an integer, then it is treated 
*  as the response code. Default empty array. 
* 
*  @type int $response  The HTTP response code. Default 500. 
*  @type bool $back_link  Whether to include a link to go back. Default false. 
*  @type string $text_direction The text direction. This is only useful internally, when WordPress 
*         is still loading and the site's locale is not set up yet. Accepts 'rtl'. 
*         Default is the value of {@see is_rtl()}. 
* } 
*/ 
function wp_die($message = '', $title = '', $args = array()) { 

     if (is_int($args)) { 
       $args = array('response' => $args); 
     } elseif (is_int($title)) { 
       $args = array('response' => $title); 
       $title = ''; 
     } 

     if (defined('DOING_AJAX') && DOING_AJAX) { 
       /** 
       * Filter callback for killing WordPress execution for AJAX requests. 
       * 
       * @since 3.4.0 
       * 
       * @param callable $function Callback function name. 
       */ 
       $function = apply_filters('wp_die_ajax_handler', '_ajax_wp_die_handler'); 
     } elseif (defined('XMLRPC_REQUEST') && XMLRPC_REQUEST) { 
       /** 
       * Filter callback for killing WordPress execution for XML-RPC requests. 
       * 
       * @since 3.4.0 
       * 
       * @param callable $function Callback function name. 
       */ 
       $function = apply_filters('wp_die_xmlrpc_handler', '_xmlrpc_wp_die_handler'); 
     } else { 
       /** 
       * Filter callback for killing WordPress execution for all non-AJAX, non-XML-RPC requests. 
       * 
       * @since 3.0.0 
       * 
       * @param callable $function Callback function name. 
       */ 
       $function = apply_filters('wp_die_handler', '_default_wp_die_handler'); 
     } 

     call_user_func($function, $message, $title, $args); 
} 
0

嘗試exit;代替。或wp_die();

相關問題