I am writing a plugin for WordPress, one of the tasks I need to-do is send the HTML content of a new post to an e-mail address at the time of posting, I know I can use certain hooks to fire after the post has been saved, but is there a way to get the full HTML content of the new page/post with the theme applied?

I have custom fields within the post so ideally I would like to get the whole post from the URL rather then reconstructing all the fields to then send in HTML.

Any help would be appreciated!


  • Is this the kind of thing your looking for: javascriptkit.com/script/cut156.shtml
    – RyanS
    Apr 23 '12 at 12:16
  • Are you trying to send an HTML e-mail or are you trying to send the post as an attachment (in the HTML format) because if its the former; you might have a hard time getting your HTML to display correctly in the destination e-mail client because HTML in e-mails is not as robust as that of todays browser clients obviously. Depending on what you are tyring to do exactly I'd probably look at writing a function to hook into the save_posts that uses PHPs DOM Parser HERE
    – Adam
    Apr 23 '12 at 12:31
  • I am basically wanting to send the captured HTML post to a mailing list which is handled by MailChimp, I have managed to get all the API working, I literally just need a copy of the blog post in HTML so I can send an HTML email to the list each time a new post is made. Apr 23 '12 at 12:46
add_action('publish_post', 'email_post');
function email_post($postID)
    $post = get_post($postID);
    $content = $post->post_content;
    $content = apply_filters('the_content', $content);
    $content = str_replace(']]>', ']]>', $content);
    $mailto = 'youremail';
    $subject = 'New Post';
    if(mail($mailto, $subject, $content))
        return true;
        return false;

Be sure to set your headers or anything else you might need to populate your email correctly. This is untested, but it should help get you started. Also consider using the 'save_post' hook.

I've posted this same reply on StackOverflow, so if this helps, please mark both of these as answered.


See the save_post action hook - http://codex.wordpress.org/Plugin_API/Action_Reference/save_post

  • Yes, I am using that hook, but how can I get access to the HTML of the article posted? Apr 23 '12 at 15:23

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.