How to Send Email to All Registered Users in WordPress without using plugin?
Getting audience as much as possible is the primary goal of any website owner or any blogger. For this email marketing is also a good option. if you want to increase customer engagement on your website blog post, you can use this technique. you can auto mail all the registered user when any new article or post created on your site. It is beneficial for both user can find their relative post directly on mail and website can get more user. Well this is basically very useful if you have a big list of user registered on your WordPress blog.
As i always told, do things without using plugin as much possible. You don’t need to install any heavy plugin for small task. you can do many thing by adding some extra hook and code snippets in your website theme function.php file.+
Solution 1: Send mail without HTML Tags –
This below code snippets only allow simple string message. you can’t pass any HTML tag it to message body. if you want to send HTML Template in mail please user second solution listed below.
So now lets implement this hook in functions.php
1 2 3 4 5 6 7 8 |
function email_members($post_ID) { global $wpdb; $usersarray = $wpdb->get_results("SELECT user_email FROM $wpdb->wp_users;"); $users = implode(",", $usersarray); mail($users, "A New Post ", 'A new post have been published on http://www.w3reign.com'); } add_action('publish_post', 'email_members'); |
Here $wpdb->wp_users is my table name in database, so is you are using any prefix for table name or your table name is different than change it accordingly to your database table name, otherwise it will give error message.
Solution 2: Send mail with HTML Tags –
If you want to send a complete HTML template in to mail you can use below code snippets.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
function email_members($post_ID) { // To send HTML mail, the Content-type header must be set $from = 'support@w3reign.com'; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; // Create email headers $headers .= 'From: '.$from."\r\n". 'Reply-To: '.$from."\r\n" . 'X-Mailer: PHP/' . phpversion(); $message = '<html><body>'; $message .= '<h1> This mail content html Tag </h1>'; $message .= '<p> A new post is published on w3reign.com, Hope you may like and enjoy it.</p>'; $message .= '</body></html>'; $subject = 'A new post is published on w3reign.com'; global $wpdb; $usersarray = $wpdb->get_results("SELECT user_email FROM $wpdb->wp_users;"); $users = implode(",", $usersarray); wp_mail( $users, $subject, $message, $headers ); } add_action('publish_post', 'email_members'); |
This Second email solution is more beneficial, you can modify the HTML section according to your need and can make a nice email template.
[paypal-donation]