0

I have set up a simple mailer before I get the full implementation sorted. I've tried a few email addresses but am not receiving them even though it returns 'true'. I'm using wp_ajax as well if that makes a difference.

functions.php

    //  Check for email return in share folders
    add_action( 'wp_ajax_share_email', 'share_email' );
    add_action( 'wp_ajax_nopriv_share_email', 'share_email' );
    function share_email(){
        $email = $_POST['email'];
        // Return a boolean!
        $to = $email;
        $subject = "Test mail";
        $message = "Hello! This is a simple email message.";
        $from = "[email protected]";
        $headers = "From:" . $from;
        if(wp_mail($to,$subject,$message,$headers)) {
            return true;
        } else {
            return false;   
        }
    }

Can anyone let me know why this isn't sending? Am I not allowed to use wp_mail and wp_ajax together perhaps?

Thanks

EDIT Jquery part

    $('#share-pop .btn').click(function(){
        var email = check.val();
        jQuery.ajax({
            type: 'POST',
            url: ajaxurl,
            data: {"action" : "share_email", 'email': email},
            success: function(data){
                if (data) {
                    alert('sent');
                } else {
                    alert('An error occured');
                }
            }
        });
    });

2
  • Current I dont see a problem in this part of code; but how is your javascript part? See this answer for the same topic. Commented Oct 18, 2012 at 13:20
  • Added an edit with jquery part. Commented Oct 18, 2012 at 13:23

2 Answers 2

0

You need to supply or line breaks for your header fields. For example:

$headers = "From:" . $from . PHP_EOL;

Or put the header fields into an array, in which case you don't need to add the line breaks manually.

$headers[] = "From:" . $from;

Email headers are not 'normal' text. Those line ending are meaningful. They are control characters.

There are examples in the Codex.

6
  • Good catch on that note! But will that cause an email to be dropped? Commented Oct 18, 2012 at 14:04
  • Yes, I believe it does. Without the line ending, you'd be sending malformed headers, essentially. The line endings are specified in the Codex as well, and the conditions in which they are needed. Commented Oct 18, 2012 at 14:07
  • Annoyingly, It looks like the reason it's not sending is because I'm working locally on a virtual host. I'm guessing this is the reason right? Commented Oct 18, 2012 at 14:12
  • I assumed that your server was actually capable of sending email. If it isn't this has been a waste of time. You can send email from a local machine. I do it by configuring sendmail or exim to be a 'send-only' server that pushes email through our host's mail server, but that is another topic altogether. Commented Oct 18, 2012 at 14:18
  • the header works also without as array, I used in tones of plugin like this $headers = 'From: ' . get_the_author_meta( 'display_name', $user->ID ) . ' (' . get_bloginfo( 'name' ) . ')' . ' <' . $user->data->user_email . '>' . PHP_EOL; Commented Oct 18, 2012 at 20:33
1

Correct me if I'm wrong here, but you're not receiving e-mails because you're not invoking the function on TRUE.

//  Check for email return in share folders
add_action( 'wp_ajax_share_email', 'share_email' );
add_action( 'wp_ajax_nopriv_share_email', 'share_email' );
function share_email(){
    $email = $_POST['email'];
    // Return a boolean!
    $to = $email;
    $subject = "Test mail";
    $message = "Hello! This is a simple email message.";
    $from = "[email protected]";
    $headers = "From:" . $from;
    if($to && $subject && $message && $headers) {
        wp_mail($to,$subject,$message,$headers);
    } else {
        return false;   
    }
}
5
  • Hm, that certainly sounded right, but still nothing. It's returning false and invokes the an error occured alert. Which would make sense since it's not being received, but I can't seem to get any sort of error message to help debug. :/ Commented Oct 18, 2012 at 13:45
  • Seems to me that the email $_POST var isn't being set... Test the $to var manually by replacing $email with a test email address of your own. Then when you invoke your function via ajax callback it should fire your mail function and you will receive your email. That then points to your front jQuery not passing back the neccessary $_POST['email'] - process of elimination here. Lets make sure the function is firing. Also look at this: {"action" : "share_email", 'email': email} you have quotes then no quotes. Commented Oct 18, 2012 at 14:00
  • Annoyingly, It looks like the reason it's not sending is because I'm working locally on a virtual host. I'm guessing this is the reason right? Commented Oct 18, 2012 at 14:10
  • Well.. you need your SMTP/port settings configured in your php.ini file in order to receive localhost mail.. Have you done this? Commented Oct 18, 2012 at 14:13
  • I'm going to look into this. Probably not. Sorry to waste your time. facepalm Commented Oct 18, 2012 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.