1

For each post, I want to send an email once it has been published. The email address is stored in a custom field.

I have added a custom field . I have got your_email for the 'name'and the value is my email address (as a test).

I have got the following code in functions.php :

function ik_send_email($post_id){

    $email_address = get_post_meta($post_id, 'your_email', true);

    $subject = "Your Subject Here!";
    $body = "Thank you for your submission!  Your story has been approved!";

    $headers = 'From: From Address <[email protected]>' . "\r\n";

    if(wp_mail($email_address, $subject, $body, $headers)){
        //mail sent!
    } else {
        //failure!
    }
}



add_action('publish_post','ik_send_email');

So once I click 'published' it should send an email to the address entered in the custom field, but I'm not getting an email? Any tips someone can give please?

1
  • if you put messages in the if statement, what do they say? Right now the return value of wp_mail is being swallowed and you're not surfacing any errors that might have occured. Also can you verify your install is capable of sending emails via wp_mail? There's a mail test plugin on the .org repo. Does it send if you remove the header?
    – Tom J Nowell
    Commented Aug 11, 2015 at 19:55

2 Answers 2

1

Try logging out of the site and going to /wp-admin (to get to the login screen). Click on the Forgot Password link and enter your username. Did you get an email with password reset instructions? If not, that means that the email configuration (as Emetrop has suggested) may not be correct for the server.

4
  • Hi, I did that and I got the forgot your password email
    – user28566
    Commented Aug 11, 2015 at 19:35
  • 1
    The rest of your code looks good, so it might be the get_post_meta function call that's failing. Ideally, if you are using Xdebug, you could put in a breakpoint at the $subject line and figure out what value $email_address has. If you aren't set up with Xdebug, you could try doing a print_r($email_address); exit; and seeing what WordPress is returning for the to address. Commented Aug 11, 2015 at 19:40
  • Thanks it has worked now. The server took 10 minutes to send the email!
    – user28566
    Commented Aug 11, 2015 at 19:41
  • If you are on shared hosting, that's not uncommon. If you have shell access (SSH), you might be able to monitor the status of the email by running mailq. Commented Aug 11, 2015 at 19:44
0

Your code looks good. I'd bet with you that it's a problem with configuration of email sending server. Maybe you haven't configured sendmail on your hosting. You can make it works easily if you send emails via a SMTP server (ie. gmail). You can do it easily with WP Mail SMTP plugin. Or just write to your hosting provider to make it working.

1
  • Thanks it is working now. Don't have a clue what I have done differently. Thanks for the help!
    – user28566
    Commented Aug 11, 2015 at 19:42

Your Answer

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