The wp_mail() Function in WordPress

wordpress php mail function

PHP 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:

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

wp_mail( $to, $subject, $message );
?>{/codecitation}

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:

{codecitation}<?php

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 );
}
?>{/codecitation}

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:

{codecitation}$headers = ‘From: My Name <myname@example.com>;’ . “\r\n”;{/codecitation}

And then you could add to that like this:

{codecitation}$headers .= ‘BCC: My Other Name <myothername@example.com>;’ . “\r\n”;{/codecitation}

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:

{codecitation}$headers = array(
‘From: My Name <myname@example.com>;’,
‘BCC: My Other Name <myothername@example.com>;’,
);{/codecitation}

Then your function would look something like this:

{codecitation}wp_mail( $to, $subject, $message, $headers );{/codecitation}

Other Options: Attachments

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

{codecitation}$attachments = WP_CONTENT_DIR . ‘/uploads/file_to_attach.zip’;{/codecitation}

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

{codecitation}$attachments = array(
WP_CONTENT_DIR . ‘/uploads/file_to_attach.zip’,
WP_CONTENT_DIR . ‘/uploads/other_file_to_attach.zip’
);{/codecitation}

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.

Summary

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.

Instructor

  • 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.

0 0 votes
Blog Rating
Subscribe
Notify of
7 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
gavjof

Do you guys still use Mandrill? I recently had issues with wp_mail being unreliable. My client noticed that people were following up on enquiries that were simply not being delivered to him by WordPress. I decided to try out Mandrill and it have not looked back (and it’s free for 12K emails per month). Simple drop in the plugin [url=https://wordpress.org/plugins/wpmandrill/]https://wordpress.org/plugi…[/url] and once setup you get access to powerful reports on deliverability, etc.

steve

Hi Gavin
Yes, great point. This site runs on Mandrill for some of the reasons you mention.

vicky

Hi,
I am getting 200 emails daily.How to stop it,please suggest for same.
Thanks

Web design Cheshire

A client of mine is using the SMTP plugin for WordPress emails. Would this function still work fine to send emails or would it needed to be coded differently?

León Guerra

Emails from my Site Go To Spam Folder (hotmail, gmail…), so, I need a Functions.php to Fix it.

Bjornen

Okay?
And how do I add the send button with everything else pre-filled?

I’m looking at adding this to a custom dashboard widget.

Dai Software

Nice Blog.

7
0
Would love your thoughts, please comment.x
()
x