I have a custom form in my module,which includes only three fields (Title, URL, email). Now how to send a email to the person who entered that form saying your form has been submitted (or any message).

My code :


/**
* implements hooks_menu
*/

function planet_extension_menu(){
$items['planetext']=array(
'title' => t('DEMO Planet Extension'),
'description' => 'Feeds aggregation ',
'page callback' => 'drupal_get_form',
'page arguments' => array('planet_extension'),
'access callback' => TRUE,
);
return $items;
}

// callback function
function planet_extension($form,&$form_submit){
$form['url']=array(
'#title' => 'URL',
'#type' => 'textfield',
'#required' => TRUE,
'#size' => 35,
'#description' => t('Enter the URL'),
'#attributes' =>array('placeholder' => t('www.example.com'))
#'#default_value' => t('www.example.com')
);
$form['title']=array(
'#title' => 'Title',
'#type' => 'textfield',
'#required' => TRUE,
'#size' => 35,
'#description' => t('Enter the Title'),
'#attributes' =>array('placeholder' => t('Title'))
);
$form['email']=array(
'#title' => 'email',
'#type' => 'textfield',
'#required' => TRUE,
'#size' => 35,
'#description' => t('Enter you valid email'),
'#attributes' =>array('placeholder' => t('example@example.com'))
);
$form['submit']=array(
'#type' =>'submit',
'#value' => 'Submit',
);
return $form;
}

//below is the form submission function

function planet_extension_submit($form,$form_state){
//store the values entered in form into variables
$values = array(
'url' => $form_state['values']['url'],
'title' => $form_state['values']['title'],
'email' => $form_state['values']['email'],
);
//insert the variables into the table
$insert = db_insert('planet_extension')
                -> fields(array(
                        'url' => $values['url'],
                        'title' => $values['title'],
                        'email' => $values['email'],
                ))
                ->execute();

        drupal_set_message(t('Form submitted. Check your email for varification')); //custom message after form submission is successful
}


Comments

jaypan’s picture

function planet_extension_submit($form,$form_state){
//store the values entered in form into variables
$values = array(
'url' => $form_state['values']['url'],
'title' => $form_state['values']['title'],
'email' => $form_state['values']['email'],
);
//insert the variables into the table
$insert = db_insert('planet_extension')
                -> fields(array(
                        'url' => $values['url'],
                        'title' => $values['title'],
                        'email' => $values['email'],
                ))
                ->execute();

// See documentation: https://api.drupal.org/api/drupal/includes!mail.inc/function/drupal_mail/7
drupal_mail('planet_extension', 'planet_extension_mail', $form_state['values']['email'], 'en', array('values' => $values));

        drupal_set_message(t('Form submitted. Check your email for varification')); //custom message after form submission is successful
}

// Implementation of hook_mail()
function planet_extension_mail($key, &$message, $params)
{
  // Use the key we used in our call to drupal_mail()
  if($key == 'planet_extension_mail')
  {
    // Set the subject and body of the message.
    $message['subject'] = t('This will be the subject of your email')
    $message['body'] = t('This value was submitted: !value', array('!value' => $params['values']['url']));
  }
}

You need to do two things to send emails:
1) Set the mail up in an implementation of hook_mail()
2) Send the mail using drupal_mail()

Contact me to contract me for D7 -> D10/11 migrations.

anilshrish’s picture

is there anyway i can attach file to my mail ? Thank you

darrylmabini’s picture