How To Programmatically Send Email in WordPress

July 8, 2013

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:

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 :).

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.

Just Getting Started with WordPress?

I write a lot about WordPress development but if you're just getting started, I recommend checking out WPBeginner. They have free training videos, glossary, and more.

WPSessions hosts some of the best WordPress training you’ll find anywhere from many well-known speakers.

If there’s something you’d like to learn, and it’s not already covered here, it’s probably been covered at WPSessions. If you use the special link below you can watch your first session for free and get a steep discount on the full-access VIP membership.