Wordpress使用Jquery发送带回调和响应的电子邮件

时间:2016-07-15 07:52:43

标签: javascript php jquery wordpress email

不幸的是我是WP代码编写的初学者,我正在尝试在WP中实现自定义邮件发送表单,但是我不知道如何做到这一点。

我在女巫中有以下代码" send_carrier_email_callback"应该是电子邮件发送功能:

add_action('wp_ajax_send_carrier_email', 'send_carrier_email_callback');
add_action('wp_ajax_nopriv_send_carrier_email', 'send_carrier_email_callback');

function send_carrier_email_callback(){
    global $wpdb; 
    var_dump($_POST);
        add_filter( 'wp_mail_content_type', 'wpdocs_set_html_mail_content_type' );

$to = $_POST['email'];
$subject = $_POST['carriertittle']." állásjelentkezés";
$body = $_POST['massage'];

wp_mail( $to, $subject, $body );

// Reset content-type to avoid conflicts -- https://core.trac.wordpress.org/ticket/23578
remove_filter( 'wp_mail_content_type', 'wpdocs_set_html_mail_content_type' );

function wpdocs_set_html_mail_content_type() {
    return 'text/html';
}
}




function get_carier_ation_callback() {
    global $wpdb; 
    $post_id = $_POST['career'];
    $meta = get_post_meta($post_id, "slide_options");
    $meta_values =$meta[0];
    $args = array (
        'p'                      => $post_id,
        'post_type'              => array( 'enetix_career' ),
    );
    $query = new WP_Query( $args );

    // The Loop
    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
            $query->the_post(); ?>

            <script>
                jQuery(document).ready(function(){
                    jQuery('#applybutton').click(function(){
                        jQuery('#applyform').toggle("slow");
                    });

                    jQuery('#carrieremailsend').click(function(){
                        var career_id = jQuery('#carrierid').val();
                        var name = jQuery('#name').val();
                        var email = jQuery('#email').val();
                        var massage = jQuery('#massage').val();

                        var data = {
                        action: 'send_carrier_email',
                        career: career_id,
                        name: name,
                        email: email,
                        massage: massage
                        }

                        jQuery.post(ajaxurl, data, function(response) {



                        });
                    });
                });     

在&#34; jQuery.post(ajaxurl,数据,函数(响应)&#34;如果发送成功与否,我应该得到回应。

有人可以帮助我吗?

谢谢

1 个答案:

答案 0 :(得分:0)

你需要做这样的事情。希望它对你有用:

add_action('wp_ajax_send_carrier_email', 'send_carrier_email_callback');
add_action('wp_ajax_nopriv_send_carrier_email', 'send_carrier_email_callback');

function send_carrier_email_callback(){
    global $wpdb;
    $send_email = false;
    $to = $_POST['email'];
    $subject = 'Your subject goes here';
    $message = $_POST['message'];
    $send_mail = wp_mail( $to, $subject, $message );
    echo $send_mail; //returns true or false
}

function get_carier_ation_callback() {
    global $wpdb; 
    $post_id = $_POST['career'];
    $meta = get_post_meta($post_id, "slide_options");
    $meta_values =$meta[0];
    $args = array (
        'p'                      => $post_id,
        'post_type'              => array( 'enetix_career' ),
    );
    $query = new WP_Query( $args );

    // The Loop
    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
            $query->the_post(); ?>

            <script>
                jQuery(document).ready(function(){
                    jQuery('#applybutton').click(function(){
                        jQuery('#applyform').toggle("slow");
                    });

                    jQuery('#carrieremailsend').click(function(){
                        var career_id = jQuery('#carrierid').val();
                        var name = jQuery('#name').val();
                        var email = jQuery('#email').val();
                        var massage = jQuery('#massage').val();
                        $.ajax({
                          type: 'post',
                          url: ajaxurl,
                          data: {action: 'send_carrier_email_callback', name: name, email: email, message: message},
                          success: function(response){
                            console.log(response); //your response goes here
                            if(response === true){
                                  //color your div or whatever element green
                            }else{
                                  //color your div red 
                            }
                          }
                        });

                    });
                });