2007-06-26 22:21:08 +00:00
|
|
|
<?php
|
|
|
|
|
2009-08-31 18:30:27 +00:00
|
|
|
/**
|
|
|
|
* @file
|
2014-06-09 21:47:53 +00:00
|
|
|
* API functions for processing and sending email.
|
2009-08-31 18:30:27 +00:00
|
|
|
*/
|
|
|
|
|
2014-08-11 14:25:38 +00:00
|
|
|
use Drupal\Core\Mail\MailFormatHelper;
|
2014-02-16 19:15:45 +00:00
|
|
|
|
2007-06-26 22:21:08 +00:00
|
|
|
/**
|
2014-06-09 21:47:53 +00:00
|
|
|
* Composes and optionally sends an email message.
|
2007-07-01 19:49:19 +00:00
|
|
|
*
|
2014-06-09 21:47:53 +00:00
|
|
|
* Sending an email works with defining an email template (subject, text
|
|
|
|
* and possibly email headers) and the replacement values to use in the
|
|
|
|
* appropriate places in the template. Processed email templates are
|
|
|
|
* requested from hook_mail() from the module sending the email. Any module
|
|
|
|
* can modify the composed email message array using hook_mail_alter().
|
|
|
|
* Finally drupal_mail_system()->mail() sends the email, which can
|
|
|
|
* be reused if the exact same composed email is to be sent to multiple
|
2009-09-01 17:40:28 +00:00
|
|
|
* recipients.
|
2007-07-01 19:49:19 +00:00
|
|
|
*
|
2014-06-09 21:47:53 +00:00
|
|
|
* Finding out what language to send the email with needs some consideration.
|
|
|
|
* If you send email to a user, her preferred language should be fine, so
|
2012-09-13 08:48:24 +00:00
|
|
|
* use user_preferred_langcode(). If you send email based on form values
|
2007-07-01 19:49:19 +00:00
|
|
|
* filled on the page, there are two additional choices if you are not
|
2014-06-09 21:47:53 +00:00
|
|
|
* sending the email to a user on the site. You can either use the language
|
2012-09-13 08:48:24 +00:00
|
|
|
* used to generate the page or the site default language. See
|
2014-06-09 21:47:53 +00:00
|
|
|
* language_default(). The former is good if sending email to the person
|
|
|
|
* filling the form, the later is good if you send email to an address
|
2012-09-13 08:48:24 +00:00
|
|
|
* previously set up (like contact addresses in a contact form).
|
2007-07-01 19:49:19 +00:00
|
|
|
*
|
|
|
|
* Taking care of always using the proper language is even more important
|
2014-06-09 21:47:53 +00:00
|
|
|
* when sending emails in a row to multiple users. Hook_mail() abstracts
|
2007-07-01 19:49:19 +00:00
|
|
|
* whether the mail text comes from an administrator setting or is
|
|
|
|
* static in the source code. It should also deal with common mail tokens,
|
2014-06-09 21:47:53 +00:00
|
|
|
* only receiving $params which are unique to the actual email at hand.
|
2007-07-01 19:49:19 +00:00
|
|
|
*
|
|
|
|
* An example:
|
|
|
|
*
|
|
|
|
* @code
|
|
|
|
* function example_notify($accounts) {
|
|
|
|
* foreach ($accounts as $account) {
|
|
|
|
* $params['account'] = $account;
|
|
|
|
* // example_mail() will be called based on the first drupal_mail() parameter.
|
2012-09-13 08:48:24 +00:00
|
|
|
* drupal_mail('example', 'notice', $account->mail, user_preferred_langcode($account), $params);
|
2007-07-01 19:49:19 +00:00
|
|
|
* }
|
|
|
|
* }
|
2007-07-02 14:41:37 +00:00
|
|
|
*
|
2007-07-01 19:49:19 +00:00
|
|
|
* function example_mail($key, &$message, $params) {
|
2010-07-28 02:19:56 +00:00
|
|
|
* $data['user'] = $params['account'];
|
2012-09-13 08:48:24 +00:00
|
|
|
* $options['langcode'] = $message['langcode'];
|
2010-07-28 02:19:56 +00:00
|
|
|
* user_mail_tokens($variables, $data, $options);
|
2007-07-01 19:49:19 +00:00
|
|
|
* switch($key) {
|
|
|
|
* case 'notice':
|
2011-11-30 03:13:51 +00:00
|
|
|
* // If the recipient can receive such notices by instant-message, do
|
|
|
|
* // not send by email.
|
|
|
|
* if (example_im_send($key, $message, $params)) {
|
|
|
|
* $message['send'] = FALSE;
|
|
|
|
* break;
|
|
|
|
* }
|
2012-09-13 08:48:24 +00:00
|
|
|
* $message['subject'] = t('Notification from !site', $variables, $options);
|
|
|
|
* $message['body'][] = t("Dear !username\n\nThere is new content available on the site.", $variables, $options);
|
2007-07-01 19:49:19 +00:00
|
|
|
* break;
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* @endcode
|
2007-06-26 22:21:08 +00:00
|
|
|
*
|
2011-11-30 03:13:51 +00:00
|
|
|
* Another example, which uses drupal_mail() to format a message for sending
|
|
|
|
* later:
|
|
|
|
*
|
|
|
|
* @code
|
|
|
|
* $params = array('current_conditions' => $data);
|
|
|
|
* $to = 'user@example.com';
|
2012-09-13 08:48:24 +00:00
|
|
|
* $message = drupal_mail('example', 'notice', $to, $langcode, $params, FALSE);
|
2011-11-30 03:13:51 +00:00
|
|
|
* // Only add to the spool if sending was not canceled.
|
|
|
|
* if ($message['send']) {
|
|
|
|
* example_spool_message($message);
|
|
|
|
* }
|
|
|
|
* @endcode
|
|
|
|
*
|
2013-02-14 15:46:05 +00:00
|
|
|
* @param string $module
|
2007-07-01 19:49:19 +00:00
|
|
|
* A module name to invoke hook_mail() on. The {$module}_mail() hook will be
|
|
|
|
* called to complete the $message structure which will already contain common
|
|
|
|
* defaults.
|
2013-02-14 15:46:05 +00:00
|
|
|
* @param string $key
|
2014-06-09 21:47:53 +00:00
|
|
|
* A key to identify the email sent. The final message ID for email altering
|
2007-07-01 19:49:19 +00:00
|
|
|
* will be {$module}_{$key}.
|
2013-02-14 15:46:05 +00:00
|
|
|
* @param string $to
|
2014-06-09 21:47:53 +00:00
|
|
|
* The email address or addresses where the message will be sent to. The
|
2012-12-15 02:42:57 +00:00
|
|
|
* formatting of this string will be validated with the
|
2014-06-09 21:47:53 +00:00
|
|
|
* @link http://php.net/manual/filter.filters.validate.php PHP email validation filter. @endlink
|
2012-12-15 02:42:57 +00:00
|
|
|
* Some examples are:
|
2009-08-31 18:30:27 +00:00
|
|
|
* - user@example.com
|
|
|
|
* - user@example.com, anotheruser@example.com
|
|
|
|
* - User <user@example.com>
|
|
|
|
* - User <user@example.com>, Another User <anotheruser@example.com>
|
2013-02-14 15:46:05 +00:00
|
|
|
* @param string $langcode
|
2014-06-09 21:47:53 +00:00
|
|
|
* Language code to use to compose the email.
|
2013-02-14 15:46:05 +00:00
|
|
|
* @param array $params
|
2014-06-09 21:47:53 +00:00
|
|
|
* (optional) Parameters to build the email.
|
2013-12-30 23:35:14 +00:00
|
|
|
* @param string|null $reply
|
2014-06-09 21:47:53 +00:00
|
|
|
* Optional email address to be used to answer.
|
2013-02-14 15:46:05 +00:00
|
|
|
* @param bool $send
|
2011-11-30 03:13:51 +00:00
|
|
|
* If TRUE, drupal_mail() will call drupal_mail_system()->mail() to deliver
|
|
|
|
* the message, and store the result in $message['result']. Modules
|
|
|
|
* implementing hook_mail_alter() may cancel sending by setting
|
|
|
|
* $message['send'] to FALSE.
|
2010-07-16 11:18:49 +00:00
|
|
|
*
|
2014-08-11 14:25:38 +00:00
|
|
|
* @return array
|
2007-07-01 19:49:19 +00:00
|
|
|
* The $message array structure containing all details of the
|
|
|
|
* message. If already sent ($send = TRUE), then the 'result' element
|
2014-06-09 21:47:53 +00:00
|
|
|
* will contain the success indicator of the email, failure being already
|
2007-08-26 08:00:49 +00:00
|
|
|
* written to the watchdog. (Success means nothing more than the message being
|
|
|
|
* accepted at php-level, which still doesn't guarantee it to be delivered.)
|
2014-08-11 14:25:38 +00:00
|
|
|
*
|
|
|
|
* @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0.
|
|
|
|
* Use \Drupal::service('plugin.manager.mail')->mail() in procedural code. In
|
|
|
|
* Object-Oriented code inject the 'plugin.manager.mail' service and use the
|
|
|
|
* ::mail() method.
|
2007-06-26 22:21:08 +00:00
|
|
|
*/
|
2013-12-30 23:35:14 +00:00
|
|
|
function drupal_mail($module, $key, $to, $langcode, $params = array(), $reply = NULL, $send = TRUE) {
|
2014-08-11 14:25:38 +00:00
|
|
|
return \Drupal::service('plugin.manager.mail')->mail($module, $key, $to, $langcode, $params, $reply, $send);
|
2007-07-01 19:49:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-02-27 11:07:19 +00:00
|
|
|
* Returns an instance of the mail plugin to use for a given message ID.
|
2009-08-31 18:30:27 +00:00
|
|
|
*
|
2013-08-24 08:52:50 +00:00
|
|
|
* @param string $module
|
2009-08-31 18:30:27 +00:00
|
|
|
* The module name which was used by drupal_mail() to invoke hook_mail().
|
2013-08-24 08:52:50 +00:00
|
|
|
* @param string $key
|
2014-06-09 21:47:53 +00:00
|
|
|
* A key to identify the email sent. The final message ID for the email
|
2009-08-31 18:30:27 +00:00
|
|
|
* alter hook in drupal_mail() would have been {$module}_{$key}.
|
2010-07-16 11:18:49 +00:00
|
|
|
*
|
2013-10-03 11:26:25 +00:00
|
|
|
* @return \Drupal\Core\Mail\MailInterface
|
2014-02-27 11:07:19 +00:00
|
|
|
* A mail plugin instance.
|
2013-03-07 10:48:27 +00:00
|
|
|
*
|
2014-02-27 11:07:19 +00:00
|
|
|
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
|
2013-08-24 08:52:50 +00:00
|
|
|
*
|
2014-02-27 11:07:19 +00:00
|
|
|
* @see \Drupal\Core\Mail\MailManager::getInstance()
|
2014-08-11 14:25:38 +00:00
|
|
|
*
|
|
|
|
* @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0.
|
|
|
|
* Use \Drupal::service('plugin.manager.mail')->getInstance() in procedural
|
|
|
|
* code. In Object-Oriented code inject the 'plugin.manager.mail' service and
|
|
|
|
* use the ::getInstance() method.
|
2007-07-01 19:49:19 +00:00
|
|
|
*/
|
2009-10-16 03:01:55 +00:00
|
|
|
function drupal_mail_system($module, $key) {
|
2014-02-27 11:07:19 +00:00
|
|
|
return \Drupal::service('plugin.manager.mail')->getInstance(array('module' => $module, 'key' => $key));
|
2009-08-31 18:30:27 +00:00
|
|
|
}
|
|
|
|
|
2007-06-26 22:21:08 +00:00
|
|
|
/**
|
2012-08-31 15:56:36 +00:00
|
|
|
* Performs format=flowed soft wrapping for mail (RFC 3676).
|
2007-06-26 22:21:08 +00:00
|
|
|
*
|
|
|
|
* We use delsp=yes wrapping, but only break non-spaced languages when
|
|
|
|
* absolutely necessary to avoid compatibility issues.
|
|
|
|
*
|
|
|
|
* We deliberately use LF rather than CRLF, see drupal_mail().
|
|
|
|
*
|
|
|
|
* @param $text
|
|
|
|
* The plain text to process.
|
|
|
|
* @param $indent (optional)
|
|
|
|
* A string to indent the text with. Only '>' characters are repeated on
|
|
|
|
* subsequent wrapped lines. Others are replaced by spaces.
|
2012-10-05 18:12:56 +00:00
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* The content of the email as a string with formatting applied.
|
2014-08-11 14:25:38 +00:00
|
|
|
*
|
|
|
|
* @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0.
|
|
|
|
* Use \Drupal\Core\Utility\Mail::wrapMail().
|
2007-06-26 22:21:08 +00:00
|
|
|
*/
|
|
|
|
function drupal_wrap_mail($text, $indent = '') {
|
2014-08-11 14:25:38 +00:00
|
|
|
return MailFormatHelper::wrapMail($text, $indent);
|
2007-06-26 22:21:08 +00:00
|
|
|
}
|