Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 = "no-reply@website.com";
        $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');
                }
            }
        });
    });

share|improve this question
    
Current I dont see a problem in this part of code; but how is your javascript part? See this answer for the same topic. –  bueltge Oct 18 '12 at 13:20
    
Added an edit with jquery part. –  Graeme Oct 18 '12 at 13:23

2 Answers 2

up vote 0 down vote accepted

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.

share|improve this answer
    
Good catch on that note! But will that cause an email to be dropped? –  userabuser Oct 18 '12 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. –  s_ha_dum Oct 18 '12 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? –  Graeme Oct 18 '12 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. –  s_ha_dum Oct 18 '12 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; –  bueltge Oct 18 '12 at 20:33

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 = "no-reply@website.com";
    $headers = "From:" . $from;
    if($to && $subject && $message && $headers) {
        wp_mail($to,$subject,$message,$headers);
    } else {
        return false;   
    }
}
share|improve this answer
    
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. :/ –  Graeme Oct 18 '12 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. –  userabuser Oct 18 '12 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? –  Graeme Oct 18 '12 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? –  userabuser Oct 18 '12 at 14:13
    
I'm going to look into this. Probably not. Sorry to waste your time. facepalm –  Graeme Oct 18 '12 at 14:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.