Create Perfect Emails For Your WordPress Website

Advertisement

Whatever type of website you operate, its success will probably hinge on your interaction with your audience. If executed well, one of the most effective tools can be a simple email.

WordPress users are in luck, since WordPress already has easy-to-use and extendable functions to give you a lot of power and flexibility in handling your website’s emails.

In order to create our own system, we will be doing four things. First, we will create a nice email template to use. We will then modify the mailer function so that it uses our new custom template. We will then modify the actual text of some of the built-in emails. Then we will proceed to hook our own emails into different events in order to send some custom emails. Let’s get started!

How WordPress Sends Emails

WordPress has a handy function built in called wp_mail(), which handles the nitty-gritty of email sending. It is able to handle almost anything you throw at it, from custom HTML emails to modifications to the “From” field.

WordPress itself uses this function, and you can, too, by using WordPress hooks. You can read all about how hooks work in WordPress1, but here is the nutshell version, and we will be working with them in this article so much that you’ll learn it by the end.

Hooks enable you to add your own functions to WordPress without modifying core files. Without hooks, if you wanted to send a publication notice to the author of a post, you would have to find the function that published the post and add your own code directly to it. With hooks, you write the function for sending the email, and then hook it into the function that publishes the post. Basically, you are telling WordPress to run your custom function whenever the function for publishing posts runs.

Setting Up Shop

The first thing we’ll have to do is create a plugin. We could get away without it and just use our theme’s functions file, but this would become clunky in the long run. Don’t worry: setting up a plugin is super-easy.

Go to your website’s plugins folder, which can be found under wp-content. Create a new folder named my_awesome_email_plugin. If you want a different name, use something unique, not email or email_plugin; otherwise, conflicts might arise with other plugins.

Create a file named my_awesome_email_plugin.php in the new folder. The name of the file (without the extension) must be the same as the name of the folder.

Edit the contents of my_awesome_email_plugin.php by copying and pasting the code below and modifying it where necessary. This is just some default information that WordPress uses to show the plugin in the plugins menu in the admin area.

<?php
/*
Plugin Name: My Awesome Email Plugin
Plugin URI: http://myawesomewebsite.com
Description: I created this plugin to rule the world via awesome WordPress email goodness
Version: 1.0
Author: Me
Author URI: http://myself.me
*/

?>

Once that’s done, save the file, go to the WordPress admin section, and activate your new plugin. If you’re new to this, then congratulations! You have just created your first working WordPress plugin! It doesn’t really do anything yet, but don’t let that bother you. Just read on, because we’ll be adding some functionality after the next section.

Creating An Email Template

Creating good email templates is worth an article on its own. I will just share the method that I use, which does not mean that doing it differently is not allowed. Feel free to experiment!

I am not a big fan of using images in emails, so we will be building an HTML template using only CSS. Our goal is to come up with a template to which we can add a header and footer section. We will send our emails in WordPress by pulling in the header, putting the email text under that and then pulling in the footer. This way, you can change the design of your emails very easily just by modifying the templates.

Without further ado, here’s the code for the email template that I made. Or you can download it as an HTML file2 (right-click, and then select “Save as”). If you want a quick preview of what it looks like, just click the link.

