MailManagerInterface.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace Drupal\Core\Mail;
  3. use Drupal\Component\Plugin\PluginManagerInterface;
  4. /**
  5. * Provides an interface for sending mail.
  6. */
  7. interface MailManagerInterface extends PluginManagerInterface {
  8. /**
  9. * Composes and optionally sends an email message.
  10. *
  11. * Sending an email works with defining an email template (subject, text and
  12. * possibly email headers) and the replacement values to use in the
  13. * appropriate places in the template. Processed email templates are requested
  14. * from hook_mail() from the module sending the email. Any module can modify
  15. * the composed email message array using hook_mail_alter(). Finally
  16. * \Drupal::service('plugin.manager.mail')->mail() sends the email, which can
  17. * be reused if the exact same composed email is to be sent to multiple
  18. * recipients.
  19. *
  20. * Finding out what language to send the email with needs some consideration.
  21. * If you send email to a user, her preferred language should be fine, so use
  22. * \Drupal\Core\Session\AccountInterface::getPreferredAdminLangcode(). If you
  23. * send email based on form values filled on the page, there are two
  24. * additional choices if you are not sending the email to a user on the site.
  25. * You can either use the language used to generate the page or the site
  26. * default language. See
  27. * Drupal\Core\Language\LanguageManagerInterface::getDefaultLanguage(). The
  28. * former is good if sending email to the person filling the form, the later
  29. * is good if you send email to an address previously set up (like contact
  30. * addresses in a contact form).
  31. *
  32. * Taking care of always using the proper language is even more important when
  33. * sending emails in a row to multiple users. Hook_mail() abstracts whether
  34. * the mail text comes from an administrator setting or is static in the
  35. * source code. It should also deal with common mail tokens, only receiving
  36. * $params which are unique to the actual email at hand.
  37. *
  38. * An example:
  39. *
  40. * @code
  41. * function example_notify($accounts) {
  42. * foreach ($accounts as $account) {
  43. * $params['account'] = $account;
  44. * // example_mail() will be called based on the first
  45. * // MailManagerInterface->mail() parameter.
  46. * \Drupal::service('plugin.manager.mail')->mail('example', 'notice', $account->mail, $langcode, $params);
  47. * }
  48. * }
  49. *
  50. * function example_mail($key, &$message, $params) {
  51. * $data['user'] = $params['account'];
  52. * $options['langcode'] = $message['langcode'];
  53. * user_mail_tokens($variables, $data, $options);
  54. * switch($key) {
  55. * case 'notice':
  56. * // If the recipient can receive such notices by instant-message, do
  57. * // not send by email.
  58. * if (example_im_send($key, $message, $params)) {
  59. * $message['send'] = FALSE;
  60. * break;
  61. * }
  62. * $message['subject'] = t('Notification from @site', $variables, $options);
  63. * $message['body'][] = t("Dear @username\n\nThere is new content available on the site.", $variables, $options);
  64. * break;
  65. * }
  66. * }
  67. * @endcode
  68. *
  69. * Another example, which uses MailManagerInterface->mail() to format a
  70. * message for sending later:
  71. *
  72. * @code
  73. * $params = array('current_conditions' => $data);
  74. * $to = 'user@example.com';
  75. * $message = \Drupal::service('plugin.manager.mail')->mail('example', 'notice', $to, $langcode, $params, FALSE);
  76. * // Only add to the spool if sending was not canceled.
  77. * if ($message['send']) {
  78. * example_spool_message($message);
  79. * }
  80. * @endcode
  81. *
  82. * @param string $module
  83. * A module name to invoke hook_mail() on. The {$module}_mail() hook will be
  84. * called to complete the $message structure which will already contain
  85. * common defaults.
  86. * @param string $key
  87. * A key to identify the email sent. The final message ID for email altering
  88. * will be {$module}_{$key}.
  89. * @param string $to
  90. * The email address or addresses where the message will be sent to. The
  91. * formatting of this string will be validated with the
  92. * @link http://php.net/manual/filter.filters.validate.php PHP email validation filter. @endlink
  93. * Some examples are:
  94. * - user@example.com
  95. * - user@example.com, anotheruser@example.com
  96. * - User <user@example.com>
  97. * - User <user@example.com>, Another User <anotheruser@example.com>
  98. * @param string $langcode
  99. * Language code to use to compose the email.
  100. * @param array $params
  101. * (optional) Parameters to build the email.
  102. * @param string|null $reply
  103. * Optional email address to be used to answer.
  104. * @param bool $send
  105. * If TRUE, call an implementation of
  106. * \Drupal\Core\Mail\MailInterface->mail() to deliver the message, and
  107. * store the result in $message['result']. Modules implementing
  108. * hook_mail_alter() may cancel sending by setting $message['send'] to
  109. * FALSE.
  110. *
  111. * @return array
  112. * The $message array structure containing all details of the message. If
  113. * already sent ($send = TRUE), then the 'result' element will contain the
  114. * success indicator of the email, failure being already written to the
  115. * watchdog. (Success means nothing more than the message being accepted at
  116. * php-level, which still doesn't guarantee it to be delivered.)
  117. */
  118. public function mail($module, $key, $to, $langcode, $params = [], $reply = NULL, $send = TRUE);
  119. }