Use drupal_mail to send an email

Public

1.Tells a lil' about menu callback.
2. How to call a custom form on menu callback.
3. How to validate email address using drupal in-built email validation function
3. How to send email pro-grammatically using drupal_mail
4. How to get html formatted email content

</> CopyGet raw version
php
  1. <?php
  2.  
  3.  
  4. /**
  5.  *
  6.  * Implements hook_menu()
  7.  * Call a custom form as menu callback
  8.  * @return
  9.  * $items array of menu items created programmatically
  10.  */
  11. function test_menu() {
  12.  
  13. $items['test'] = array(
  14. 'title' => t('Send Email Using Drupal mail'),
  15. 'page callback' => 'drupal_get_form',
  16. 'page arguments' => array('test_custom_form', 1),
  17. 'access arguments' => array('access content'),
  18. //'file' => 'test.inc',
  19. //'file path' => drupal_get_path('module', 'test') . '/includes',
  20. );
  21. return $items;
  22. }
  23.  
  24.  
  25. /**
  26.  * fucntion to return custom form on menu callback
  27.  * @param
  28.  * type $form an array consists of form fields
  29.  * @param
  30.  * type $form_state an array consists of form fields values input a user
  31.  * @return
  32.  * $form returns array of dorm fields.
  33.  */
  34. function test_custom_form($form, $form_state) {
  35.  
  36. $form = array();
  37. $form['email'] = array(
  38. '#type' => 'textfield',
  39. '#title' => 'To Email',
  40. '#prefix' => '<div id="email-field-wrapper">',
  41. '#suffix' => '</div>',
  42. '#required' => TRUE,
  43. );
  44. $form['from_email'] = array(
  45. '#type' => 'textfield',
  46. '#title' => 'From Email',
  47. '#prefix' => '<div id="from-email-address">',
  48. '#suffix' => '</div>',
  49. '#required' => TRUE,
  50. );
  51. $form['email_body'] = array(
  52. '#type' => 'textarea',
  53. '#title' => 'Email Body',
  54. '#prefix' => '<div id="email-body">',
  55. '#suffix' => '</div>',
  56. '#required' => TRUE,
  57. );
  58. $form['submit_form'] = array(
  59. '#type' => 'submit',
  60. '#value' => t('Send Email'),
  61. '#submit' => array('test_custom_form_submit'),
  62. );
  63. $form['#validate'] = array('test_custom_form_validation');
  64. return $form;
  65. }
  66.  
  67. /**
  68.  * validation function for custom form
  69.  * check for vadlidation for email address.
  70.  * @param type $form
  71.  * @param type $form_state
  72.  */
  73. function test_custom_form_validation($form, &$form_state) {
  74. $mail = $form_state['values']['email'];
  75. $from_email = $form_state['values']['from_email'];
  76. if (!valid_email_address($mail)) {
  77. form_set_error('email', t('Please Enter a valid to email address.'));
  78. }
  79.  
  80. if (!valid_email_address($from_email)) {
  81. form_set_error('from_email', t('Please Enter a valid from email address.'));
  82. }
  83. }
  84.  
  85. function test_custom_form_submit($form, &$form_state) {
  86. drupal_set_message("form is submitted, thanks");
  87. $email_content = get_mail_content($form_state);
  88. $params = array('body' => $email_content);
  89. $key = 'test_email';
  90. $to = $form_state['values']['email'];
  91. $from = $form_state['values']['from_email'];
  92. $mail = drupal_mail('test', $key, $to, language_default(), $params, $from);
  93. // for better understanding,you can uncomment the following
  94. // dpm($mail);
  95.  
  96. }
  97.  
  98. /**
  99.  * Implements hook_mail()
  100.  * @param
  101.  * type $key to decide which email body to sent on basis of key parameter inacese of multiple email content
  102.  * @param
  103.  * type $message the email content to be sent.Message array contains 'subject and body ' for the email.
  104.  * @param
  105.  * type $params using to get the cusotm email content from a function.This can be used in my other ways aslo as per need.
  106.  */
  107. function test_mail($key, &$message, $params) {
  108. $language = $message['language'];
  109. switch ($key) {
  110. //switching on $key lets you create variations of the email based on the $key parameter
  111. case 'test_email':
  112. $message['subject'] = t('Test Email');
  113. //the email body is here, inside the $message array
  114. $message['body'][] = $params['body'];
  115. break;
  116. }
  117. }
  118.  
  119. /**
  120.  * function to get the html formatted email content
  121.  * @param
  122.  * type $form_state to get values from form fields.
  123.  * @return
  124.  * $body type string return the html email content
  125.  */
  126. function get_mail_content($form_state) {
  127.  
  128. $email_to = $form_state['values']['email'];
  129. $pos = strpos($email_to, '@');
  130. $user_name = substr($email_to, 0, $pos);
  131. $body = '';
  132. $body .= 'Hi ' . $user_name . '<br>';
  133. $body .= 'Please find my test email. <br>';
  134. $body .= $form_state['values']['email_body'] . '<br>';
  135. $body .= 'Thanks<br>';
  136. $body .= 'TestTeam';
  137. return $body;
  138. }

Comments

white dragon's picture

Nice Post. Thx.

how is add css style to body section?

ooops's picture

css will always be inline.

Negative rating's picture

Why do people rate something negatively without saying what's wrong. I wan't to use this code example but I'm worried there are errors or problems with the code because of the negative rating. If you negatively rate something without saying why, you're a d***.

ooops's picture

you should better give a try for this code.

anilshrish's picture

how to attach file in mail ?

Ehsan's picture

What's the purpose of line 108?