<html>
	<head>
		
		<title>The Subject of My Email</title>
	
	</head>
	<body>
		<div id="email_container" style="background:#444">
			<div style="width:570px; padding:0 0 0 20px; margin:50px auto 12px auto" id="email_header">
				<span style="background:#585858; color:#fff; padding:12px;font-family:trebuchet ms; letter-spacing:1px; 
					-moz-border-radius-topleft:5px; -webkit-border-top-left-radius:5px; 
					border-top-left-radius:5px;moz-border-radius-topright:5px; -webkit-border-top-right-radius:5px; 
					border-top-right-radius:5px;">
					MyAwesomeWebsite.com
				</div>
			</div>
		
		
			<div style="width:550px; padding:0 20px 20px 20px; background:#fff; margin:0 auto; border:3px #000 solid;
				moz-border-radius:5px; -webkit-border-radius:5px; border-radius:5px; color:#454545;line-height:1.5em; " id="email_content">
				
				<h1 style="padding:5px 0 0 0; font-family:georgia;font-weight:500;font-size:24px;color:#000;border-bottom:1px solid #bbb">
					The subject of this email
				</h1>
				
				<p>
					Lorem ipsum dolor sit amet, consectetuer adipiscing 
					elit. Aenean commodo ligula eget dolor. Aenean massa 
					<strong>strong</strong>. Cum sociis natoque penatibus 
					et magnis dis parturient montes, nascetur ridiculus 
					mus. Donec quam felis, ultricies nec, pellentesque 
					eu, pretium quis, sem. Nulla consequat massa quis 
					enim. Donec pede justo, fringilla vel, aliquet nec, 
					vulputate eget, arcu. In enim justo, rhoncus ut.
				</p>
				<p>
					Imperdiet a, venenatis vitae, justo. Nullam dictum 
					felis eu pede <a style="color:#bd5426" href="#">link</a> 
					mollis pretium. Integer tincidunt. Cras dapibus. 
					Vivamus elementum semper nisi. Aenean vulputate 
					eleifend tellus. Aenean leo ligula, porttitor eu, 
					consequat vitae, eleifend ac, enim. Aliquam lorem ante, 
					dapibus in, viverra quis, feugiat a, tellus. Phasellus 
					viverra nulla ut metus varius laoreet. Quisque rutrum. 
					Aenean imperdiet. Etiam ultricies nisi vel augue. 
					Curabitur ullamcorper ultricies nisi.
				</p> 
				
				<p style="">
					Warm regards,<br>
					The MyAwesomeWebsite Editor
				</p>
				
				<div style="text-align:center; border-top:1px solid #eee;padding:5px 0 0 0;" id="email_footer"> 
					<small style="font-size:11px; color:#999; line-height:14px;">
						You have received this email because you are a member of MyAwesomeSite.com.
						If you would like to stop receiving emails from us, feel free to 
						<a href="" style="color:#666">unregister</a> from our mailing list
					</small>
				</div>
				
			</div>
		</div>
	</body>
</html>

Remember that this is an email, so the HTML won’t be beautiful. The safest styling method is inline, so the fewer frills you can get away with, the better.

Let’s split this into two parts. The header part of the email is everything from the top right up to and including the h1 heading on row 23 (i.e. lines 01 to 23). Copy that bit and paste it into a new file in your my_email_plugin folder, and name it email_header.php. The footer part of the email is everything from the paragraph tag before “Warm regards” right until the end (i.e. lines 48 to 64). The text between the header and footer is just a placeholder so that you can see what the finished product will look like. We will fill it with whatever content we need to send at the time.

Preparing The WordPress System For Our Emails

By default, WordPress sends plain-text emails. In order to accommodate our fancy HTML email, we need to tell the wp_mail() function to use the HTML format. We will also set up a custom “From” name and “From” address in the process, so that the email looks good in everyone’s inbox. To accomplish this, we’ll be using the previously mentioned hooks. Let’s look at the code; explanation follows.

add_filter ("wp_mail_content_type", "my_awesome_mail_content_type");
function my_awesome_mail_content_type() {
	return "text/html";
}
	
add_filter ("wp_mail_from", "my_awesome_mail_from");
function my_awesome_mail_from() {
	return "hithere@myawesomesite.com";
}
	
add_filter ("wp_mail_from_name", "my_awesome_mail_from_name");
function my_awesome_email_from_name() {
	return "MyAwesomeSite";
}

On line 01, we have defined that we are adding a filter to the WordPress function wp_mail_content_type(). Our filter will be called my_awesome_mail_content_type. A filter is nothing more than a function, so we need to create the function my_awesome_mail_content_type().

Remember that actions are functions called from within other functions? We add an action to the wp_insert_user() function, and the action is performed whenever wp_insert_user() runs. Filters are specified in much the same way; but, instead of running alongside the function that it is called from, it modifies the value of the entity that it is called on.

In our case, this means that somewhere inside the wp_mail() function is a variable that holds the email type, which is by default text/plain. The filter hook wp_mail_content_type is called on this variable, which means that all attached filters will be run. We happen to have attached a filter to it on line 01, so our function will perform its task. All we need to do is return the value text/html, which will modify the value of the variable in the wp_mail function to text/html.

