How to Set Up Email Notifications in WordPress
Transactional emails are one-time messages sent to a specific address, triggered by a user action.
While the word “transactional” doesn’t sound all that exciting, it’s really not all that complicated. There are many examples in WordPress: When users register with a website they get an email, and when someone leaves a comment on a post the admin might receive an email notification. Then there are email notifications for password recoveries and so on.
In this Weekend WordPress Project we’ll take a look at how you can use the
wp_mail() function to send a simple email based on a custom user action. Specifically, we’ll take a look at how we can notify authors that their work has been published.
Many people don’t know that WordPress has an easy-to-use function for sending email messages. The
wp_mail function allows you to send email flexibly by specifying many relevant parameters, let’s take a look.
The email of the receiver
The email subject
The email’s message
Mail headers, separated by newlines (
A file or array of files to attach
A very simple text email can be sent by specifying just the first three parameters, here’s a quick example:
Sending HTML Emails
HTML emails can be sent by switching the content type of the email. This can be done via the
wp_mail_content_type hook. Here’s an example of the same email as above but with a little additional formatting. Of course, this goes in your functions.php file:
Note that we add the filter before sending the email and then remove it right away afterward. I’ll explain this in the next section.
In my experience there are two issues people often wonder about with
wp_mail(). The first one is all about that HTML filter we added earlier. If you add the filter anywhere in your
functions.php file or your plugin, emails will always use the HTML content type.
Unfortunately, this doesn’t play nice with some WordPress internals and password reset emails aren’t sent out properly. Whenever you want to send an HTML email, make sure to add the filter before and remove it right after so default WordPress emails continue to function.
The second issue arises if the email function isn’t set up properly on your server. This happens on some hosts but it is more common to see this on localhost environments like MAMP and XAMPP. The WP Mail SMTP plugin can help you out. While this plugin works just fine, it isn’t actively updated so you may also want to take a look at Easy SMTP Mail.
These plugins allow you to send your emails through a SMTP server, like your Gmail account for example. This has worked just fine on all the local and remote hosts I’ve tried over the years.
Triggering Emails On User Actions
To send a transactional email we’ll need to hook into some action. As I mentioned earlier, let’s now look at how we can notify authors when their work has been published
publish_post action seems like a good fit. According to the WordPress Codex:
publish_post is an action triggered whenever a post is published, or if it is edited and the status is changed to publish.
Here’s all the code put together:
There shouldn’t be a whole lot of new code here. Let me explain what it does: I’ve defined the function which sets the content type and I’ve hooked a function into the
publish_post action. When this action is triggered, I check that this is indeed a post (and not a page or some other custom post type). If it is a post, I go ahead and send the email, that’s all.
There is a whole lot more you can do here. From basic welcome emails to user notifications when a new post is published in a specific category, the options are endless.
One thing to keep in mind is that your server is most probably not made to send hundreds upon hundreds of emails. If you want to send out bulk emails you should talk with your host and perhaps look into tying in the Mailchimp or Madmimi API, otherwise you may get blocked.