What's the best way to send a email from Wordpress after a certain action.

I'm working on a site where users can submit ideas and then other users can comment on them.

I would like to send emails to a number of different addresses (moderators) when a post is made and when a comment is made.

I have this code to add the posts with an attempt to send an email that doesn't work.

<section class="submitForm">

    <h1>Submit idea</h1>


    if(isset($_POST['new_post']) == '1'){

        $post_title = $_POST['post_title'];
        $post_content = $_POST['content'];

        $new_post = array(
            'ID' => '',
            'post-author' => $user->ID,
            'post_title' => $post_title,
            'post_content' => $post_content,
            'post_status' => 'publish'

        $post_id = wp_insert_post($new_post);
        $post = get_post($post_id);

        // send email notifications

        wp_mail( '[email protected]', 'New Idea', $post_content);

    echo '<form method="post" action="" >';
    echo '<label for="idea_title">Idea Title</label>';
    echo '<input type="text" name="post_title" class="title">';

    $settings = array(
        'quicktags' => false,
        'textarea_name' => 'content',
        'media_buttons' => true,
        'textarea_rows' => 12,
        'tinymce' => array(
            'theme_advanced_buttons1' => 'formatselect,|,bold,italic,underline,|,' .
            'bullist,blockquote,|,justifyleft,justifycenter' .
            ',justifyright,justifyfull,|,link,unlink,|' .
    wp_editor( '', 'content', $settings );

    echo '<input type="hidden" name="new_post" value="1">';
    echo '<input type="submit" class="submit" value="Submit Idea">';

  • Have you tested if wp_mail works on your site, outside a form?
    – Wyck
    Commented Jun 24, 2014 at 16:05

2 Answers 2


Try to add some code to the functions.php like:


function send_email() { // write some code with wp_mail function. }

I think you can use the $post to get the post info.

For comments I guess you can use:

add_action('comment_post', 'send_email');

Let me know if you need some more help!


Your wp_mail() function has a wp_redirect() and an exit right in front of it. These things stop execution of the current script.

Move your mail function above the redirect.

Your Answer

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

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