CMS - Wordpress - Coding - PHP

WordPress Plugin Development, Lesson 3: Email Functions

This is the third in a series of tutorials meant to introduce programmers to WordPress plugin development. If you haven’t yet, I recommend you go through Lesson 1: Introduction to WP Plugin Development and Lesson 2: The Admin Page. Each one will only take you 10-20 minutes to complete, and then you’ll be ready for this tutorial.

In today’s lesson, we’re going to move on from the basic string capabilities we used in our first plugin to something a little more complex: email functions.

Despite the rise of instant messaging and some high-profile attempts to supplant it (remember Google Wave?), email remains one of the most important ways we communicate online. There are several situations in which you might want a WordPress plugin to send email. Here are a few examples:

  • A contact form to let customers and visitors get in touch with you
  • An order confirmation sent when a purchase is made
  • An alert sent to your email when your site is loading too slowly

There are many more. Today, we’re going to concentrate on the first and most common one, building a contact form plugin that emails you when one of your site visitors has a question or comment.

The Basic Code

We’re going to start this lesson with the most basic code possible and then build on that. Create a file in your /wp-content/plugins/ directory and call it contactform.php:

 * @package WP Contact Form Plugin
 * @version 1.0
Plugin Name: Contact Form
Plugin URI: 
Description: Plugin Example for WordPress
Author: Your Name Here
Version: 1.0
Author URI: 

// Functions
function cform ($content) {
  return $content; 
// Hooks
add_filter( 'the_content', 'cform' );


Make sure you understand the structure of this file and what it is doing. The first section (between the comment marks /* */) establishes the plugin and lets WordPress read its metadata. The second section, Functions, has one empty function called cform() that is going to add a contact form to a WordPress page of our choosing. Right now, all it does is take the content of the page and then return the same content, which isn’t very useful of course, but it provides the framework for us to add more functionality. The last section applies the cform() function to page content with a filter hook.

Creating The Form

Next, replace the content of the cform() function with this code:

 global $post;

 $formsub = $_POST['formsub'];

 if ($formsub) {
  $form = '<br /><a name="cform"></a><br /><strong>
     Thank you for your question or comment. 
     We will be in touch soon.</strong>';
 } else { 
  $form = '<br /><a name="cform"></a><br />
     Do you have a question or comment about our services? 
     Fill out the form below and let us know:<br />
     <form method="post" action="#cform">
     Name: <input type="text" name="cformname" /><br />
     Email: <input type="text" name="cformemail"><br />
     Question or Comment: <textarea name="cformcontent"></textarea><br />
     <input type="submit" name="formsub" value="Submit Form" /></form>'; 
 if ($post->ID == CONTACT_PAGE_ID) 
    { $content = $content . $form; }
 return $content;

This is just a basic non-functional form with a few required fields. We still need to add the email functionality. Notice this line of code: if ($post->ID == CONTACT_PAGE_ID) {. That makes sure that the form is only added to the content area of one page, your Contact Us page. Replace CONTACT_PAGE_ID with the ID (a unique number) of the page where you want the form to appear. If you don’t know your contact page’s ID, open it to edit in your WordPress dashboard and look at the URL that appears in your browser. There will be a variable in the URL that looks like post=. The number that follows this is the post/page ID:


Go to the line of code marked if ($formsub) {. This area of the code covers what you want to happen after the form has been submitted. This is where we’ll put our email functionality. Replace the contents of that if ($formsub) {}code with this:

  $cformname = $_POST['cformname'];
  $cformemail = $_POST['cformemail'];
  $cformcontent = $_POST['cformcontent'];

  // The message
  $message = "Contact Name: ".$cformname.
	"\r\nContact Email: ".$cformemail.
	"\r\nComment: ".$cformcontent;

  // In case any of our lines are larger than 70 characters, we should use wordwrap()
  $message = wordwrap($message, 70, "\r\n");

  // Send
  mail('YOUR_EMAIL', 'New Contact Form Message from '.$cformname, $message);

  $form = '<br /><a name="cform"></a><br /><strong>
     Thank you for your question or comment. 
     We will be in touch soon.</strong>';

The mail() function in PHP builds and sends an email. Most web servers have the ability to send email without any admin changes, but let’s test it. Change YOUR_EMAIL to your actual email address, then save the file. Go to the page you specified with the page ID on your site. You should see the contact form now. Give it a test run, entering anything you want for the name, email, and comment fields.

When you hit the submit button, you should not only see our thank you message, but you should also receive an email in your inbox. Check your spam filter if it doesn’t arrive in a few minutes. If there’s still no message there, check that you spelled your email address correctly, then get in touch with your hosting provider and make sure their servers can send mail from code. I’ve never worked with a hosting provider that didn’t do this out of the box, so this shouldn’t be a problem.

The mail() function is really pretty simple. It only requires three variables: an address to send the message to, a subject, and the body of the message. The rest of that function is just building the message and preparing it to send. Go over the code and make sure you understand what it’s doing and how.

A Note on Ethics

It took us a while to get to the meat of this function, because I wanted to put it in a working context, but once we got there, I hope you can see that there isn’t much work needed to make your plugin send mail. Obviously, this is one of those “with great power comes great responsibility” sort of programming situations. Don’t use this code to spam anyone. You’ll get in trouble with your hosting provider, and your visitors will hate you. If you are going to send large numbers of emails out to addresses other than your own, use an email service like MailChimp rather than your hosting server.

What’s Next?

This is just about the simplest imaginable WordPress email plugin. Take this code, and expand on it. Here are a few ideas:

  • Add form validation to make sure your visitors are leaving valid comments and email addresses.
  • Add more fields to your form, to gather more information.
  • Add a timestamp and the URL of the page to your email, so you know exactly where it came from and when.

Good luck, and happy programming!

About the author

Ian Rose is a web developer, blogger, and writer living in Portland, OR. In his day job, he develops WordPress plugins and custom PHP solutions, focusing on nonprofit clients. By night, he attempts to write both fiction and nonfiction. Ian's site is Seaworthy Web Solutions

Share this post

Leave a Comment

Subscribe To Our Newsletter