The logic behind the rest of the code is exactly the same. Adding a filter to wp_mail_from enables us to change the sender’s address to hithere@myawesomewebsite.com, and adding a filter to wp_mail_from_name enables us to change the sender’s name.

Modifying Existing WordPress System Emails

Welcoming New Users

3

This is the content of the default WordPress email.

As mentioned, WordPress has a bunch of built-in emails that can be easily controlled (using hooks, of course). Let’s modify the default greeting email that WordPress sends out to new users. This email is sent out using a so-called “pluggable function.” This function is supplied by WordPress, but, contrary to the usual core functions, you are allowed to overwrite it with your own code.

The function in question is called wp_new_user_notification(). To modify it, all we need to do is create a function with the same name. Due to the method by which WordPress calls pluggable functions, there will not be any conflict, even though you are creating a function with the same name. Below is the function that I wrote. See the explanation and preview of it further below.

function wp_new_user_notification($user_id, $plaintext_pass) {
	$user = new WP_User($user_id);

	$user_login = stripslashes($user->user_login);
	$user_email = stripslashes($user->user_email);
	
	$email_subject = "Welcome to MyAwesomeSite ".$user_login."!";
	
	ob_start();

	include("email_header.php");
	
	?>
	
	<p>A very special welcome to you, <?php echo $user_login ?>. Thank you for joining MyAwesomeSite.com!</p>
	
	<p>
		Your password is <strong style="color:orange"><?php echo $plaintext_pass ?></strong> <br>
		Please keep it secret and keep it safe!
	</p>
	
	<p>
		We hope you enjoy your stay at MyAwesomeSite.com. If you have any problems, questions, opinions, praise, 
		comments, suggestions, please feel free to contact us at any time
	</p>
	
	
	<?php
	include("email_footer.php");
	
	$message = ob_get_contents();
	ob_end_clean();

	wp_mail($user_email, $email_subject, $message);

As you can see, the function is passed two arguments: the ID of the new user and the generated password. We will be using these to generate the variable parts of our message. On line 2, we’ve built a user object that will contain the data of the user in question. On line 7, we’ve created an email subject using the variable $email_subject.

Before we move on, let’s go back to our email_header.php file. Replace “The Subject of My Email” and “The subject of this email” (lines 04 and 22 if you’re looking at the code here) with <?php echo $email_subject ?>. We don’t want all of our subjects to be “The Subject of My Email,” so we need to pull that data from the email that we are building.

From lines 09 to 31, we are using a handy technique called “output buffering.” Because the content email_header.php is not stored inside a variable, it is included directly; this would result in it being printed right away, and we would not be able to use it in our function. To get around this problem, we output buffering. When it is turned on (using ob_start()), no output is sent from the script; instead, it is stored in an internal buffer.

So, first, we include the header, then we write our our message content, then include the footer. Because we are buffering the content, we can simply close our PHP tags and use regular HTML for our message, which I find much cleaner than storing all of it in a variable. On line 30, we pull the contents of the buffer into a variable; and on line 31, we discard the buffer’s content, since we don’t need it anymore.

With that done, we have all of the information needed to use wp_mail(). So, on line 33, we send our email to the user, which should look something like this:

Password Retrieval Emails

For some reason, WordPress doesn’t use the same pluggable functions to handle all emails. For example, to modify the look and feel of the password retrieval emails, we have to resort to hooks. Let’s take a look.

add_filter ("retrieve_password_title", "my_awesome_retrieve_password_title");

function my_awesome_retrieve_password_title() {
	return "Password Reset for MyAwesomeWebsite";
}

	
add_filter ("retrieve_password_message", "my_awesome_retrieve_password_message");
function my_awesome_retrieve_password_message($content, $key) {
	global $wpdb;
	$user_login = $wpdb->get_var("SELECT user_login FROM $wpdb-<users WHERE user_activation_key = '$key'");
	
	ob_start();
	
	$email_subject = imp_retrieve_password_title();
	
	include("email_header.php");
	?>
	
	<p>
		It likes like you (hopefully) want to reset your password for your MyAwesomeWebsite.com account.
	</p>

	<p>
		To reset your password, visit the following address, otherwise just ignore this email and nothing will happen.
		<br>
		<?php echo wp_login_url("url") ?>?action=rp&key=<?php echo $key ?>&login=<?php echo $user_login ?>			
	<p>
	
	?>
	
	include("email_footer.php");
	
	$message = ob_get_contents();

	ob_end_clean();
  
	return $message;
}

First, we’ve added a filter to retrieve_password_title, which will modify the default value of the email’s title to our own. Then, we’ve added a filter to retrieve_password_message, which will modify the contents of the message.

On line 10, we’ve used the $wpdb object to query the database for the user’s name based on the key that was generated when the retrieval was initiated. We then do the same thing as before: we start the content buffering, pull our email header, add our message content, and pull our email footer.

One fantastic part about using hooks can be seen on line 14. Our password title needs to be “Password Reset for MyAwesomeWebsite.” We could well have typed that in, but instead we created a function (imp_retrieve_password_title()) that outputs exactly the same thing. It should be clear by now that all we are doing with these hooks is creating regular ol’ functions that can just be plugged into WordPress as actions (which run when initiated by something) or filters (which run and modify data when they are initiated).

This time, instead of using wp_mail(), all we need to do is return the message’s content. This is because we are creating a filter that modifies the contents of the password-retrieval email, nothing else. WordPress will do whatever it usually does to send that email, but now it will use our content.

Pluggable Function and Hooks

This question is not easily answered, because this is not too well documented yet. Your best bet is looking in the file pluggable.php (in your wp-includes folder) to see which emails are controlled from there. Remember not to edit this file; use the plugin we are creating here. You can scan the list of WordPress filters4 to find filters that control email content.

Right now, most emails are handled through pluggable functions; only the password-retrieval email and some WordPress MU emails are handled using hooks. This might change, as development is quite active, but I would guess that if any new emails are added, you will be able to use pluggable functions.

Here is a list of emails that you can modify using pluggable functions:

  • Notify authors of comments: wp_notify_postauthor()
  • Notify moderator of comments waiting for approval: wp_notify_moderator()
  • Notify administrator of password changes on the website: wp_password_change_notification()
  • Notify administrator of new registrations: wp_new_user_notification()

Adding New Emails To The System

So far, we’ve just been modifying what WordPress has to offer. Now it’s time to add some emails of our own! Let’s implement an email that will notify an author when you have published their post.

To accomplish this, we need to find the WordPress action hook that publishes a post when we press the “Publish” button. We then have to hook our own function into that, which will perform the task of sending the email.

Looking at the list of action hooks5, we can see that the hook we are looking for is called {$new_status}_{$post->post_type}. This looks a bit different than what we’re used to, but it’s really very simple. A post can go through numerous statuses. It can be a draft, it can be private, published and so on. There are also a lot of potential post types, such as “Post” and “Page,” not to mention that you can create custom post types. This hook simply enables us to put a status and a post type together and then get the hook that runs when that post’s type changes to the indicated status. So, our hook will be called publish_post.

add_action("publish_post", "my_awesome_publication_notification");

function my_awesome_publication_notification($post_id) {
	$post = get_post($post_id);
	$author = get_userdata($post->post_author);
	
	$author_email = $author->user_email;
    $email_subject = "Your article has been published!";
	
	ob_start();
	
	include("email_header.php");
	
	?>
	
	<p>
		Hi, <?php echo $author->display_name ?>. I've just published one of your articles 
		(<?php echo $post->post_title ?>) on MyAwesomeWebsite!
	</p>
	
	<p>
		If you'd like to take a look, <a href="<?php echo get_permalink($post->ID) ?>">click here</a>. 
		I would appreciate it if you could come back now and again to respond to some comments.
	</p>
	
	
	<?php
	
	include("email_footer.php");
	
	
	$message = ob_get_contents();
	
	ob_end_clean();
	
	
	wp_mail($author_email, $email_subject, $message);
	
}

By now, this should be second nature to you. The only real difference here is that we have to retrieve the data of the post, and the author’s data on lines 4 and 5, so that we have the necessary data for the email.

One thing you might be wondering is how I know that my function takes the ID of this post as its argument. I cannot freely choose this value, of course; it is dictated by how WordPress is built. Every hook supplies different arguments; some even supply more than one. To find out what arguments are at your disposal, you will have to go into some core files.

I suggest browsing the hooks database6, clicking on the hook that you need, and then clicking on “View hook in source” next to your version of WordPress (preferably the latest one). Once there, scroll down, and find the highlighted line, which is where the hook is called. It will be in the form of do_action( $tag, $arg_a, $arg_b, $etc ) or apply_filters( $tag, $arg_a, $arg_b, $etc ).

Extending Our Functions

Interestingly, the wp_mail() function itself is a pluggable function, so you can completely override how it works. This may be going a bit over the top, but if you need some serious email-sending power (for example, you want a system that notifies your 120,000 registered users about new posts), you can completely modify it to use your mass-mailer application.

Because we are using a template for email headers and footers, a lot can be done to extend our emails. We can distinguish between emails to staff and emails to users by using different header files; we can add the latest three posts to the bottom of each email by using footer templates; and so on.

We can add a table to our database that holds information about which users are emailed the most, who responds to emails, and so on. Whenever you plug a function into something, it can contain any sort of code you’d like. You could include code for increasing the email count for user #112 inside the function that sends them the email, for example. This is not a good practice (you should create separate functions and plug them both in), but getting to grips with the vast power that this methodology offers is important.

A Word Of Warning

While the method described here is great, I am not an expert in creating HTML emails. The code for the HTML email above is tested to work in Gmail and some other applications, but each email application handles email differently. Some strip out all CSS, some strip out just background colors, and so on.

Before using the template, please test it with the most common applications (Gmail, Yahoo Mail, Apple Mail, Outlook, Entourage, Thunderbird, etc.) to make sure it works as expected.

Conclusion

Hopefully by now you have learned how to modify the emails that WordPress sends out, how to create your own emails, and how to plug them into different actions.

I also hope that your knowledge of WordPress hooks has expanded, because they are the tool for creating great plugins and add-ons for WordPress, and the thinking behind them is a glimpse into the world of object-oriented programming.

If you have any questions or comments, let me know: I am at your disposal. Also, if you’ve created a similar system, do share your thoughts, I would be happy to hear how you’ve accomplished the same, or better!

(al)

↑ Back to topShare on Twitter

I am a web developer in love with WordPress. I have a company called Bonsai Shed where we make webapps and premium themes like Musico and Estatement.

  1. 1

    Thanks, Daniel! This article is going into Evernote to do this week. :)
    Fawn

    2
  2. 2

    wondering about the legality of the copyright on your personal site . . .

    wordpress.site5.net/journalcrunch/

    -4
    • 3

      Thank you so much for pointing that out!

      I have just started to revamp the site and I didn’t put as lot of research into the them as it IS free. The theme is a free theme created by site5 so i COULD use it, but your comment pushed me to do some research and these themes are only for their customers (I am not). I have contacted them about this and if they allow me to use it I will continue to do so, otherwise I will take it down ASAP.

      Thanks again!

      0
    • 4

      I had a reply from them and thankfully they say:

      “Anyone is free to use the designs and edit them as you see fit. You can change the copyright’s as well :)”

      Do check these out as they are pretty cool!
      http://www.site5.com/wordpress-themes/

      0
    • 5

      @jennifer Get a life.WP is GPL so putting a price tag on a theme doesn’t make that exclusive.

      3
      • 6

        @emagineit
        Um, she asked a illegitimate question. Perhaps you shouldn’t comment on these things when you don’t know what you’re talking about. Taking a design from someone else and editing out the copywrite isn’t exactly design standards in this industry.

        This isn’t a knock on Daniel, he did his due diligence and contacted them but if I were using this, I would use:
        “(c) 2011 Daniel Pataki” or “(c) Content by Daniel Pataki 2011″

        -2
      • 7

        I completely agree with Graham. I am totally rebuilding the thing so it is just a placeholder/inspiration anyway so I didn’t put too much thought into it although I should have. I didn’t mean the design of the actual site in the footer (since the design will be different soon), I meant the designs for the upcoming projects I am putting on there. Terrible wording and foresight on my part, I will correct it as soon as I get the chance.

        Also, putting a price tag kind of does make it “exclusive”. Not the price tag in itself but the licence of it does. Some people make an honest living out of making themes so just as I don’t go around stealing cars I like, I shouldn’t go about using themes I should pay for but don’t either.

        I think she could have been more tactful in bringing it up but just because you don’t agree with her is no reason to be rude.

        -1
  3. 8

    Great article ! For less techy users, I’m using the wp better emails plugin that does the job pretty well.

    1
  4. 9

    This is awesome. Finally someone did something about the emails. Thanks for the post

    0
  5. 10

    Justin St. Germain

    October 25, 2011 9:53 am

    great stuff Daniel. I’m always making new plugins, but, this is something I hadn’t thought of myself. I will have to go personalize mine later.

    0
  6. 11

    It’s really interesting! Thank You.

    0
  7. 12

    Great tutorial. Very concise.

    Is there a way it can be used to email people who sign up for a newsletter or promotion etc?

    Thanks
    Nathan

    0
    • 13

      Hi Nathan,

      Yes there is, depending on what you need you may need to do some modification. The main thing to take away from it is the wp_mail() function. If you pass this function a subject and email and whatnot you can use it anywhere.

      You can store weather someone is registered for a promotion in a separate table, or if these are all registered users you can use the user_meta table. Say everyone registers on your site and some of them opt in. In this case you could store a single “1″ in the usermeta table for each person who opts in.

      You can query the table for all these emails and then use wp_mail() to send them all a promotion. You can store your email as an HTML file and include it but I prefer to store it in a PHP file or a function so I can use variables to get “Hi Nathan” in the body of the email and not just “Hi”.

      If you have a lot of registrants it is better to use an external service. I found that MadMimi (http://madmimi.com) is excellent for this. It has an API so once you register you basically never have to visit their site. Instead of doing a foreach loop on all the 50,000 emails in the databse to send them emails you just create and send a promotion to the list via MadMimi and they take care of the actual mailing. Some servers have limits so you probably can’t just send 50,000 all at once so you’d need to build in delays and whatnot which is another headache.

      I hope I answered some of your questions, I know this is not a do this, this and that type of answer but this is a complex issue and depends a lot on what you need to do. The bottom line is that it is not difficult to use wp_mail() for this at all. At higher volumes it is better to use an external service though.

      0
  8. 14

    There seems to be a missing end bracket in the wp_new_user_notification function and also I get an error when activating the plugin with that function “Fatal error: Cannot redeclare wp_new_user_notification() (previously declared in…”

    3
    • 15

      Hi Tony,

      Sorry about that, I meant to name that differently. If you name it something like: “my_awesome_new_user_notification()” it should all work. If the function is referenced elsewhere make sure to update those calls as well.

      2
    • 16

      To prevent the fatal error you have to wrap the function:

      if ( !function_exists(‘wp_new_user_notification’) ) {
      function wp_new_user_notification( $user_id, $plaintext_pass = ” ) {

      “If you include this, your function will still get priority over the WP core; it’ll just prevent fatal errors if you activate a plugin that happens to have overridden the same function.”

      Reference: http://sltaylor.co.uk/blog/customizing-new-user-email-pluggable-function/

      1
  9. 17

    Another great article and very timely for me. I was just looking to add email notification hooks to my plugin. Thanks for this!

    0
  10. 18

    Great article, but you should probably mentioned that HTML emails tend to be written in extremely basic HTML, think nested tables here.

    0
  11. 19

    Great post, I’ve been using WordPress for a couple of years now and I always found the email system to be one of the less polished points. Thanks to this no more, thanks!

    0
  12. 20

    I noticed a typo with $wpdb-<users, but I am still getting an warning which will send the email but you can't reset the password. Thoughts on why i am getting this warning in the following code: (Basically I am not getting $key passed in)

    Warning: Missing argument 2 for my_awesome_retrieve_password_message()

    add_filter ("retrieve_password_message", "my_awesome_retrieve_password_message");
    function my_awesome_retrieve_password_message($content, $key) {
    global $wpdb;
    $user_login = $wpdb->get_var("SELECT user_login FROM $wpdb->users WHERE user_activation_key = '$key'");
    ob_start();
    $email_subject = imp_retrieve_password_title();

    0
    • 21

      It’s because when you add a filter you need to point out how many parameters your function accepts. It defaults to 1.
      So you need to change the above code to:

      add_filter (“retrieve_password_message”, “my_awesome_retrieve_password_message”, 10, 2);

      10 is the default priority (you can change it to whatever) and 2 is the number of arguments to pass to your function. Since retrieve_password_message sends only two arguments – $message and $key, you need to have it set to 2.

      Hope it makes sense.

      2
  13. 22

    Realy great article. Its very useful post.Thank you.

    0
  14. 23

    Nice post, even code examples. Well done, well written!!
    Best of all – It’s usable and in demand content.
    Thanks!

    0
  15. 24

    Excellent article! :) Something I would definitely use for my next WordPress projects..

    0
  16. 25

    What about posting via email? Why does that feature have to suck so much?

    0
  17. 26

    Is this a “five-minute install” ?

    0
  18. 27

    I wish I could download the working code. I copied everything as it was in the article – didn’t work, I put here and there closing and opening php tags – it didn’t work. My WordPress admin doesn’t show up, if I keep the code as I have it now. Are there only 3 files – the main one, with all the code and than the header and footer?
    At least I learned something about hooks, and that creating plugins is not as hard as I imagine at first place. Thank you Daniel!

    0
  19. 28

    It would be nice if your Comments had a “wrap code” or some sort of way to show a php query without being “censored”.

    Please, if you would, remove the email address in my comment! Thank you so much.

    0
  20. 29

    Would it be possible to include a finished script? I’m getting errors on the ending:
    unexpected $end

    I also found with this tutorial that it didn’t really explain what to do with all the codes provided, was that supposed to go into the plugin file?

    1
  21. 31

    This is a terrific post. My ‘Smashing WordPress Beyond The Blog’ is one of the most thumbed titles on my bookshelf, and I just clicked through and bought Smashing Book#3 via your link.

    I have got the new e-mail script working, sending notifications on new posts.

    Quick question: I’d like to send out an automated e-mail to certain clients on publication of certain posts. I would presumably be able to control this by setting up a mailing list, and giving the posts a particular category. I would then try to say ‘send an e-mail to THESE addresses when I post to THIS category’. How would I identify that I have made a post OF A CERTAIN CATEGORY, and then implement the mail to an identified list of recipients?

    One other query if I may: not only does it send out a notification on publication of a post, but also if I update it. Could be irksome for recipients if they get a new e-mail every time I go in to correct a typo. Can I restrict the e-mail to the first time only that I publish a post?

    0
    • 32

      I seem to have answered the second question for myself, by wrapping the code in:

      if( ( $_POST['post_status'] == ‘publish’ ) && ( $_POST['original_post_status'] != ‘publish’ ) ) { –code here– }.

      0
  22. 33

    Another typo is:

    add_filter (“wp_mail_from_name”, “my_awesome_mail_from_name”);
    function my_awesome_email_from_name() {

    should be:

    add_filter (“wp_mail_from_name”, “my_awesome_mail_from_name”);
    function my_awesome_mail_from_name() {

    It would be worth to fix the orginal article.

    0
  23. 34

    this article was great but it’s not very easy to follow.

    create email_header.php

    but you didn’t say create email_footer.php

    and from that point on you haven’t said where to add any of the other code.

    this article seems un finished and if anything has just overloaded me with more info and no finished product.
    can you please explain more in detail where the rest of the code goes lol thanks for a great write up it was cool.

    6
  24. 35

    Hello!

    Great tutorial. But I have a problem with the retrieve_password_message which got me stuck.

    It seems the problem is with getting the user_login and the key.

    Anyone else experienced problem with this? I am on a multisite if that might help.

    The error I’m getting:
    Catchable fatal error: Object of class m_wpdb could not be converted to string in

    Thank you

    0
    • 36

      I got the same result Oscar.
      I did some adjustment to the code and got it to work:

      $user_login = $wpdb->get_var(“SELECT user_login FROM $wpdb->users WHERE user_activation_key = ‘$key’”);

      I also needed to follow Avioli’s comment and change to this:

      add_filter (“retrieve_password_message”, “my_awesome_retrieve_password_message”, 10, 2);

      This is working fine for me now.

      2
      • 37

        Important is write the add_filter to accept 2 args
        with
        add_filter (“retrieve_password_message”, “my_awesome_retrieve_password_message”, 10, 2);

        0
  25. 38

    I have to agree with randell.

    this article looks great and something that i would use my self, but it’s not that easy to follow.

    after you create email_header.php part, the steps and nolonger listed in what you need to do.
    i worked out you need to create email_footer.php but from that point on you haven’t said where to add any of the other code.

    Any chance you can update the post and finished the missing steps, as this would be a great post, if it was all complete.

    Thanks

    0
  26. 39

    What if you wanted to include user meta_key and value in the new_user_admin_notification, how would you add that?

    As in a job title and company name…

    0
  27. 40

    please help me,i want use 2 fields to sending a mail in wordpress(not use any plugins),please explain what are the procedures,please

    0
  28. 41

    spent sometime to understand the logic. If you have to create a plugin for this and update as WordPress updates, why bother reading this article?

    BECAUSE you can just use this plugin

    http://wordpress.org/extend/plugins/welcome-email-editor/

    0
  29. 42

    hi. I was helpful through your article and i have a doubt , When i am using wp-mail to send newsletters to my client and on the same time when a new user register in my site, it will automatically send a welcome message to him/her. My doubt is whether it will synchronize itself or happen to get error report.?

    0
  30. 43

    Hi Daniel,
    think my_awesome_email_plugin is missing to reset wp_mail_content_type to the default plain text. Most coders rely on this default and bad things can happen if it is left to html.

    Cheers,
    Tony

    0
  31. 44

    HI,

    Would you have a download link for this example?

    Briliantly wrote!

    0
  32. 45

    This is perfect, but i have to agree with a few of the comments above about being vaugue as to where the code goes.. im not new to coding, but this is my first attempt at creating a plugin…
    So do i add the code to the plugin file we created, or to one of the core WordPress files? like specifically for the “Modifying Existing WordPress System Emails”, In what file does that PHP code go? anybody, anybody at all?

    0
  33. 46

    I want to create a form in html and send parameters onto the plugin. Can you tell me how to do that?

    0
  34. 47

    Thanks for the article,
    There’s a syntax error on email_template file you’ve put for download, SPAN tag was mistakenly closed with a DIV

    0
  35. 48

    Hello there,
    First of all i want to thank you for this function. I’m wondering how can i do it to send posts to some users depending on the category the post is published. Example:
    If the new post is published in category “Sport” the function gets the post and sends it to 2-3 email addresses that are manually putted in the function.
    If another post is published in category “Politics”, the function gets the post and sends it to other 2-3 addresses.
    I have up to 15 mail addresses that should receive the new published posts by mail.
    Thank you and hope that you will help me with a way to do it.

    0
  36. 49

    Hi,
    Please tell me How to send a link on Mail template.My Mail template are showing on Mail id but latest blog link are not at proper position.Please help me

    0
  37. 50

    Hi,

    this is a great tutorial, as usual.

    I was wondering, could I create a hook that would email authors of the site on say a monthly basis to tell them how many views their posts have had?

    1
  38. 51

    Matthew O'Donnell

    April 16, 2014 4:43 pm

    Hi, How would one go about registering to a specific Taxonomy Term and sending a email when a new post is created with that term? Been struggling for days as I only just learning about the email function itself which this article has helped with. Thanks

    0
  39. 52

    great tutorial..thanks a lot!!!!

    0
  40. 53

    Dear Author, am still not clear on where the hooks should go. could you please clarify this for me as am a beginner to the hook features of word-press.

    0

↑ Back to top