Codeable info

How To Send Out Notification E-mails To Subscribed Users

Posted on by in WordPress Tutorials

Sometimes spam mail can be a good thing, as long as it is used responsibly. For instance, say you were to implement a batch e-mail feature that notified subscribed readers every time you published a new post.

This would keep your subscribed visitors and readers up to date and encourage them to visit your WordPress blog or site frequently. Worst case scenario, they decide to unsubscribe from the active users list so the e-mails stop coming.

Batch e-mail features are possible through the use of a WordPress plugin or add-on, but who needs plugins. In this tutorial we are going to implement a batch e-mail feature for published posts natively, or without the help of an external plugin.

The code to implement such a feature is simple and quick but does involve (as with most of our tutorials) editing important WordPress source files, so make sure to do a complete database backup, just in case.

The Native Email Code

The following segment of code must be added to the active themes “functions.php” file. It can be added anywhere within the file using a standard text editing program, just make sure not to interrupt existing code tags:

function send_email_users($post_ID)  {
    global $wpdb;
    $usersarray = $wpdb->get_results("SELECT user_email FROM $wpdb->users;");
    $users = implode(",", $usersarray);
    mail($users, "New Article is Published", 'A new article has been published on <a href="" rel="nofollow"></a>. Please pay us a visit and check it out!');
    return $post_ID;
add_action('publish_post', 'send_email_users');

Make sure to replace the “YOURURLHERE” section with the web URL address for your WordPress blog or site. Also, to customize the text sent in the e-mail to your subscribed users edit any of the text between the quotations.

Keep in mind the blanket e-mail will only be sent to registered users on your WordPress blog or website, which means that if you have frequent visitors that haven’t subscribed or registered- you’ll want to encourage them to do so.

In the end if you decide such a feature IS a little too spammy for your tastes, all that needs to be done is to delete the above code and wallah- no more e-mails sent to users. No harm done.

Now get out there and start pumping out those blog posts! You’ll want your readers to receive as many e-mails as possible (that was a joke).

Codeable info

Comments (3)

Comment by Rev. Voodoo says:

Yup, that’s pretty cool!

Comment by Pippin Williamson says:

To see an example of what could be done with this code, you should take a look at Post Notify

Comment by Rev. Voodoo says:

Cool idea. Be fun to set up a custom role/level in, and maybe during registration or something have an opt-in box which assigns them to that role. Then send the emails only to the opt-in people? Or some such mechanism…..

Codeable info