Simple form to email example

Last modified: March 14, 2009 - 23:10

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

Thank you for writing this

PRZ - July 24, 2008 - 13:49

Thank you for writing this snippet.

I could not get it to function in a block on my 5.9 installation as written. I simplified it by removing variables the following way and it works like a charm:

<?php
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 Email List address: '. $form['email'];
 
drupal_mail('my_sign_up_form', 'an@example.com', 'New Email List Signup', $message, 'email_list@example.com');
 
drupal_set_message('Thank you for signing up to our email list!');
}

return
drupal_get_form('my_sign_up_form');
?>

Not sure why the variables were giving me troubles....

global variable

Bacteria Man - September 17, 2008 - 21:12

This is definitely a handy way to add a quick email form without using (yet another) third-party module.

Regarding the $send_form_to variable. Making it global will make it accessible to functions.

<?php
global $send_form_to;
$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) {
  global
$send_form_to;

 
$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');
?>

-------------------------------------------------------

"The sting in any rebuke is the truth." - Benjamin Franklin

Your email address appears malformed

opihi - May 12, 2009 - 04:19

Hi,
I'm a newcomer to drupal and php programming. I was hoping to copy/paste your code above and elaborate on it once it works, but I'm having trouble getting the example to work with Drupal 6.11. Despite entering a valid email address, I consistently get the "Your email address appears malformed" error.

I've copied & pasted in all 3 examples above. What am I missing?

TIA!

example code for Drupal 5.x

Bacteria Man - May 12, 2009 - 15:09

The example code shown is for Drupal 5.x. There's a few subtle code differences between the two versions, which is no-doubt the reason why the validation is failing. You might want to read-up on the Forms API.

-------------------------------------------------------

"I remember that sound. That's a bad sound."
-- Gwen DeMarco

Great! Thanks for your help!

opihi - May 13, 2009 - 04:28

Great! Thanks for your help! I'm glad it was compatibility & not user error :)

Drupal 6 Version

AdrianSmithUK - September 28, 2009 - 14:56

Here is an updated snippet to cover Drupal 6. Many thanks to the original author and to Christian Roy (roychri) - http://drupal.org/node/358855 for helping me do the conversion.

<?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');
?>

 
 

Drupal is a registered trademark of Dries Buytaert.