/[drupal]/contributions/modules/mail/mail.module
ViewVC logotype

Diff of /contributions/modules/mail/mail.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.22, Tue Oct 17 02:23:58 2006 UTC revision 1.23, Tue Oct 17 18:08:50 2006 UTC
# Line 1  Line 1 
1  <?php  <?php
2    
3  // $Id: mail.module,v 1.21 2006/10/16 20:15:32 nedjo Exp $  // $Id: mail.module,v 1.22 2006/10/17 02:23:58 nedjo Exp $
4    
5  /**  /**
6   * Provide online user help   * Provide online user help
# Line 18  function mail_help($section = 'admin/hel Line 18  function mail_help($section = 'admin/hel
18        <h3>Setting up content types for mailing</h3>        <h3>Setting up content types for mailing</h3>
19        <p>Use a content type's <a href=\"%node-configuration\">configuration page</a> to enable emailing for that content type.  Set Email to \"enabled\".  This will add an email section to the editing form for that content type, allowing users with appropriate permissions to send updates by email.</p>        <p>Use a content type's <a href=\"%node-configuration\">configuration page</a> to enable emailing for that content type.  Set Email to \"enabled\".  This will add an email section to the editing form for that content type, allowing users with appropriate permissions to send updates by email.</p>
20        <h3>Accepting emails</h3>        <h3>Accepting emails</h3>
21        <p>The mail module adds a new field to the user registration and editing forms through which users can opt in to receiving site emails.  Emails send from non-admin accounts will go out only to users who have opted to receive emails.  However, mails sent from user accounts with the \"administer users\" permission will go out to all users, whether or not they have opted in.</p>        <p>The mail module adds a new field to the user registration and editing forms through which users can opt in to receiving site emails.  Emails send from non-admin accounts will go out only to users who have opted to receive emails.  However, mails sent from user accounts with the \"administer users\" permission can go out to all users, whether or not they have opted in.</p>
22        <h3>User access permissions for mail</h3>        <h3>User access permissions for mail</h3>
23        <p><strong>send emails to users:</strong> Allows a role to post email to users. You must enable this permission to in order for a role to create a mail message.</p>        <p><strong>send emails to users:</strong> Allows a role to post email to users. You must enable this permission to in order for a role to create a mail message.</p>
24        <h3>Settings</h3>        <h3>Settings</h3>
# Line 174  function mail_form_alter($form_id, &$for Line 174  function mail_form_alter($form_id, &$for
174          '#default_value' => $node->mail_out,          '#default_value' => $node->mail_out,
175        );        );
176        $roles = user_roles(TRUE);        $roles = user_roles(TRUE);
       unset($roles[DRUPAL_AUTHENTICATED_RID]);  
177        $form['mail_options']['mail_roles_selected'] = array(        $form['mail_options']['mail_roles_selected'] = array(
178          '#type' => 'checkboxes',          '#type' => 'checkboxes',
179          '#title' => t('Roles'),          '#title' => t('Roles'),
# Line 182  function mail_form_alter($form_id, &$for Line 181  function mail_form_alter($form_id, &$for
181          '#options' => $roles,          '#options' => $roles,
182          '#description' => t('Select roles to send the email to.  At least one role is required for email to be sent.'),          '#description' => t('Select roles to send the email to.  At least one role is required for email to be sent.'),
183        );        );
184          if (user_access('administer users')) {
185            $form['mail_options']['mail_override_opt'] = array(
186              '#type' => 'checkbox',
187              '#title' => t('Send to users who haven\'t opted in'),
188              '#default_value' => $node->mail_override_opt,
189              '#description' => t('Checking this option will send mail to users in the selected roles even if they have not opted in to receiving mail.'),
190            );
191          }
192        $form['mail_options']['mail_format'] = array(        $form['mail_options']['mail_format'] = array(
193          '#type' => 'select',          '#type' => 'select',
194          '#title' => t('Format'),          '#title' => t('Format'),
# Line 222  function mail_nodeapi(&$node, $op, $arg) Line 229  function mail_nodeapi(&$node, $op, $arg)
229    if (variable_get('mail_'. $node->type, FALSE) && user_access('send mail to users')) {    if (variable_get('mail_'. $node->type, FALSE) && user_access('send mail to users')) {
230      switch ($op) {      switch ($op) {
231        case 'validate':        case 'validate':
232          if ($node->mail_out && empty($node->mail_roles_selected)) {          if ($node->mail_out && !count(array_filter($node->mail_roles_selected))) {
233            form_set_error('mail_roles_selected', t('You need to select at least one role to send the mail to.'));            form_set_error('mail_roles_selected', t('You need to select at least one role to send the mail to.'));
234          }          }
235          break;          break;
# Line 289  function theme_mail_message($node) { Line 296  function theme_mail_message($node) {
296    $output = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";    $output = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
297    $output .= '<html xmlns="http://www.w3.org/1999/xhtml">';    $output .= '<html xmlns="http://www.w3.org/1999/xhtml">';
298    $output .= '<head>';    $output .= '<head>';
299    $output .= ' <title>'. $node->title .'</title>';    $output .= ' <title>'. check_plain($node->title) .'</title>';
300    $output .= "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n";    $output .= "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n";
301      $base = $base_url;
302      if (strlen(base_path()) > 1) {
303        $base = substr($base, 0, strpos($base, substr(base_path(), 0, strlen(base_path()) - 1)));
304      }
305      $output .= '<base  href="'. $base .'" />';
306    /*    /*
307    $output .= '<style type="text/css" src="'. base_path() .'/misc/drupal.css" />';    $output .= '<style type="text/css" src="'. base_path() .'/misc/drupal.css" />';
308    foreach (theme_add_style() as $style) {    foreach (theme_add_style() as $style) {
# Line 323  function _mail_dispatch($node) { Line 335  function _mail_dispatch($node) {
335    $node->from_name = ($user->uid == 1 ? variable_get('site_name', 'drupal') : t('%user at %site', array('%user' => $user->name, '%site' => variable_get('site_name', 'drupal'))));    $node->from_name = ($user->uid == 1 ? variable_get('site_name', 'drupal') : t('%user at %site', array('%user' => $user->name, '%site' => variable_get('site_name', 'drupal'))));
336    $node->encode = 'quoted-printable';    $node->encode = 'quoted-printable';
337    $roles = $node->mail_roles_selected;    $roles = $node->mail_roles_selected;
338    if(empty($roles)) {    if (empty($roles)) {
339      $roles = array();      $roles = array();
340    }    }
341    $roles_where = array();    $roles_where = array();
342    foreach ($roles as $rid) {    // If 'authenticated user' role is selected, we don't filter by role as we're sending to all roles.
343      $roles_where[] = 's.rid = ' . $rid;    if (!in_array(DRUPAL_AUTHENTICATED_RID, $roles)) {
344        foreach ($roles as $rid) {
345          $roles_where[] = 's.rid = ' . $rid;
346        }
347        (count($roles_where)) ? $where = ' AND (' . implode(' OR ', $roles_where) . ') ' : $where = '';
348        $result = db_query('SELECT DISTINCT(u.uid) FROM {users} u, {role} r, {users_roles} s WHERE u.uid = s.uid AND r.rid = s.rid AND u.status != 0' . $where);
349      }
350      else {
351        $result = db_query('SELECT uid FROM {users} WHERE status != 0');
352    }    }
   (count($roles_where) > 0) ? $where = ' AND (' . implode(' OR ', $roles_where) . ') ': $where = '';  
   $result = db_query('SELECT DISTINCT(u.uid) FROM {users} u, {role} r, {users_roles} s WHERE u.uid = s.uid AND r.rid = s.rid AND u.status != 0' . $where);  
353    $success = TRUE;    $success = TRUE;
354    while ($uid = db_fetch_object($result)) {    while ($uid = db_fetch_object($result)) {
355      $account = user_load(array('uid' => $uid->uid, 'status' => 1));      $account = user_load(array('uid' => $uid->uid, 'status' => 1));
356      if (!$account->mail_accept && !user_access('administer users')) {      if (!$account->mail_accept && !($node->mail_override_opt && user_access('administer users'))) {
357        continue;        //continue;
358      }      }
359      if($account->mail) {      if ($account->mail) {
360        $node->to = $account->mail;        $node->to = $account->mail;
361        $variables = array('%username' => $account->name, '%site' => variable_get('site_name', 'drupal'), '%uri' => $base_url, '%uri_brief' => substr($base_url, strlen('http://')), '%mailto' => $account->mail, '%date' => format_date(time()), '%login_uri' => url('user/login', NULL, NULL, TRUE), '%edit_uri' => url('user/edit', NULL, NULL, TRUE));        $variables = array('%username' => $account->name, '%site' => variable_get('site_name', 'drupal'), '%uri' => $base_url, '%uri_brief' => substr($base_url, strlen('http://')), '%mailto' => $account->mail, '%date' => format_date(time()), '%login_uri' => url('user/login', NULL, NULL, TRUE), '%edit_uri' => url('user/edit', NULL, NULL, TRUE));
362        $message = strtr(node_view($node, FALSE, FALSE, FALSE), $variables);        $message = strtr(node_view($node, FALSE, FALSE, FALSE), $variables);
# Line 348  function _mail_dispatch($node) { Line 366  function _mail_dispatch($node) {
366        else {        else {
367          $node->message = theme('mail_message', $node);          $node->message = theme('mail_message', $node);
368        }        }
369        if(!mail_send($node)) {        if (!mail_send($node)) {
370          $success = FALSE;          $success = FALSE;
371        }        }
372      }      }
373    }    }
374    if($success) {    if ($success) {
375      drupal_set_message(t('Message %title sent to users with the following roles: %roles.', array('%title' => $node->title, '%roles' => implode(', ', _mail_get_roles_names($roles)))));      drupal_set_message(t('Message %title sent to users with the following roles: %roles.', array('%title' => $node->title, '%roles' => implode(', ', _mail_get_roles_names($roles)))));
376    }    }
377    else {    else {
# Line 387  function mail_send($mail) { Line 405  function mail_send($mail) {
405  /**  /**
406   * Generate an array of role names   * Generate an array of role names
407   */   */
408  function _mail_get_roles_names($roles) {  function _mail_get_roles_names($roles = array()) {
409    $roles_names = array();    $roles_names = array();
410    $result = db_query('SELECT name FROM {role} WHERE rid in (' . implode(',', $roles) . ') ORDER BY name');    if (count($roles)) {
411    while ($role = db_fetch_object($result)) {      $result = db_query('SELECT name FROM {role} WHERE rid in (' . implode(',', $roles) . ') ORDER BY name');
412      $roles_names[] = $role->name;      while ($role = db_fetch_object($result)) {
413          $roles_names[] = $role->name;
414        }
415    }    }
416    return $roles_names;    return $roles_names;
417  }  }

Legend:
Removed from v.1.22  
changed lines
  Added in v.1.23

  ViewVC Help
Powered by ViewVC 1.1.2