Late last year, I shared a post in which I provided some steps on how to change the email sender in WordPress. Specifically, the post provides the steps necessary to change the name and the address from which the email is sent.
You can read the post in its entirety here – and it’s a quick read – but the short if it is that it uses the following filters:
The thing is, I never actually shared how to programmatically send email in WordPress. Though there’s a specific function for doing this, it can get a little more complicated if you’re writing an advanced feature of a theme, plugin, or an application.
Programmatically Send Email in WordPress
Note in the screenshot above, you’ll see the standard format of an email sent from a blog. In this case, it’s been sent when a comment has been left on a specific post.
Obviously, this is an email that’s sent using WordPress core; however, WordPress makes this easy to do on our own given the `wp_mail` API.
In the simplest form, all we need is the following information:
- The `to` address
- The subject line of the email
- The actual message
To clarify, times in which you may want to send a customized email within a project would be when, say, a user has signed up for the application, something has been programmatically created, and/or someone has triggered an event.
Obviously, there’s a wide array of use cases.
An Example Email
Anyway, so let’s say that at some point in the application, we’re programmatically creating a user and want to send a welcome email. To do this, we’ll need to setup:
- A message
- A subject line
- A name
- A `from` address
Easy stuff, right?
Typically, I like to make sure that my emails use the content-type of `text/html` so that’s what you’ll see below.
First, I’ll setup the message. Obviously, this is a bit generic but works for the purposes of this post:
$message = 'Hey There,'; $message .= '<p>'; $message .= 'Your account has been created. Your login information is below:'; $message .= '</p>'; $message .= '<p>'; $message .= '<ul>'; $message .= '<li>Username: ' . $email_address . '</li>'; $message .= '<li>Password: ' . $password . '</li>'; $message .= '</ul>'; $message .= '</p>'; $message .= '<p>'; $message .= 'You can login to the application <a href="http://YourAddress.com/">here</a>.'; $message .= '<p>';
Next, I’ll make sure that I setup the appropriate filter for using the `text/html` content-type:
add_filter( 'wp_mail_content_type', create_function( '', 'return "text/html";' ) );
Note that in the line above, I’m using an anonymous function in PHP. For what it’s worth, some people are not a fan of this particular method because of the impact anonymous functions can have on performance, because of code readability, and because it doesn’t actually define a function that’s maintainable.
All of those points are valid; however, within the context of sending a simple email in WordPress for this specific purpose, I think it’s an acceptable use. The topic of anonymous functions is stuff for another post anyway :).
And then I’ll define the `to` address and the subject line:
wp_mail( email@example.com, 'Your Account Has Been Created!', $message );
This also assumes that you’ve already setup the necessary filters for customizing the sender email address and sender name.
Done and done – not bad, right?
Generally speaking, this is a strategy that I’ve used in a number of different projects when I need to send a single, very specific email to the end user without adding options and/or cluttering the UI with something an administrator needs to change.
Of course, this isn’t difficult to customize – you could easily grab information from, say, the `$_POST` collection if information comes from a form, you could read a value from the WordPress database if you opt to expose some options for the user, or more.
Clearly, your mileage may vary based on your implementation.