Is it possible to let the user opt out to receive emails? I got a rule that send out emails to every user of a roll, but i still wanna allow ppl to opt out to get automated emails.

Comments

Les Lim’s picture

Component: Rules Core » Rules Engine
Status: Active » Fixed

Create an additional role called "Opt out" or something similar. Then add another "User has role" condition to your rule, set to the "Opt Out" role and "Negate".

Anonymous’s picture

good idea, will do that way. thanks.
can also use a user flag with an flag action->role change for a nice UI for the user.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

jvieille’s picture

Component: Rules Engine » Rules Core
Status: Closed (fixed) » Active

The proposed solution does not make sense to me.
A condition will check either the acting user or the content user, never the users who will be targeted by the "send email" action.

Is there any means to prevent a user of receiving emails blindly sent by a rules' action?

jvieille’s picture

Status: Active » Needs review

Below a solution : add excluded roles selection so people can be opted out by granting them a role like "OptOut"
(not a patch, sorry)

in mimemail.rules_forms.inc

**
 * 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('<strong>WARNING!</strong> 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 mail.'),
  );
+  $rolesless = array_map('filter_xss_admin', user_roles(TRUE));
+  $form['settings']['excluded'] = array(
+    '#type' => 'checkboxes',
+    '#title' => t('Excluded roles'),
+    '#required' => FALSE,
+    '#default_value' => isset($settings['excluded']) ? $settings['excluded'] : array(),
+    '#options' => $rolesless,
+    '#description' => t('Select the roles whose users should not receive this mail.'),
+  );

  mimemail_rules_action_mail_to_user_form($settings, $form);
}

mimemail.rules.inc

/**
 * Implements action to 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 .')');
  }

  // Set the sender name and from address.
  if (empty($settings['from'])) {
    $from = NULL;
  }
  else {
    $from = array(
      'name' => $settings['sender'],
      'mail' => $settings['from'],
    );
  }

+  $excluded = array_filter(array_keys(array_filter($settings['excluded'])));
+
+  // All authenticated users, which is everybody.
+  if (in_array(DRUPAL_AUTHENTICATED_RID, $excluded)) {
+    $resultexcluded = db_query('SELECT mail FROM {users} WHERE uid > 0');
+  }
+  else {
+    $rids = implode(',', $excluded);
+    // Avoid sending emails to members of two or more target role groups.
+    $resultexcluded = db_query('SELECT DISTINCT u.mail FROM {users} u INNER JOIN {users_roles} r ON u.uid = r.uid WHERE r.rid IN ('. $rids .')');
+  }
+  while (($account = db_fetch_object($resultexcluded))) {
+    $emailstoexclude[] = $account->mail;
+  }
+  
  while (($account = db_fetch_object($result))) {
    // Prepare the message but don't send.

+    if (!in_array($account->mail, $emailstoexclude)){
         $message = drupal_mail('mimemail', 'rules_action_role_mail',  $account->mail, user_preferred_language($account), $settings, $from, FALSE);
         // Send the prepared message.
         $message = mimemail(
            $message['from'],
            $message['to'],
            $message['subject'],
            $message['body'],
            NULL ,
            $message['headers'],
            $message['params']['plaintext'],
            $message['params']['attachments'],
            $message['id']
          );
+     }
  }
  
  if ($message['result']) {
    $roles = array_intersect_key(user_roles(TRUE), drupal_map_assoc($recipients));
    watchdog('rules', 'HTML mail successfully sent to role(s) %roles.', array('%roles' => implode(', ', $roles)));
  }
}
TR’s picture

Issue summary: View changes
Status: Needs review » Fixed

Several solutions to this support request were provided above.

TR’s picture

Status: Fixed » Closed (fixed)