Drupal 7 custom form with mail send functionality

Public

How to create custom form and send mail in drupal 7, step by step explanation.

Get raw version
php
  1. name = Custom form send mail
  2. description = Contact form within block and send mail
  3. core = 7.x
  4. version = 7.x-1.0
  5. package = Custom form
  6.  
  7.  
  8.  
  9. <?php
  10.  
  11. /**
  12.  * Implementation of hook_block_info().
  13.  */
  14. function email_form_block_info(){
  15. $blocks = array();
  16. $blocks['email_form'] = array(
  17. 'info' => t("Custom form with email send Block"),
  18. );
  19. return $blocks;
  20. }
  21.  
  22. /**
  23.  * Implementation of hook_block_view().
  24.  */
  25. function email_form_block_view($delta = ""){
  26. $block = array();
  27. switch($delta){
  28. case "email_form":
  29. $block['subject'] = t("Customized block for mail send");
  30. $block['content'] = drupal_get_form('email_form_form');
  31. break;
  32. }
  33. return $block;
  34. }
  35.  
  36. /**
  37.  * Implementation of hook_form().
  38.  */
  39. function email_form_form($form, &$form_state){
  40. $form = array();
  41.  
  42. $form['#prefix'] = '<div class="widget-trending widget"><h2 class="widget-title">Contact Form</h2><form class="well form-horizontal" action=" " method="post" id="contact_form">';
  43. $form['#suffix'] = '</form></div>';
  44.  
  45. $form['course'] = array(
  46. '#type' => 'select',
  47. '#options' => array(
  48. 'none' => t('Select Courses'),
  49. 'physics' => t('Physics'),
  50. 'chemistry' => t('Chemistry'),
  51. 'math' => t('Math'),
  52. ),
  53. '#default_value' => 'none',
  54. '#attributes' => array(
  55. 'class' => array('selectpicker'),
  56. ),
  57. '#required' => TRUE,
  58. );
  59.  
  60. $form['name'] = array(
  61. '#type' => 'textfield',
  62. '#maxlength'=> 255,
  63. '#size'=> 15,
  64. '#prefix' => '<div class="input-group input_name"><span class="input-group-addon" id="basic-addon1"><span class="ion-person"></span></span>',
  65. '#suffix' => '</div>',
  66. '#attributes' => array(
  67. 'placeholder' => t('Name'),
  68. 'class' => array('form-control'),
  69. ),
  70. );
  71.  
  72. $form['contact'] = array(
  73. '#type' => 'textfield',
  74. '#maxlength'=> 10,
  75. '#size'=> 15,
  76. '#prefix' => '<div class="input-group input_mb"><span class="input-group-addon"><span class="ion-ios-telephone"></span></span><span class="input-group-addon">+91</span>',
  77. '#suffix' => '</div>',
  78. '#attributes' => array(
  79. 'placeholder' => t('Mobile Number'),
  80. 'class' => array('form-control'),
  81. ),
  82. );
  83.  
  84. $form['email'] = array(
  85. '#type' => 'textfield',
  86. '#maxlength'=> 255,
  87. '#size'=> 15,
  88. '#prefix' => '<div class="input-group input_email"><span class="input-group-addon"><span class="ion-ios-email"></span></span>',
  89. '#suffix' => '</div>',
  90. '#attributes' => array(
  91. 'placeholder' => t('Email Id'),
  92. 'class' => array('form-control'),
  93. ),
  94. );
  95.  
  96. $form['message'] = array(
  97. '#type' => 'textarea',
  98. '#cols' => 20,
  99. '#rows' => 5,
  100. '#required' => true,
  101. '#attributes' => array(
  102. 'placeholder' => t('Message'),
  103. 'class' => array('msg_box'),
  104. ),
  105. );
  106.  
  107. $form['submit'] = array(
  108. '#type' => 'submit',
  109. '#value' => 'Submit',
  110. '#attributes' => array(
  111. 'class' => array('btn success'),
  112. ),
  113. );
  114.  
  115. return $form;
  116. }
  117.  
  118. /**
  119.  * Implements hook_form_validate()
  120.  */
  121. function email_form_form_validate($form, &$form_state) {
  122. if ($form_state['values']['course'] == 'none') {
  123. form_set_error('course', t('Please Select Course'));
  124. };
  125. if (empty($form_state['values']['name'])) {
  126. form_set_error('name', t('Please enter name'));
  127. };
  128. if (empty($form_state['values']['contact'])) {
  129. form_set_error('contact', t('Please enter contact no.'));
  130. };
  131. if (!is_numeric($form_state['values']['contact'])) {
  132. form_set_error('contact', t('contact must be Numeric'));
  133. }
  134. if(strlen($form_state['values']['contact']) < 10) {
  135. form_set_error('contact', t('contact no must be 10 digits'));
  136. }
  137. if (empty($form_state['values']['email'])) {
  138. form_set_error('email', t('Please enter email-id.'));
  139. };
  140. if (!valid_email_address($form_state['values']['email'])) {
  141. form_set_error('email', t('Please Enter a valid email address.'));
  142. }
  143. }
  144.  
  145. /**
  146.  * Add a submit handler/function to the form.
  147.  */
  148. function email_form_form_submit($form, &$form_state) {
  149. $email_content = get_mail_content($form_state);
  150. $params = array('body' => $email_content);
  151. $key = 'email_form_email';
  152. $to = 'abc@gmail.com';
  153. $from = $form_state['values']['email'];
  154. $mail = drupal_mail('email_form', $key, $to, language_default(), $params, $from);
  155. if($mail){
  156. drupal_set_message('Thanks, we will be in contact with more information soon.');
  157. }
  158. else {
  159. drupal_set_message('Please try again');
  160. }
  161. // for better understanding,you can uncomment the following:dpm($mail);
  162. }
  163.  
  164.  
  165. /**
  166.  * hook_mail implementation.
  167.  */
  168. function email_form_mail($key, &$message, $params) {
  169. $language = $message['language'];
  170. switch ($key) {
  171. case 'email_form_email':
  172. $message['subject'] = t('Custom Contact Form');
  173. //the email body is here, inside the $message array
  174. $message['body'][] = $params['body'];
  175. break;
  176. }
  177. }
  178.  
  179. /**
  180.  * get all form values.
  181.  */
  182. function get_mail_content($form_state) {
  183. $email_to = 'abc@gmail.com';
  184. $email_from = $form_state['values']['email'];
  185. $body = '';
  186. $body .= 'Name: ' . $form_state['values']['name'] . '<br>';
  187. $body .= 'Course Applied for: ' . $form_state['values']['course'] . '<br>';
  188. $body .= 'Contact: ' . $form_state['values']['contact'] . '<br>';
  189. $body .= 'Email: ' . $email_from . '<br>';
  190. $body .= 'Message: ' . $form_state['values']['message'] . '<br><br>';
  191. $body .= 'Thanks<br>';
  192. $body .= 'Dev Team';
  193. return $body;
  194. }