Why wp_mail() function isn't sending any emails and displaying '0' in Chrome 'Network' response

I'm trying to send emails through WordPress using the wp_mail() function and it doesn't seem to work.

It's displaying 0 in Chrome 'Network' response with

Status Code:200 OK

This is my code part:

    // Contact form  Ajax 

    add_action('wp_ajax_nopriv_submit_contact_form', 'submit_contact_form'); 

    function submit_contact_form(){

        if(isset($_POST['email'])) {

            $email = $_POST['email'];       
            $email_to = "[email protected]";

            $host = "ssl://smtp.gmail.com:465";
            $username = '[email protected]';
            $password = 'passpass';

            $email_subject = "You have a new email from $email via company.com website";
            $message = $_POST['text']; 

            $headers = array ('From' = $email, 'To' = $email_to,'Subject' = $email_subject);
            /*$smtp = Mail::factory('smtp',
              array ('host' = $host,
                'auth' = true,
                'username' = $username,
                'password' = $password));*/

            //$mail = $smtp-send($email_to, $headers, $message);

            wp_mail( $email_to, $email_subject, $message );

            /*if (PEAR::isError($mail)) {
              echo($mail-getMessage());
            } else {
              echo("Message successfully sent!\n");
            }*/
        }
    }





    error_reporting(E_ALL);
    ini_set("display_errors", 1);

What part might be wrong?

Edit:

This is the ajax part:

// Send button for the "contact form".
$('#sendBtn').click(function(){
    //get info 
    var fullname = $("#fullname").val();
    var email = $("#email").val();
    var text = $("#text").val();

    //send info to php 
    $.ajax({
        beforeSend: function() {
            if ( IsEmail(email) == false) {
                $('#aboutUnsuccess').show("slow");
                $('.form_content').hide("slow");
            }
        },
        url: document.location.protocol+'//'+document.location.host+'/wp-admin/admin-ajax.php', 
        type: "POST", 
        /*action: 'submit_contact_form',*/
        data: ({ "action": "submit_contact_form", "fullname": fullname, "email": email, "text": text }), 
        success: function (results){
            if ( IsEmail(email) == true) {
                //hide table
                $('.form_content').hide('slow', function() {
                    $('.form_content').hide( "slow" );
                  });
                //show textboxes
                $('#aboutSuccess').show("slow");
                $( "#aboutSuccess" ).append( "iframe id=\"pixel-thing\" src=\"http://54.xx.xx.xx/wp-content/themes/twentyfifteen-child/thePixel.html\" width=\"1\" height=\"1\"  border=\"0\"/iframe" );

            }
        }
    }); 


});

Topic phpmailer wp-mail php plugins Wordpress

Category Web


As this is an AJAX function your function must exit; or die(); after the final line of executed code, in this case outside of the if statement before the final }

However I don't think this is the true issue, if you are getting a 0 returned in my experience it means the function is not running because admin-ajax.php returns 0 if it hits the end of the file and does not pick up your function.

Can you post the AJAX request?

Also for debugging (because your function doesn't actually return anything at the moment), before the exit; or die(); you may want to add a response to check that your function is actually being executed, I usually use something like:

    header("Content-Type: application/json", true);
    echo json_encode( array("AJAX" => "Success") );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.