email_example.module 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * @file
  4. * Example of how to use Drupal's mail API.
  5. */
  6. use Drupal\Component\Utility\SafeMarkup;
  7. /**
  8. * @defgroup email_example Example: Email
  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 MailManager::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 MailManager::mail(). The
  27. * goal of hook_mail() is to set the message's 'subject' and 'body' properties,
  28. * as 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 MailManager::mail(). So
  36. * it's a callback of a type, but not a hook.
  37. */
  38. function email_example_mail($key, &$message, $params) {
  39. // Each message is associated with a language, which may or may not be the
  40. // current user's selected language, depending on the type of e-mail being
  41. // sent. This $options array is used later in the t() calls for subject
  42. // and body to ensure the proper translation takes effect.
  43. $options = [
  44. 'langcode' => $message['langcode'],
  45. ];
  46. switch ($key) {
  47. // Send a simple message from the contact form.
  48. case 'contact_message':
  49. $from = \Drupal::config('system.site')->get('mail');
  50. $message['subject'] = t('E-mail sent from @site-name', ['@site-name' => $from], $options);
  51. // Note that the message body is an array, not a string.
  52. $account = \Drupal::currentUser();
  53. $message['body'][] = t('@name sent you the following message:', ['@name' => $account->getUsername()], $options);
  54. // Because this is just user-entered text, we do not need to translate it.
  55. // Since user-entered text may have unintentional HTML entities in it like
  56. // '<' or '>', we need to make sure these entities are properly escaped,
  57. // as the body will later be transformed from HTML to text, meaning
  58. // that a normal use of '<' will result in truncation of the message.
  59. $message['body'][] = SafeMarkup::checkPlain($params['message']);
  60. break;
  61. }
  62. }
  63. /**
  64. * Implements hook_mail_alter().
  65. *
  66. * This function is not required to send an email using Drupal's mail system.
  67. *
  68. * hook_mail_alter() provides an interface to alter any aspect of email sent by
  69. * Drupal. You can use this hook to add a common site footer to all outgoing
  70. * email, add extra header fields, and/or modify the email in anyway. HTML-izing
  71. * the outgoing email is one possibility.
  72. */
  73. function email_example_mail_alter(&$message) {
  74. // For the purpose of this example, modify all the outgoing messages and
  75. // attach a site signature. The signature will be translated to the language
  76. // in which message was built.
  77. $options = [
  78. 'langcode' => $message['langcode'],
  79. ];
  80. $signature = t("\n--\nMail altered by email_example module.", [], $options);
  81. if (is_array($message['body'])) {
  82. $message['body'][] = $signature;
  83. }
  84. else {
  85. // Some modules use the body as a string, erroneously.
  86. $message['body'] .= $signature;
  87. }
  88. }
  89. /**
  90. * @} End of "defgroup email_example".
  91. */