email_example.module 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * @file
  4. * Example of how to use Drupal's mail API.
  5. */
  6. /**
  7. * @defgroup email_example Example: Email
  8. * @ingroup examples
  9. * @{
  10. * Example of how to use Drupal's mail API.
  11. *
  12. * This example module provides two different examples of the Drupal email API:
  13. * - Defines a simple contact form and shows how to use drupal_mail()
  14. * to send an e-mail (defined in hook_mail()) when the form is submitted.
  15. * - Shows how modules can alter emails defined by other Drupal modules or
  16. * Core using hook_mail_alter by attaching a custom signature before
  17. * they are sent.
  18. */
  19. /**
  20. * Implements hook_mail().
  21. *
  22. * This hook defines a list of possible e-mail templates that this module can
  23. * send. Each e-mail is given a unique identifier, or 'key'.
  24. *
  25. * $message comes in with some standard properties already set: 'to' address,
  26. * 'from' address, and a set of default 'headers' from drupal_mail(). The goal
  27. * of hook_mail() is to set the message's 'subject' and 'body' properties, as
  28. * well as make any adjustments to the headers that are necessary.
  29. *
  30. * The $params argument is an array which can hold any additional data required
  31. * to build the mail subject and body; for example, user-entered form data, or
  32. * some context information as to where the mail request came from.
  33. *
  34. * Note that hook_mail() is not actually a hook. It is only called for a single
  35. * module, the module named in the first argument of drupal_mail(). So it's
  36. * a callback of a type, but not a hook.
  37. */
  38. function email_example_mail($key, &$message, $params) {
  39. global $user;
  40. // Each message is associated with a language, which may or may not be the
  41. // current user's selected language, depending on the type of e-mail being
  42. // sent. This $options array is used later in the t() calls for subject
  43. // and body to ensure the proper translation takes effect.
  44. $options = array(
  45. 'langcode' => $message['language']->language,
  46. );
  47. switch ($key) {
  48. // Send a simple message from the contact form.
  49. case 'contact_message':
  50. $message['subject'] = t('E-mail sent from @site-name', array('@site-name' => variable_get('site_name', 'Drupal')), $options);
  51. // Note that the message body is an array, not a string.
  52. $message['body'][] = t('@name sent you the following message:', array('@name' => $user->name), $options);
  53. // Because this is just user-entered text, we do not need to translate it.
  54. // Since user-entered text may have unintentional HTML entities in it like
  55. // '<' or '>', we need to make sure these entities are properly escaped,
  56. // as the body will later be transformed from HTML to text, meaning
  57. // that a normal use of '<' will result in truncation of the message.
  58. $message['body'][] = check_plain($params['message']);
  59. break;
  60. }
  61. }
  62. /**
  63. * Sends an e-mail.
  64. *
  65. * @param array $form_values
  66. * An array of values from the contact form fields that were submitted.
  67. * There are just two relevant items: $form_values['email'] and
  68. * $form_values['message'].
  69. */
  70. function email_example_mail_send($form_values) {
  71. // All system mails need to specify the module and template key (mirrored from
  72. // hook_mail()) that the message they want to send comes from.
  73. $module = 'email_example';
  74. $key = 'contact_message';
  75. // Specify 'to' and 'from' addresses.
  76. $to = $form_values['email'];
  77. $from = variable_get('site_mail', 'admin@example.com');
  78. // "params" loads in additional context for email content completion in
  79. // hook_mail(). In this case, we want to pass in the values the user entered
  80. // into the form, which include the message body in $form_values['message'].
  81. $params = $form_values;
  82. // The language of the e-mail. This will one of three values:
  83. // - user_preferred_language(): Used for sending mail to a particular website
  84. // user, so that the mail appears in their preferred language.
  85. // - global $language: Used when sending a mail back to the user currently
  86. // viewing the site. This will send it in the language they're currently
  87. // using.
  88. // - language_default(): Used when sending mail to a pre-existing, 'neutral'
  89. // address, such as the system e-mail address, or when you're unsure of the
  90. // language preferences of the intended recipient.
  91. //
  92. // Since in our case, we are sending a message to a random e-mail address that
  93. // is not necessarily tied to a user account, we will use the site's default
  94. // language.
  95. $language = language_default();
  96. // Whether or not to automatically send the mail when drupal_mail() is
  97. // called. This defaults to TRUE, and is normally what you want unless you
  98. // need to do additional processing before drupal_mail_send() is called.
  99. $send = TRUE;
  100. // Send the mail, and check for success. Note that this does not guarantee
  101. // message delivery; only that there were no PHP-related issues encountered
  102. // while sending.
  103. $result = drupal_mail($module, $key, $to, $language, $params, $from, $send);
  104. if ($result['result'] == TRUE) {
  105. drupal_set_message(t('Your message has been sent.'));
  106. }
  107. else {
  108. drupal_set_message(t('There was a problem sending your message and it was not sent.'), 'error');
  109. }
  110. }
  111. /**
  112. * Implements hook_mail_alter().
  113. *
  114. * This function is not required to send an email using Drupal's mail system.
  115. *
  116. * Hook_mail_alter() provides an interface to alter any aspect of email sent by
  117. * Drupal. You can use this hook to add a common site footer to all outgoing
  118. * email, add extra header fields, and/or modify the email in anyway. HTML-izing
  119. * the outgoing email is one possibility.
  120. */
  121. function email_example_mail_alter(&$message) {
  122. // For the purpose of this example, modify all the outgoing messages and
  123. // attach a site signature. The signature will be translated to the language
  124. // in which message was built.
  125. $options = array(
  126. 'langcode' => $message['language']->language,
  127. );
  128. $signature = t("\n--\nMail altered by email_example module.", array(), $options);
  129. if (is_array($message['body'])) {
  130. $message['body'][] = $signature;
  131. }
  132. else {
  133. // Some modules use the body as a string, erroneously.
  134. $message['body'] .= $signature;
  135. }
  136. }
  137. /**
  138. * Supporting functions.
  139. */
  140. /**
  141. * Implements hook_menu().
  142. *
  143. * Set up a page with an e-mail contact form on it.
  144. */
  145. function email_example_menu() {
  146. $items['example/email_example'] = array(
  147. 'title' => 'E-mail Example: contact form',
  148. 'page callback' => 'drupal_get_form',
  149. 'page arguments' => array('email_example_form'),
  150. 'access arguments' => array('access content'),
  151. );
  152. return $items;
  153. }
  154. /**
  155. * The contact form.
  156. */
  157. function email_example_form() {
  158. $form['intro'] = array(
  159. '#markup' => t('Use this form to send a message to an e-mail address. No spamming!'),
  160. );
  161. $form['email'] = array(
  162. '#type' => 'textfield',
  163. '#title' => t('E-mail address'),
  164. '#required' => TRUE,
  165. );
  166. $form['message'] = array(
  167. '#type' => 'textarea',
  168. '#title' => t('Message'),
  169. '#required' => TRUE,
  170. );
  171. $form['submit'] = array(
  172. '#type' => 'submit',
  173. '#value' => t('Submit'),
  174. );
  175. return $form;
  176. }
  177. /**
  178. * Form validation logic for the contact form.
  179. */
  180. function email_example_form_validate($form, &$form_state) {
  181. if (!valid_email_address($form_state['values']['email'])) {
  182. form_set_error('email', t('That e-mail address is not valid.'));
  183. }
  184. }
  185. /**
  186. * Form submission logic for the contact form.
  187. */
  188. function email_example_form_submit($form, &$form_state) {
  189. email_example_mail_send($form_state['values']);
  190. }
  191. /**
  192. * @} End of "defgroup email_example".
  193. */