diff --git includes/mimemail.rules.inc includes/mimemail.rules.inc new file mode 100644 index 0000000..d7a1cf6 --- /dev/null +++ includes/mimemail.rules.inc @@ -0,0 +1,90 @@ + array( + 'label' => t('Send an HTML mail to a user'), + 'arguments' => array( + 'user' => array('type' => 'user', 'label' => t('Recipient')), + ), + 'module' => 'Mime Mail', + 'eval input' => array('subject', 'message', 'from'), + ), + 'mimemail_rules_action_mail' => array( + 'label' => t('Send an HTML mail to an arbitrary mail address'), + 'module' => 'Mime Mail', + 'eval input' => array('subject', 'message', 'from', 'to'), + ), + 'mimemail_rules_action_mail_to_users_of_role' => array( + 'label' => t('Send an HTML mail to all users of a role'), + 'module' => 'Mime Mail', + 'eval input' => array('subject', 'message', 'from'), + ), + ); +} + +/** + * Action Implementation: Send a mail to a user. + */ +function mimemail_rules_action_mail_to_user($user, $settings) { + // We also handle $settings['to'] if it's set. + $to = empty($settings['to']) ? array($user->mail) : $settings['to']; + + $status = mimemail($settings['from'], $to, $settings['subject'], + $settings['message_html'], NULL, array(), $settings['message_plaintext'], + $settings['attachments'] + ); + if (!empty($status)) { + watchdog('rules', 'Successfully sent HTML email to %recipient', array( + '%recipient' => implode(', ', $to), + )); + } +} + +/** + * Action Implementation: rules_action_mail + * This action makes use of the rules_action_mail_to_user action implementation. + */ +function mimemail_rules_action_mail($settings) { + mimemail_rules_action_mail_to_user(NULL, $settings); +} + +/** + * Action: Send mail to all users of a specific role group(s). + */ +function mimemail_rules_action_mail_to_users_of_role($settings) { + $recipients = $settings['recipients']; + + // All authenticated users, which is everybody. + if (in_array(DRUPAL_AUTHENTICATED_RID, $recipients)) { + $result = db_query('SELECT mail FROM {users} WHERE uid > 0'); + } + else { + $rids = implode(',', $recipients); + // Avoid sending emails to members of two or more target role groups. + $result = db_query('SELECT DISTINCT u.mail FROM {users} u INNER JOIN {users_roles} r ON u.uid = r.uid WHERE r.rid IN ('. $rids .')'); + } + + // Now, actually send the mails. + $status = TRUE; + while (($account = db_fetch_object($result)) && !empty($status)) { + $status = mimemail($settings['from'], $account->mail, $settings['subject'], + $settings['message_html'], NULL, array(), $settings['message_plaintext'], + $settings['attachments'] + ); + } + if (!empty($status)) { + $roles = array_intersect_key(user_roles(TRUE), drupal_map_assoc($recipients)); + watchdog('rules', 'Successfully sent HTML email to the role(s) %roles.', array('%roles' => implode(', ', $roles))); + } +} diff --git includes/mimemail.rules_forms.inc includes/mimemail.rules_forms.inc new file mode 100644 index 0000000..0636b99 --- /dev/null +++ includes/mimemail.rules_forms.inc @@ -0,0 +1,127 @@ + '', 'subject' => '', 'message' => ''); + $form['settings']['from'] = array( + '#type' => 'textfield', + '#title' => t('Sender'), + '#default_value' => isset($settings['from']) ? $settings['from'] : '', + '#description' => t("The mail's from address. Leave it empty to use the site-wide configured address."), + ); + $form['settings']['subject'] = array( + '#type' => 'textfield', + '#title' => t('Subject'), + '#default_value' => isset($settings['subject']) ? $settings['subject'] : '', + '#description' => t("The mail's subject."), + ); + $form['settings']['message_html'] = array( + '#type' => 'textarea', + '#title' => t('HTML message'), + '#default_value' => isset($settings['message_html']) + ? $settings['message_html'] + : "\n\n

\n\n\n", + '#description' => t("The message body in HTML format."), + ); + $form['settings']['message_plaintext'] = array( + '#type' => 'textarea', + '#title' => t('Text message'), + '#default_value' => isset($settings['message_plaintext']) ? $settings['message_plaintext'] : '', + '#description' => t("Optional plaintext portion of a multipart e-mail."), + ); + + $attachments_string = ''; + if (isset($settings['attachments'])) { + foreach ($settings['attachments'] as $attachment) { + $attachments_string .= $attachment['filemime'] . ':' . $attachment['filepath'] . "\n"; + } + } + $form['settings']['attachments'] = array( + '#type' => 'textarea', + '#title' => t('Attachments'), + '#default_value' => trim($attachments_string), + '#description' => t('A list of attachments, one file per line. The format of each line is "[mimetype]:[path]", e.g. "image/png:files/images/mypic.png"'), + ); +} + +function mimemail_rules_action_mail_to_user_submit(&$settings, $form, $form_state) { + $settings['from'] = empty($settings['from']) + ? NULL + : str_replace(array("\r", "\n"), '', $settings['from']); + + $settings['subject'] = str_replace(array("\r", "\n"), '', $settings['subject']); + + if (empty($settings['message_plaintext'])) { + $settings['message_plaintext'] = NULL; + } + + $attachments = array(); + $attachments_string = trim($settings['attachments']); + if (!empty($attachments_string)) { + $attachment_lines = array_filter(explode("\n", trim($attachments_string))); + foreach ($attachment_lines as $key => $attachment_line) { + $attachment = explode(":", trim($attachment_line), 2); + $attachments[] = array( + 'filepath' => $attachment[1], + 'filemime' => $attachment[0], + ); + } + } + $settings['attachments'] = $attachments; +} + +/** + * Action "Send an HTML mail to an arbitrary mail address" configuration form + */ +function mimemail_rules_action_mail_form($settings = array(), &$form) { + $form['settings']['to'] = array( + '#type' => 'textfield', + '#title' => t('Recipient'), + '#default_value' => isset($settings['to']) ? implode(', ', $settings['to']) : '', + '#description' => t("The mail's recipient address. You may separate multiple addresses with ','."), + '#required' => TRUE, + '#weight' => -1, + ); + mimemail_rules_action_mail_to_user_form($settings, $form); +} + +function mimemail_rules_action_mail_submit(&$settings, $form, $form_state) { + $settings['to'] = empty($settings['to']) ? array() : explode(',', $settings['to']); + foreach ($settings['to'] as $key => $address) { + $settings['to'][$key] = str_replace(array("\r", "\n"), '', trim($address)); + } + mimemail_rules_action_mail_to_user_submit($settings, $form, $form_state); +} + +/** + * Action "Send an HTML mail to users of a role" configuration form + */ +function mimemail_rules_action_mail_to_users_of_role_form($settings = array(), &$form) { + // Select only non-anonymous user roles because anonymous users won't have emails. + $roles = user_roles(TRUE); + + $form['settings']['recipients'] = array( + '#type' => 'checkboxes', + '#title' => t('Recipient roles'), + '#prefix' => t('WARNING: This may cause problems if there are too many users of these roles on your site, as your server may not be able to handle all the mail requests all at once.'), + '#required' => TRUE, + '#default_value' => isset($settings['recipients']) ? $settings['recipients'] : array(), + '#options' => $roles, + '#description' => t('Select the roles whose users should receive this email.'), + ); + mimemail_rules_action_mail_to_user_form($settings, $form); +} + +function mimemail_rules_action_mail_to_users_of_role_submit(&$settings, $form, $form_state) { + $settings['recipients'] = array_filter(array_keys(array_filter($settings['recipients']))); + mimemail_rules_action_mail_to_user_submit($settings, $form, $form_state); +}