Index: actions.inc =================================================================== RCS file: /cvs/drupal/contributions/modules/actions/actions.inc,v retrieving revision 1.8 diff -u -F^f -r1.8 actions.inc --- actions.inc 28 Dec 2005 03:09:52 -0000 1.8 +++ actions.inc 15 May 2006 14:52:41 -0000 @@ -359,21 +359,18 @@ function action_send_email($op, $edit = ); case 'do': - // note this is the user who owns the node, not global $user - $user = user_load(array('uid' => $node->uid)); - $site_name = variable_get('site_name', 'Drupal'); + // this is the user who owns the node + $account = user_load(array('uid' => $node->uid)); + $site_name = variable_get('site_name', 'drupal'); $from = "$site_name <" . variable_get('site_mail', ini_get('sendmail_from')) . '>'; - $subject = $edit['subject']; - $message = $edit['message']; - if ($edit['recipient'] == t('%author')) { - $recipient = $user->mail; - } else { - $recipient = $edit['recipient']; - } + $header = "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from"; + $subject = $edit['message']['subject']; + $message = $edit['message']['message']; + $recipients = _action_send_email_get_mail_addresses($edit, $node); if (isset($node) && is_object($node)) { $variables = array( '%site_name' => $site_name, - '%username' => $user->name, + '%username' => $account->name, '%uid' => $node->uid, '%node_url' => url('node/' . $node->nid, NULL, NULL, TRUE), '%node_type' => $node->type, @@ -381,49 +378,81 @@ function action_send_email($op, $edit = '%teaser' => strip_tags($node->teaser), '%body' => strip_tags($node->body) ); - $message = strtr($message, $variables); } - if (user_mail($recipient, $subject, $message, "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from" )) { - watchdog('action', t('Sent email to %recipient', array('%recipient' => $recipient))); - } - else { - watchdog('error', t('Unable to send email to %recipient', array('%recipient' => $recipient))); + + watchdog('action', t('Sending mails...')); + foreach($recipients as $recipient => $blank) { + if (!user_mail($recipient, $subject, $message, $header)) { + watchdog('error', t('Unable to send email to %recipient', array('%recipient' => $recipient))); + } } break; // return an HTML config form for the action case 'form': // default values for form - if (!isset($edit['recipient'])) $edit['recipient'] = ''; - if (!isset($edit['subject'])) $edit['subject'] = ''; - if (!isset($edit['message'])) $edit['message'] = ''; + if (!isset($edit['recipients']['raw_mail_addresses'])) $edit['recipients']['raw_mail_addresses'] = array(); + if (!isset($edit['recipients']['users'])) $edit['recipients']['users'] = array(); + if (!isset($edit['message']['subject'])) $edit['message']['subject'] = ''; + if (!isset($edit['message']['message'])) $edit['message']['message'] = ''; $form = array(); // add form components - $form['recipient'] = array( + $form['recipients'] = array( + '#type' => 'fieldset', + '#title' => t('Recipients'), + '#collapsible' => true, + '#collapsed' => true, + ); + $form['recipients']['roles'] = array( + '#type' => 'checkboxes', + '#title' => t('Roles'), + '#default_value' => $edit['recipients']['roles'], + '#options' => user_roles(true), + '#description' => t('All users of the checked roles get this email.'), + ); + $form['recipients']['users'] = array( '#type' => 'textfield', - '#title' => t('Recipient'), - '#default_value' => $edit['recipient'], - '#size' => '20', - '#maxlength' => '254', - '#description' => t('The email address to which the message should be sent OR enter %author if you would like to send an e-mail to the original author of the post.', array('%author' => theme('placeholder', t('%author')))), + '#title' => t('Usernames'), + '#default_value' => implode(',', $edit['recipients']['users']), + '#autocomplete_path' => 'user/autocomplete', + '#description' => t('Enter the names of registered users who should get this email.') .' '. t('Separate multiple users by commas.'), ); - $form['subject'] = array( + $form['recipients']['raw_mail_addresses'] = array( + '#type' => 'textfield', + '#title' => t('Email addresses'), + '#default_value' => implode(',', $edit['recipients']['raw_mail_addresses']), + '#description' => t('The email addresses of individual people who should get this email.') .' '. t('Separate multiple addresses by commas.'), + ); + $form['recipients']['author'] = array( + '#type' => 'checkbox', + '#title' => t('Author'), + '#default_value' => $edit['recipients']['author'], + '#description' => t("Check this if the nodes' author should get this email."), + ); + + $form['message'] = array( + '#type' => 'fieldset', + '#title' => t('Message'), + '#collapsible' => true, + '#collapsed' => true, + ); + $form['message']['subject'] = array( '#type' => 'textfield', '#title' => t('Subject'), - '#default_value' => $edit['subject'], + '#default_value' => $edit['message']['subject'], '#size' => '20', '#maxlength' => '254', '#description' => t('The subject of the message.'), ); - $form['message'] = array( + $form['message']['message'] = array( '#type' => 'textarea', '#title' => t('Message'), - '#default_value' => $edit['message'], + '#default_value' => $edit['message']['message'], '#cols' => '80', '#rows' => '20', - '#description' => t('The message that should be sent. You may include the following variables: %site_name, %username, %node_url, %node_type, %title, %teaser, %body'), + '#description' => t('The message that should be sent. You may include the following variables: %site_name, %username, %node_url, %node_type, %title, %teaser, %body'), ); return $form; @@ -431,21 +460,132 @@ function action_send_email($op, $edit = case 'validate': $errors = array(); - if (!valid_email_address($edit['recipient']) && $edit['recipient'] != t('%author')) { - $errors['recipient'] = t('Please enter a valid email address or %author.', array('%author' => theme('placeholder', t('%author')))); + foreach($edit['roles'] as $key => $rid) { + if($key == $rid) { + $recipients_specified = true; + break; + } + } + + if($edit['users']) { + $recipients_specified = true; + $users = explode(',', $edit['users']); + foreach($users as $account) { + if(!empty($account) && !user_load(array('name' => $account))) { + $errors['users'] = t('The username %name does not exist.', array('%name' => theme('placeholder', $account))); + break; + } + } + } + + if($edit['raw_mail_addresses']) { + $recipients_specified = true; + $raw_mail_addresses = explode(',', $edit['raw_mail_addresses']); + foreach($raw_mail_addresses as $address) { + if(!valid_email_address($address)) { + $errors['raw_mail_addresses'] = t('%address is not a valid email address.', array('%address' => theme('placeholder', $address))); + break; + } + } + } + + if($edit['author']) { + $recipients_specified = true; + } + + if(!$recipients_specified) { + $errors[] = t('You have to specify any recipient.'); } + foreach ($errors as $name => $message) { form_set_error($name, $message); } - return count($errors) == 0; // process the HTML form to store configuration case 'submit': - $params = array( - 'recipient' => $edit['recipient'], - 'subject' => $edit['subject'], - 'message' => $edit['message']); + $roles = array(); + foreach($edit['roles'] as $key => $rid) { + if($key == $rid) { + $roles[] = $key; + } + } + + if($edit['users']) { + $edit['users'] = array_unique(explode(',', $edit['users'])); + foreach($edit['users'] as $key => $account) { + $account = trim($account); + if($account=='') { + unset($edit['users'][$key]); + } + else { + $edit['users'][$key] = $account; + } + } + } + else { + $edit['users'] = array(); + } + + if($edit['raw_mail_addresses']) { + $edit['raw_mail_addresses'] = array_unique(explode(',', $edit['raw_mail_addresses'])); + foreach($edit['raw_mail_addresses'] as $key => $address) { + $address = trim($address); + if($address == '') { + unset($edit['raw_mail_addresses'][$key]); + } + else { + $edit['raw_mail_addresses'][$key] = $address; + } + } + } + else { + $edit['raw_mail_addresses'] = array(); + } + + $params['recipients']['roles'] = $roles; + $params['recipients']['users'] = $edit['users']; + $params['recipients']['raw_mail_addresses'] = $edit['raw_mail_addresses']; + $params['recipients']['author'] = ($edit['author']); + $params['message']['subject'] = check_plain($edit['subject']); + $params['message']['message'] = check_plain($edit['message']); return $params; } -} \ No newline at end of file +} + +function _action_send_email_get_mail_addresses($edit, $node) { + $mail_addresses = array(); + + $count = count($edit['recipients']['roles']); + if($count) { + $first = array_shift($edit['recipients']['roles']); + $sql = 'SELECT uid FROM {users_roles} WHERE (rid = %d'; + if($count > 1) { + foreach($edit['recipients']['roles'] as $foo) { + $sql .= ' OR rid = %d'; + } + } + $result = db_query($sql .');', $first, $edit['recipients']['roles']); + while ($account = db_fetch_object($result)) { + if ($account = user_load(array('uid' => $account->uid, 'status' => 1))) { + $mail_addresses[$account->mail] = ''; + } + } + } + + foreach($edit['recipients']['users'] as $name) { + if ($account = user_load(array('name' => $name, 'status' => 1))) { + $mail_addresses[$account->mail] = ''; + } + } + + foreach($edit['recipients']['raw_mail_addresses'] as $address) { + $mail_addresses[$address] = ''; + } + + if ($account = user_load(array('uid' => $node->uid, 'status' => 1))) { + $mail_addresses[$account->mail] = ''; + } + + return $mail_addresses; +} Index: actions.install =================================================================== RCS file: /cvs/drupal/contributions/modules/actions/actions.install,v retrieving revision 1.1.2.2 diff -u -F^f -r1.1.2.2 actions.install --- actions.install 21 Apr 2006 14:19:54 -0000 1.1.2.2 +++ actions.install 15 May 2006 14:52:42 -0000 @@ -18,7 +18,7 @@ function actions_install() { ) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */; QUERY ); - + $query2 = db_query( <<params); + if (isset($old['recipient'])) { + $new['recipients']['raw_mail_addresses'] = $old['recipient']; + $new['message']['subject'] = $old['subject']; + $new['message']['message'] = $old['message']; + $query[] = db_update_sql('UPDATE {actions} SET params = %s WHERE aid = %s;', serialize($new), $res->aid); + } + } + return $query; +}