Community Documentation

Simple form to email example

Last updated April 26, 2010. Created by AjK on December 2, 2007.
Edited by WorldFallz, bryan kennedy. Log in to edit this page.

Inspired by http://drupal.org/node/197092 I decided to write a short snippet to demo how to take a simple form field, validate it and send the field to an email address on submission.

Like the above post, this snippet just asks for an email address with a submit button. Validation just checks the email address syntax.

This is really meant as a "starting point", it's up to you, the reader, to add bells and whistles as required.

To use this snippet, create a new block. Cut'n'paste the code below and set the blocks input filter to "PHP". Then place the block somewhere to use it.

<?php
$send_form_to
= 'jdow@example.com'; // Change this!

function my_sign_up_form() {
 
$form['email'] = array(
   
'#type' => 'textfield',
   
'#title' => '',
   
'#size' => '20',
  );
 
$form['submit'] = array(
   
'#type' => 'submit',
   
'#value' => 'Subscribe',
  );
  return
$form;
}

function
my_sign_up_form_validate($form_id, $form) {
  if (!
valid_email_address($form['email'])) {
   
form_set_error('email', 'Your email address appears malformed');
  }
}

function
my_sign_up_form_submit($form_id, $form) {
 
$message = 'New signup email address: '. $form['email']; // Body of your email here.
 
drupal_mail('my_sign_up_form', $send_form_to, 'New signup', $message, variable_get('site_mail', 'an@example.com'));
 
drupal_set_message('Thank you for signing up to our email alerts'); // Your thank you message here
}

return
drupal_get_form('my_sign_up_form');
?>

btw, the above is a basic starting point and does not include any filtering so you are advised to see the handbook section on writing secure code

Drupal 6 Version

<?php
/**
* Create a custom php block
* Place the block in the rh column.
*/

/**
* Create a form with a single text field and submit button.
*/
function my_form() {

 
$form['email'] = array(
   
'#type' => 'textfield',
   
'#title' => '',
   
'#size' => '20',
  );
 
$form['submit'] = array(
   
'#type' => 'submit',
   
'#value' => 'Subscribe',
  );
  return
$form;
}

/**
* Validate the form.
*/
function my_form_validate($form, &$form_state) {
 
$valid_email = $form_state['values']['email'];
  if (!
valid_email_address($valid_email)) {
 
form_set_error('email', 'Your email address -- ' . $valid_email . ' -- appears malformed');
  }
}

/**
* Create the hook_mail function
* Required in Drupal 6
*/
function my_form_mail($key, &$message, $params) {

 
$headers = array(
   
'MIME-Version' => '1.0',
   
'Content-Type' => 'text/html; charset=UTF-8; format=flowed',
   
'Content-Transfer-Encoding' => '8Bit',
   
'X-Mailer' => 'Drupal'
 
);

  foreach (
$headers as $key => $value) {
   
$message['headers'][$key] = $value;
  }

 
$message['subject'] = $params['subject'];
 
$message['body'] = $params['body'];
}

/**
* Create the form submit function
*/
function my_form_submit($form, &$form_state) {

   
$valid_email = $form_state['values']['email'];
   
$from = 'test@example.com';
   
$body = 'New Email Sent = ' . $valid_email;
 
   
$params = array(
   
'body' => $body,
   
'subject' => 'This is a subject',
    );
 
   
//NB First argument of the drupal_mail function must match the first part of the hook_mail function.
    //Therefore the first argument is  'my_form' and the the mail hook is 'my_form_mail'
    //For more info see:
    //http://api.drupal.org/api/function/drupal_mail/6    
    //http://api.drupal.org/api/function/hook_mail/6
  
   
if (drupal_mail('my_form', 'some_mail_key', $valid_email, language_default(), $params, $from, TRUE))
    {
       
drupal_set_message('An email has been sent to ' . $valid_email);     
    } else {
       
drupal_set_message('There was an error sending your email');
    }
}

/**
* Return the form.
*/
return drupal_get_form('my_form');
?>

Comments

Thanks

Thanks for above. It was really helpful ;-)

Nice start

Thanks for this. Much appreciated.
The form sends the email BACK to the submitter though.
How about having the form send (collect) the email to an internal (site) address?
Much more useful to collect contact info & then set up an auto-responder to reply.
Guess I am not sure why you would want a visitor to be able to send themselves an eMail?

To clarify; This is PERFECT for what I need and want. Simple, no fuss or muss. Single form field for email address (harvest from user) and a button. SWEET., BUT, I want the form to send the email TO me at a (hidden form specified) internal site email address with the USER (form entered) address as the FROM or SENDER so that my internal auto-responder will reply to them and then I also capture their email address. Cool?

I think you forgot to

I think you forgot to put
drupal_mail('my_form', 'some_mail_key', $valid_email, language_default(), $params, $from, TRUE);
somewhere above "//NB First argument of the..." comments

would this work for Drupal 7?

how would one accomplish this in drupal 7? pasting the code from Drupal 6 just prints out the word "Array"

I successfully managed the

I successfully managed the code for Drupal 6 to get it work in Drupal 7 almost without changes. The code is lower.

Thanks for the help

I was having trouble figuring this out for the longest time but your note about the first argument cleared things up for me.

Insecure code

the snippet is tagged like Insecure code.., why is insecure?

Same question here.

Same question here.

Working in Drupal 7

I successfully managed the code for Drupal 6 to get it work in Drupal 7 almost without changes.

I only made $body an array $body[]:

    $body[] = 'New Email Sent';
    $body[] = '123123131232';

There was a error in mail system which implodes body with something, and if it's not an array body stays empty.

I use this form in a created custom module.

Work in Drupal 7

To work in block you must change
return drupal_get_form('my_form');
to
return drupal_render(drupal_get_form('my_form'));

Why is this code insecure?

Why is this code insecure? The only user-submitted content is e-mail, but there is a validation. Only problem is spam protection.

Page status

About this page

Drupal version
Drupal 5.x, Drupal 6.x

Reference

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.