123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- <?php
- /**
- * @file
- * Rules actions for sending Mime-encoded emails.
- *
- * @addtogroup rules
- * @{
- */
- /**
- * Implements hook_rules_action_info().
- */
- function mimemail_rules_action_info() {
- return array(
- 'mimemail' => array(
- 'label' => t('Send HTML e-mail'),
- 'group' => t('System'),
- 'parameter' => array(
- 'key' => array(
- 'type' => 'text',
- 'label' => t('Key'),
- 'description' => t('A key to identify the e-mail sent.'),
- ),
- 'to' => array(
- 'type' => 'text',
- 'label' => t('To'),
- 'description' => t("The mail's recipient address. The formatting of this string must comply with RFC 2822."),
- ),
- 'cc' => array(
- 'type' => 'text',
- 'label' => t('CC Recipient'),
- 'description' => t("The mail's carbon copy address. You may separate multiple addresses with comma."),
- 'optional' => TRUE,
- ),
- 'bcc' => array(
- 'type' => 'text',
- 'label' => t('BCC Recipient'),
- 'description' => t("The mail's blind carbon copy address. You may separate multiple addresses with comma."),
- 'optional' => TRUE,
- ),
- 'from_name' => array(
- 'type' => 'text',
- 'label' => t('Sender name'),
- 'description' => t("The sender's name. Leave it empty to use the site-wide configured name."),
- 'optional' => TRUE,
- ),
- 'from_mail' => array(
- 'type' => 'text',
- 'label' => t('Sender e-mail address'),
- 'description' => t("The sender's address. Leave it empty to use the site-wide configured address."),
- 'optional' => TRUE,
- ),
- 'reply_to' => array(
- 'type' => 'text',
- 'label' => t('Reply e-mail address'),
- 'description' => t("The address to reply to. Leave it empty to use the sender's address."),
- 'optional' => TRUE,
- ),
- 'list_unsubscribe' => array(
- 'type' => 'text',
- 'label' => t('Unsubscription e-mail and/or URL'),
- 'description' => t("An e-mail address and/or a URL which can be used for unsubscription. Values must be enclosed by angel brackets and separated by a comma."),
- 'optional' => TRUE,
- ),
- 'subject' => array(
- 'type' => 'text',
- 'label' => t('Subject'),
- 'description' => t("The mail's subject."),
- 'translatable' => TRUE,
- ),
- 'body' => array(
- 'type' => 'text',
- 'label' => t('Body'),
- 'description' => t('The mail\'s HTML body. Will be formatted using the text format selected on the <a href="@url">settings</a> page.', array('@url' => url('admin/config/system/mimemail'))),
- 'sanitize' => TRUE,
- 'optional' => TRUE,
- 'translatable' => TRUE,
- ),
- 'plaintext' => array(
- 'type' => 'text',
- 'label' => t('Plain text body'),
- 'description' => t("The mail's plaintext body."),
- 'optional' => TRUE,
- 'translatable' => TRUE,
- ),
- 'attachments' => array(
- 'type' => 'text',
- 'label' => t('Attachments'),
- 'description' => t("The mail's attachments, one file per line e.g. \"files/images/mypic.png\" without quotes."),
- 'optional' => TRUE,
- ),
- 'language' => array(
- 'type' => 'token',
- 'label' => t('Language'),
- 'description' => t('If specified, the language used for getting the mail message and subject.'),
- 'options list' => 'entity_metadata_language_list',
- 'optional' => TRUE,
- 'default value' => LANGUAGE_NONE,
- 'default mode' => 'selector',
- ),
- ),
- 'base' => 'rules_action_mimemail',
- 'access callback' => 'rules_system_integration_access',
- ),
- 'mimemail_to_users_of_role' => array(
- 'label' => t('Send HTML mail to all users of a role'),
- 'group' => t('System'),
- 'parameter' => array(
- 'key' => array(
- 'type' => 'text',
- 'label' => t('Key'),
- 'description' => t('A key to identify the e-mail sent.'),
- ),
- 'roles' => array(
- 'type' => 'list<integer>',
- 'label' => t('Roles'),
- 'options list' => 'entity_metadata_user_roles',
- 'description' => t('Select the roles whose users should receive the mail.'),
- ),
- 'active' => array(
- 'type' => 'boolean',
- 'label' =>('Send to active users'),
- 'description' => t('Send mail only to active users.'),
- ),
- 'from_name' => array(
- 'type' => 'text',
- 'label' => t('Sender name'),
- 'description' => t("The sender's name. Leave it empty to use the site-wide configured name."),
- 'optional' => TRUE,
- ),
- 'from_mail' => array(
- 'type' => 'text',
- 'label' => t('Sender e-mail address'),
- 'description' => t("The sender's address. Leave it empty to use the site-wide configured address."),
- 'optional' => TRUE,
- ),
- 'subject' => array(
- 'type' => 'text',
- 'label' => t('Subject'),
- 'description' => t("The mail's subject."),
- 'translatable' => TRUE,
- ),
- 'body' => array(
- 'type' => 'text',
- 'label' => t('Body'),
- 'description' => t("The mail's message HTML body."),
- 'optional' => TRUE,
- 'translatable' => TRUE,
- ),
- 'plaintext' => array(
- 'type' => 'text',
- 'label' => t('Plaintext body'),
- 'description' => t("The mail's message plaintext body."),
- 'optional' => TRUE,
- 'translatable' => TRUE,
- ),
- 'attachments' => array(
- 'type' => 'text',
- 'label' => t('Attachments'),
- 'description' => t("The mail's attachments, one file per line e.g. \"files/images/mypic.png\" without quotes."),
- 'optional' => TRUE,
- ),
- 'language_user' => array(
- 'type' => 'boolean',
- 'label' => t("Send mail in each recipient's language"),
- 'description' => t("If checked, the mail message and subject will be sent in each user's preferred language. <strong>You can safely leave the language selector below empty if this option is selected.</strong>"),
- ),
- 'language' => array(
- 'type' => 'token',
- 'label' => t('Fixed language'),
- 'description' => t('If specified, the fixed language used for getting the mail message and subject.'),
- 'options list' => 'entity_metadata_language_list',
- 'optional' => TRUE,
- 'default value' => LANGUAGE_NONE,
- 'default mode' => 'selector',
- ),
- ),
- 'base' => 'rules_action_mimemail_to_users_of_role',
- 'access callback' => 'rules_system_integration_access',
- ),
- );
- }
- /**
- * Implements hook_rules_action_base_upgrade_map_name().
- */
- function mimemail_rules_action_mail_upgrade_map_name($element) {
- return 'mimemail';
- }
- /**
- * Implements hook_rules_action_base_upgrade_map_name().
- */
- function mimemail_rules_action_mail_to_user_upgrade_map_name($element) {
- return 'mimemail';
- }
- /**
- * Implements hook_rules_action_base_upgrade_map_name().
- */
- function mimemail_rules_action_mail_to_users_of_role_upgrade_map_name($element) {
- return 'mimemail_to_users_of_role';
- }
- /**
- * Implements hook_rules_action_base_upgrade().
- */
- function mimemail_rules_action_mail_upgrade($element, RulesPlugin $target) {
- $target->settings['key'] = $element['#settings']['key'];
- $target->settings['from_name'] = $element['#settings']['sender'];
- $target->settings['from_mail'] = $element['#settings']['from'];
- $target->settings['body'] = $element['#settings']['message_html'];
- $target->settings['plaintext'] = $element['#settings']['message_plaintext'];
- }
- /**
- * Implements hook_rules_action_base_upgrade().
- */
- function mimemail_rules_action_mail_to_user_upgrade($element, RulesPlugin $target) {
- switch ($element['#settings']['#argument map']['user']) {
- case 'author':
- $token = 'node:author';
- break;
- case 'author_unchanged':
- $token = 'node-unchanged:author';
- break;
- case 'user':
- $token = 'site:current-user';
- break;
- }
- $target->settings['to:select'] = $token . ':mail';
- mimemail_rules_action_mail_upgrade($element, $target);
- }
- /**
- * Implements hook_rules_action_base_upgrade().
- */
- function mimemail_rules_action_mail_to_users_of_role_upgrade($element, RulesPlugin $target) {
- $target->settings['roles'] = $element['#settings']['recipients'];
- mimemail_rules_action_mail_upgrade($element, $target);
- }
- /**
- * Action Implementation: Send HTML mail.
- */
- function rules_action_mimemail($key, $to, $cc = NULL, $bcc = NULL, $from_name = NULL, $from_mail = NULL, $reply_to = NULL, $list_unsubscribe = NULL, $subject, $body, $plaintext = NULL, $attachments = array(), $langcode, $settings, RulesState $state, RulesPlugin $element) {
- module_load_include('inc', 'mimemail');
- // Set the sender name and from address.
- if (empty($from_mail)) {
- $from = NULL;
- }
- else {
- $from = array(
- 'name' => $from_name,
- 'mail' => $from_mail,
- );
- // Create an address string.
- $from = mimemail_address($from);
- }
- // Figure out the language to use - fallback is the system default.
- $languages = language_list();
- $language = isset($languages[$langcode]) ? $languages[$langcode] : language_default();
- $params = array(
- 'context' => array(
- 'subject' => $subject,
- 'body' => $body,
- 'action' => $element,
- 'state' => $state,
- ),
- 'cc' => $cc,
- 'bcc' => $bcc,
- 'reply-to' => $reply_to,
- 'list-unsubscribe' => $list_unsubscribe,
- 'plaintext' => $plaintext,
- 'attachments' => $attachments,
- );
- drupal_mail('mimemail', $key, $to, $language, $params, $from);
- }
- /**
- * Action: Send HTML mail to all users of a specific role group(s).
- */
- function rules_action_mimemail_to_users_of_role($key, $roles, $active, $from_name = NULL, $from_mail = NULL, $subject, $body, $plaintext = NULL, $attachments = array(), $use_userlang = FALSE, $langcode= NULL, $settings, RulesState $state, RulesPlugin $element) {
- module_load_include('inc', 'mimemail');
- // Set the sender name and from address.
- if (empty($from_mail)) {
- $from = NULL;
- }
- else {
- $from = array(
- 'name' => $from_name,
- 'mail' => $from_mail,
- );
- // Create an address string.
- $from = mimemail_address($from);
- }
- $query = db_select('users', 'u');
- $query->fields('u', array('mail', 'language'));
- if ($active) {
- $query->condition('u.status', 1, '=');
- }
- if (in_array(DRUPAL_AUTHENTICATED_RID, $roles)) {
- $query->condition('u.uid', 0, '>');
- }
- else {
- $query->join('users_roles', 'r', 'u.uid = r.uid');
- $query->condition('r.rid', $roles, 'IN');
- $query->distinct();
- }
- $result = $query->execute();
- $params = array(
- 'context' => array(
- 'subject' => $subject,
- 'body' => $body,
- 'action' => $element,
- 'state' => $state,
- ),
- 'plaintext' => $plaintext,
- 'attachments' => $attachments,
- );
- // Create language list before initializing foreach.
- $languages = language_list();
- $message = array('result' => TRUE);
- foreach ($result as $row) {
- // Decide which language to use.
- if (!$use_userlang || empty($row->language) || !isset($languages[$row->language])) {
- $language = isset($languages[$langcode]) ? $languages[$langcode] : language_default();
- }
- else {
- $language = $languages[$row->language];
- }
- $message = drupal_mail('mimemail', $key, $row->mail, $language, $params, $from);
- if (!$message['result']) {
- break;
- }
- }
- if ($message['result']) {
- $role_names = array_intersect_key(user_roles(TRUE), array_flip($roles));
- watchdog('rules', 'Successfully sent HTML email to the role(s) %roles.', array('%roles' => implode(', ', $role_names)));
- }
- }
- /**
- * @}
- */
|