Daily Contact Form Tests with Python and Cron

For most websites, the contact form is the primary means of lead generation. A contact form that fails to email you those leads can potentially devastate your business - no exaggeration. How can you tell whether business is slow or your contact form just isn't working? The answer is to simply test the form by filling it out yourself and seeing if you receive the email. This article aims to automate that process.

Time Required

30-45 minutes

Tools You Will Need

First and foremost, you will need a UNIX server with root access. That server needs the following items installed:

Getting Everything Set Up

Move to your scripts directory

cd to the directory where you keep all your custom scripts. In case you don't have one, you can do what I did and make a scripts directory from the home folder.
sudo mkdir ~/scripts
cd ~/scripts

Set up working environment

Now that you're in the ~/scripts directory, make another directory and call it daily_mail and cd into it.
mkdir daily_mail
cd daily_mail
(Sidenote: Learn how to make a directory and cd into it in one line) Now we want to setup our virtualenv, which allows for local python packages.
virtualenv env --python=python2.6 #Put your version here
To activate the virtual environment (and grant python access to the libraries we will be installing), execute the following from the ~/daily_mail directory:
. env/bin/activate
If the environment activation was successful, you will see (env) somewhere in your next terminal line. (To disable the virtual environment at anytime, simply execute the command "deactivate") We'll now install the Mechanize library. (Note: We activate the virtual environment before installing libraries to make sure that the libraries are installed only to this environment instead of across the whole system).
pip install mechanize

Coding the Contact Form Filler-Outer

Make sure your virtual environment is activated and start python (just type python in the terminal). Create and open contact_form.py in your favorite text editor. Paste the following in. [python] ''' Fill out a contact form! ''' import mechanize # Simulate a browser br = mechanize.Browser() # The URL where the contact form is located br.open("http://www.yourdomain.com/contact") # Choose which form on the page we're going to use, beginning # from the 0th index. If only one form on the page, it's 0. br.select_form(nr=0) # Begin filling br with the name => value pairs of your form. br["name"] = "Joseph McCullough" br["phone"] = "903 555 5555" br["email"] = "Joseph@vertstudios.com" br["message"] = "Daily test of contact form" # Submit and we're done! response = br.submit() [/python] Test by executing
python contact_form.py
You should get an email in your inbox just as if a user had filled out the form. Due to how Cron works, we won't be able to directly call the python file, or else the virtual environment won't get activated and our mechanize library won't be available to use. The solution is to create a bash script that activates the virtual environment, executes the python script, and deactivates the environment. Create and open send_mail in your favorite text editor.
cd ~/scripts/daily_mail 
. env/bin/activate
python contact_form.py
To test this script, first deactivate your virtual environment. Next, make sure the file is executable.
chmod +x send_mail
Now execute
If everything goes according to plan, the python script will execute and you'll get another email in your inbox.

The Cron Job

So our python script and bash helper script work -- great! Now we need to automate the execution of this script so it gets executed at a specified interval. This is where cron comes into play. First, open up the crontab file.
crontab -e
Next, append the following line at the end of the file
0 9 * * * ~/scripts/daily_mail/send_mail
This schedules the send_mail bash script(which executes the python script) to run at 9AM every day. Save and exit the text editor -- you're done! You should receive an email from the form every morning at 9AM (server time). To test if the cron file is working, you can change the line we appended to
* * * * * ~/scripts/daily_mail/send_mail
This executes the script every minute. Just sit by your inbox for a few minutes and see if the email comes. Be sure to change it back ASAP, or your mailbox will get flooded! Also, remember that the crontabs changes do not take effect until you exit your text editor. If you want to use another time interval, I suggest using the Crontab Code Generator. To learn more about Cron, visit the Crontab quick reference.


Have any questions? Get stuck somewhere in the tutorial? Let us know and we'll help you out! August 25, 2011
About the Author:

Joseph is the lead developer of Vert Studios Follow Joseph on Twitter: @Joe_Query
Subscribe to the blog: RSS
Visit Joseph's site: joequery.me