I needed to add a checkbox to Drupal’s Contact Us form for a client of mine. The client requested that I add a way for those filling out the form to request a newsletter. I discovered it’s pretty easy to add a form field and get it added as part of the email that’s sent. You just need to hack into the module on your server (sorry, no GUI for this). Here’s what I did:
First, find the module file:
modules/contact/contact.module
If you prefer command line editing open up that file in your favorite editor. If you prefer the download/edit/upload approach via FTP that will work also. I assume if you have interest in this article, that you have some knowledge of either of those 2 methods. If not, I wonder how you set up Drupal in the first place.
Now that you are in edit mode, find a chunk of code that talks about not allowing anonymous users to send themselves a copy for spam reasons. Found that comment? Good, now just above it you are going to add your field. I found it easy to copy/paste an existing form element and edit it’s contents, but you can type it out if you like. Actually, feel free to copy/paste and edit my example below. Here’s what I ended up with:
$form['newsletter'] = array('#type' => 'checkbox', '#title' => t('Sign me up for the Newsletter'), ); |
You may want to add a comment somewhere in there to remind yourself at a later date that this is no longer part of the original configuration.
That was easy, now I just need to tell it to add content to the email sent out so that when that box is checked, it actually does something! Next look for a chunk of comment code that looks like this:
// Compose the body: |
below that you will find a couple lines that start with “$message[] =” which is where the body of the email’s message gets created. Just add another line that says something like the following:
if ($form_values['newsletter'] == 1) { $message[] = "Yes, please send me a newsletter"; } else { $message[] = "No, please do not send me a newsletter"; } |
Now crank open your form and test it out. Assuming you are the recipient of the email being generated, you will get a nice extra piece of text at the bottom of the email with this new content based on the user’s input.




Subscribe to
I have plan to write tutorial for this for drupal 6
at my blog
About Us
Not a good idea to hack anything core. Write the module instead.
But, good tip otherwise.
If you need something more than the site-wide contact form use http://www.drupal.org/project/webform instead. Much more flexible, and you can add as many fields as you want!
I’m trying do it too. But hack the code isn’t a good idea.. I created a new module to do it:
# add a checkbox in contact form to subscribe on newsletter
if( $form_id == 'contact_mail_page' || $form_id == 'bauma_deixe_seu_numero_form' ){
$form['key'] = array(
'#type' => 'checkbox',
'#title' => t('Sign me up for the Newsletter'),
'#weight' => 1,
);
// replace the submit button to checkbox is before
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Send e-mail'),
'#weight' => 2,
);
}
But, I already don’t know what the hook I can use to subscribe the user..
Yo, thank you for this informative tutorial. The tutorial helped me improving my skills greatly. It is always gorgeous to get some new inspiration and I hope to read more of such posts here the next time because nobody will ever stop learning new things. Cheers
Dreamweaver has been lately my personal goto software for years. I really do not know what I would do without having it. There were times when I first began making use of the software, and I thought it was way too difficult. Now I fly around it, and it has become a great asset in my tool box. Anyways thank you for the article.