Ever have the need to log emails from a contact form inside of of wordpress? Here’s my solution.
The Problem
The client uses the Contact Form 7 wordpress plugin for events, registrations and subscriptions. Up until this point all the emails needed to be cut and pasted manually into a master list for marketing or other action items.
Specific actions are taken with each contact form. Some are simply added to a master email list while others are used to send content through snail mail. The need for more forms over the next few months will be increasing. These forms are very time sensitive so the solution must be dynamic enough to preclude more than a few hours in turnaround.
The Solution
If resources were unlimited (time and budget) this would be a great place to implement pre-built CRM (customer relations mangment) system. There are plenty of open source and commercial applications that would work well.
The biggest limiting resource here is time. The client knows how use wordpress and the current site is already well established with content. So we’ll hack it.
Logging Emails
Logging outgoing emails from any type of contact form is essential. This could be as simple as storing them in a database or even writing to a flat file. Logging emails will help you pick up security issues as well as troubleshooting.
The solution is pretty simple. Every email sent out through wordpress uses the wp_mail() function, including forms through Contact 7. All we need to do is capture the $_POST variables when a call is made to wp_mail().
We can use the add_action() hook from wordpress to accomplish this.
A simple example
We’ll insert our hook in the functions.php page for the theme. For this example I’ll use a theme called custom.
wp-content/themes/custom/functions.php
We’ll create a function to capture the posts. This function won’t really do anything but you’ll get the point. Then we’ll hook the function into wp_mail().
function saveContact() {
// all of our post variables are in the $contact array
$contact = $_POST;
// now would be a good time to clean and filter
foreach( $contact as $key => $value ) {
// trim it, escape ... it you know the drill
}
// insert into the database or save a flat file
}
// hook it
add_action( 'wp_mail', 'saveContact' );
Now anytime wordpress sends an email it will call our saveContact() function. This is fine to just capture the data but we want to do something with it. Keep in mind Contact 7 adds in some of its own post variables so you may want to pop those off before processing the data.
If you want to echo anything back to test this turn off your javascript otherwise Contact 7 will send an ajax call behind the scenes.
An advanced example (go cURL yourself)
Wordpress is great but I want to use a MVC framework to handle all of the the different actions. I want to know what form was sent and use the information to populate various tables. I use Kohana but any of the popular frameworks can handle this in the same way. The benefit here is I can use the framework’s built in cleaning and database helpers. We’ll use cURL to send the post variables to our controller.
I’ll send the data to a controller in public_html/example. So the domain www.mydomain.com has a standard wordpress install but we have a kohana controller living in www.mydomain.com/example. I’ll also send along the URI where the form originated. We’ll take care of all the data inside the framework. Clear as mud?
Wordpress by the way does not like putting this source code inside the post. You can check it out here.
For security you can make sure the cURL posts are only sent from your domain.
With permalinks enabled you can differentiate between forms posted from something like www.mydomain.com/contact and www.mydomain.com/contact/subscribe.