In this guide we will show you how to send a custom WordPress email to a newly registered user. This will use WP Zapier as an example, but these principles may apply to any WordPress hook.
WordPress is really awesome, in that there’s almost a function for everything you need to make development easier. We will be using the wp_mail function that WordPress provides to send a ‘simple’ custom email.
Here is a code sample that integrates with WP Zapier to send a custom email through WordPress to a newly registered user. In the above paragraph, I mentioned a ‘simple’ email. This will contain dummy text but you may include any HTML an email can render for your users.
In the above code, the wp_mail function takes 5 parameters being $to, $subject, $message, $headers, $attachments. The last two are option and only need to pass three of the required parameters for this to work.
This will send an email from your WordPress site whenever a new user is created in WordPress from Zapier using the WP Zapier plugin. As mentioned, you can use this example to implement on another solution or another hook such as login, registration, membership checkout for a plugin like Paid Memberships Pro and more.
We will focus on the four variables for the custom WordPress email we sent to our test user:
- $email – This is the email address you want to send it to, in the above example we get it from the user’s WordPress ID. This can be hardcoded too.
- $subject – The subject of the email that’s being sent, this could also be set dynamically.
- $message – The actual email body content. This accepts HTML.
- $headers – This allows us to set the content type of the email, BCC/CC in addresses and more. Here’s more information about headers for BCC/CC email addresses.
The first three points are the required parameters for this to work, once this is done you can send emails on almost any WordPress action trigger.
This is what our simple email looks like, as you can imagine you could build more real/robust emails for your WordPress site.
This guide should explain the wp_mail function in a way that you should be able to implement it onto your own site. This does require a bit of code knowledge, and hopefully is a good starting point for sending custom WordPress emails.
The wp_mail function may return true if the function ran successfully. This does not guarantee that the user will actually receive the email in their inbox.
If you have any questions about implementing this onto your site, please feel free to leave a comment below.