wp_mail returns true but not receiving mails

I am using wp_mail() to receive mails which is submitted through a contact form in my wordpress blog. wp_mail() returns true but the thing is i am not receiving any mails. I have also tried to change the mail address to hotmail from gmail but no luck.

Ajax code in my contact template

Read More
$('#send').click(function() {
    //For Validation
    function validateText(name) {
        var pattern = /^[a-zA-Z'-.s]+$/;
        if (pattern.test(name)) {
            return true;
        } 
        return false;
    }
    //For Validation
    function validateMail(mail) {
        var pattern = /^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z]{2,3}$/;
        //var pattern = /^w+@[a-zA-Z_]+?.[a-zA-Z]{2,3}$/;
        if (pattern.test(mail)) {
            return true;
        }     
        return false;
    }
    //Getting values from the form
    var name = $('#name').val(), mail = $('#mailid').val(), query = $('#message').val(), error = 1;
    //For Validation
    if(name == "" || !(validateText(name))) {
        $('#name').addClass('error');
        error = 0;
    }
    ////For Validation
    if(mail == "" || !(validateMail(mail))) {
        $('#mailid').addClass('error');
        error = 0;
    }
    //For Validation
    if(query == "") {
        $('#message').addClass('error');
        error = 0; 
    }
    if(!error) { // If validation fails
        return false;
    }

    $('#sendAlert').show();
        $('#send').html('Sending...');
        $.post(ajax_object.ajaxurl, { // Using ajax post method to send data
            action: 'ajax_action',
            sendmail: 'nothing',
            name: name,
            mail: mail,
            query: query
        }, function(data) {
            $('#send').html('Send');
            alert(data); // Alerting response
            return false;
        });

   });

In Functions.php

function ajax_action_stuff() {
if(isset($_POST['sendmail'])) {

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

    if(isset($_POST['name']) && isset($_POST['mail']) && isset($_POST['query'])) {  

    $name = $_POST['name'];
    $email = $_POST['mail'];
    $query = $_POST['query'];
    $to = 'vigneshmoha@gmail.com';

    if($name == "" || $email == "" || $query == "") {
        echo "Fail";
        return false;
    }

    $subject = "Website - Query from ".$name;
    $message = "Hi,
        <p><strong>Name</strong>:".$name."</p>
        <p><strong>Mail</strong>:".$email."</p>
        <h3><strong>Query</h3>
        <p>".$query."</p>";
    $headers[] = 'From: no-reply@gmail.com'."rn";
    $headers[] = '';

    add_filter( 'wp_mail_content_type', 'set_html_content_type' );
    $mailsent = wp_mail( $to, $subject, $message, $headers);
    remove_filter( 'wp_mail_content_type', 'set_html_content_type' ); // reset content-type to to avoid conflicts -- http://core.trac.wordpress.org/ticket/23578


    if($mailsent) {
        echo $to;
    } else {
        echo 'error';
    }
   } else {
    echo 'error';   
   }

 } else {
    echo 'error';
 }
 die();
   }
   add_action( 'wp_ajax_ajax_action', 'ajax_action_stuff' );
   add_action( 'wp_ajax_nopriv_ajax_action', 'ajax_action_stuff' ); 

Related posts

Leave a Reply

1 comment

  1. There is a possibility that your email is being marked as spam, or it’s simply your email provider is not allowing it to reach your inbox, are you sending via SMTP?

    Do you have SPF records setup? If you are sending an email from your website, and have the from header set as @gmail.com or @hotmail.com, this will surely not arrive in your inbox as the email is not originating from the gmail or hotmail servers, it’s coming from yours, so it think’s you are trying some phishing attack.

    Edit:

    No, Its not marked as spam. I have checked the spam too. Mail is not
    receiving at all. wp_mail() should returns true once it has sent the
    mail right? So Should i change the from header to something else?

    -vigneshmoha

    That means the mail has left your server, it doesn’t mean it’ll arrive in your inbox, as there are many other steps between your server and your inbox, and a few different things could of went wrong in this process. Try testing out the From: header, change to example@yourdomainname.com