| WordPress

wordpress php mail functionPHP has a built-in function called mail() that allows you to send email.  The advantage of mail() is that it’s very easy to use.  The disadvantage is that it’s also very easy to use improperly, allowing spammers to send rafts of email through your site.

Because of this, there’ve been many wrappers and libraries written to help prevent that abuse.  Most CMSs have their own built in methods, we’re going to take a look at WordPress’ in this post.

The wp_mail() function

The definitive source for information on this function is the WordPress Codex.  If you really want to know everything about it, you should go read that.  This post is going to show you some common examples.

The wp_mail() function really only needs three inputs; to, subject, and message.  Here’s the way I like to use it:

$to = ‘steve.jobs@apple.com’;
$subject = ‘Apple Computer’;
$message = ‘Steve, I think this computer thing might really take off.’;

wp_mail( $to, $subject, $message );

That’s all there is to sending a simple email. This is a ridiculously simple example of course. You’d probably want to wrap this in a function, and have it run when certain events occur, like a new post made, or a new comment made.

You’re probably going to use variables instead of hardcoding the message and subject as well. Something like this:

function doer_of_stuff() {
return new WP_Error( 'broke', __( "I've fallen and can't get up", "my_textdomain" ) );

$return = doer_of_stuff();
if( is_wp_error( $return ) ) {
    $to = $site_owner;
        $subject = ‘Error occured on ‘ . date( ‘Y-m-d’ );
        $message = ‘We had an error!  Here it is: ‘ . $return->get_error_message();

        wp_mail( $to, $subject, $message );

Here’s that code in plain English:

First there’s a function that simply reports an error.

Then I run that function, and assign it to the variable $return.

Then I say "If $return is an error, assign some variables and send an email"

Basically you can use wp_mail() to send email in any circumstance, you simply need to decide when that should be.

Other Options: Headers

wp_mail() can also take input for headers and attachments.  The headers input can take any valid mail headers, like this:

$headers = 'From: My Name <myname@example.com>;' . “\r\n”;

And then you could add to that like this:

$headers .= 'BCC: My Other Name <myothername@example.com>;' . “\r\n”;

Note the .= in that line.  That means “append to this variable instead of over writing it”.  That’s how you can make multiple headers in that one variable.

Alternatively, you could make an array out of $headers, like this:

$headers = array(
    ‘From: My Name <myname@example.com>;',
    'BCC: My Other Name <myothername@example.com>;',

Then your function would look something like this:

wp_mail( $to, $subject, $message, $headers );

Other Options: Attachments

The attachments input can take any valid file that exists on the filesystem.  You can make a string like this:

$attachments = WP_CONTENT_DIR . '/uploads/file_to_attach.zip’;

Or, like $headers, you can make an array:

$attachments = array(
WP_CONTENT_DIR . '/uploads/file_to_attach.zip',
WP_CONTENT_DIR . '/uploads/other_file_to_attach.zip'

I’d like to urge you to think long and hard about sending attachments.  If you’re building this programmatically, then you may not know how large your attachments are.


Getting an email when something happens can be really useful.  That said, if you’re not careful your site could end up sending hundreds of emails, and get flagged as a spammer.

Honestly, the logic and reasoning behind whether to send an email is far more difficult to figure out than the code to send that email.  The code is really relatively simple.

Can you is easy.

Should you is much harder.

About the author

Topher is an accomplished programmer, having written his own content management systems and managed some very large websites. He loves to help people and believes playing with WordPress is fun. Topher lives in Michigan, USA.