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 <from@address.com>' . "\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?

  • 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 Aug 11 '15 at 19:55
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.

  • Hi, I did that and I got the forgot your password email – mattnewbie Aug 11 '15 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. – Kevin Fodness Aug 11 '15 at 19:40
  • Thanks it has worked now. The server took 10 minutes to send the email! – mattnewbie Aug 11 '15 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. – Kevin Fodness Aug 11 '15 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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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