Index: includes/mimemail.rules_forms.inc =================================================================== RCS file: includes/mimemail.rules_forms.inc diff -N includes/mimemail.rules_forms.inc --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ includes/mimemail.rules_forms.inc 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,102 @@ + '', 'subject' => '', 'message' => '', 'message_html' => "\n\n

\n\n\n", 'message_plaintext' => '', 'attachments' => ''); + $form['settings']['from'] = array( + '#type' => 'textfield', + '#title' => t('Sender'), + '#default_value' => $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' => $settings['subject'], + '#description' => t("The mail's subject."), + ); + $form['settings']['message_html'] = array( + '#type' => 'textarea', + '#title' => t('HTML message'), + '#default_value' => $settings['message_html'], + '#description' => t("The message body in HTML format."), + ); + $form['settings']['message_plaintext'] = array( + '#type' => 'textarea', + '#title' => t('Text message'), + '#default_value' => $settings['message_plaintext'], + '#description' => t("Optional plaintext portion of a multipart e-mail."), + ); + $form['settings']['attachments'] = array( + '#type' => 'textarea', + '#title' => t('Attachments'), + '#default_value' => $settings['attachments'], + '#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"'), + ); +} + +/** + * 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']) ? $settings['to'] : '', + '#description' => t("The mail's recipient address. You may separate multiple addresses with ','."), + '#required' => TRUE, + '#weight' => -1, + ); + $form['settings']['cc'] = array( + '#type' => 'textfield', + '#title' => t('CC Recipient'), + '#default_value' => isset($settings['cc']) ? $settings['cc'] : '', + '#description' => t("The mail's carbon copy address. You may separate multiple addresses with ','."), + '#required' => FALSE, + '#weight' => 0, + ); + $form['settings']['bc'] = array( + '#type' => 'textfield', + '#title' => t('BCC Recipient'), + '#default_value' => isset($settings['bc']) ? $settings['bc'] : '', + '#description' => t("The mail's blind carbon copy address. You may separate multiple addresses with ','."), + '#required' => FALSE, + '#weight' => 0, + ); + mimemail_rules_action_mail_to_user_form($settings, $form); +} + +/** + * 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 = array_map('filter_xss_admin', 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); +} + +/** + * @} + */ Index: includes/mimemail.rules.inc =================================================================== RCS file: includes/mimemail.rules.inc diff -N includes/mimemail.rules.inc --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ includes/mimemail.rules.inc 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,156 @@ + 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('from', 'to', 'cc', 'bc', 'subject', 'message_html', 'message_plaintext', 'attachments'), + ), + 'mimemail_rules_action_mail' => array( + 'label' => t('Send an HTML mail to an arbitrary mail address'), + 'module' => 'Mime Mail', + 'eval input' => array('from', 'to', 'cc', 'bc', 'subject', 'message_html', 'message_plaintext', 'attachments'), + ), + '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('from', 'to', 'cc', 'bc', 'subject', 'message_html', 'message_plaintext', 'attachments'), + ), + ); +} + +/** + * Action Implementation: Send a mail to a user and to an arbitrary mail address. + */ +function mimemail_rules_action_mail_to_user($user, $settings) { + //if $user is empty set acting user + if (empty($user)) { + global $user; + } + + //validate settings + $settings = mimemail_rules_form_settings_validate($settings); + + //if recipient field is empty send it to given user object. if not it's the acting user. + $to = empty($settings['to']) ? $user->mail : implode(',', $settings['to']); + + $headers = array( + 'Bcc' => isset($settings['bc']) ? implode(',', $settings['bc']) : '', + 'Cc' => isset($settings['cc']) ? implode(',', $settings['cc']) : '', + ); + + $status = mimemail($settings['from'], $to, $settings['subject'], + $settings['message_html'], NULL, $headers, $settings['message_plaintext'], + $settings['attachments'] + ); + + if (!empty($status)) { + $all_recipients = array_merge(explode(',', $to), $settings['bc'], $settings['cc']); + watchdog('rules', 'Successfully sent HTML email to %recipient', array( + '%recipient' => implode(', ', $all_recipients), + )); + } +} + +/** + * 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 = array_filter(array_keys(array_filter($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 .')'); + } + + //validate settings + $settings = mimemail_rules_form_settings_validate($settings); + + // 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))); + } +} + +/* + * Helper function for validating form settings on execution time, so that tokens/input evaluators apply + */ +function mimemail_rules_form_settings_validate($settings) { + // We also handle $settings['to'] and co if it's set. + $settings['to'] = empty($settings['to']) ? array() : explode(',', $settings['to']); + $settings['cc'] = empty($settings['cc']) ? array() : explode(',', $settings['cc']); + $settings['bc'] = empty($settings['bc']) ? array() : explode(',', $settings['bc']); + + foreach ($settings['to'] as $key => $address) { + $settings['to'][$key] = str_replace(array("\r", "\n"), '', trim($address)); + } + foreach ($settings['cc'] as $key => $address) { + $settings['cc'][$key] = str_replace(array("\r", "\n"), '', trim($address)); + } + foreach ($settings['bc'] as $key => $address) { + $settings['bc'][$key] = str_replace(array("\r", "\n"), '', trim($address)); + } + + $settings['from'] = str_replace(array("\r", "\n"), '', $settings['from']); + $settings['subject'] = str_replace(array("\r", "\n"), '', $settings['subject']); + $settings['message_html'] = str_replace(array("\r", "\n"), '', $settings['message_html']); + $settings['message_plaintext'] = str_replace(array("\r", "\n"), '', $settings['message_plaintext']); + + $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'] = empty($attachments[0]['filepath']) ? array() : $attachments; + + return $settings; +} + +/** + * @} + */