/[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.24, Tue Oct 24 23:18:09 2006 UTC revision 1.25, Mon Nov 26 18:08:53 2007 UTC
# Line 1  Line 1 
1  <?php  <?php
2    
3  // $Id: mail.module,v 1.23 2006/10/17 18:08:50 nedjo Exp $  // $Id: mail.module,v 1.24 2006/10/24 23:18:09 nedjo Exp $
4    
5  /**  /**
6   * Provide online user help   * Provide online user help
7   */   */
8  function mail_help($section = 'admin/help#story') {  function mail_help($section = 'admin/help#story') {
9    switch ($section) {    switch ($section) {
     case 'admin/modules#description':  
       return t('Enables site administrator or users to send emails to registered users.');  
10      case 'admin/settings/mail':      case 'admin/settings/mail':
11        return t('Mail postings are sent to registered users with specified roles and also saved to the site database for future reference. Email messages may be customized through the use of variables as listed under the "Message" field.');        return t('Mail postings are sent to registered users with specified roles and also saved to the site database for future reference. Email messages may be customized through the use of variables as listed under the "Message" field.');
12      case 'admin/help#mail':      case 'admin/help#mail':
# Line 38  function mail_help($section = 'admin/hel Line 36  function mail_help($section = 'admin/hel
36  /**  /**
37   * Declare administrative settings for a module.   * Declare administrative settings for a module.
38   */   */
39  function mail_settings() {  function mail_admin_settings() {
40    $form = array();    $form = array();
41    $form['options'] = array(    $form['options'] = array(
42      '#type' => 'fieldset',      '#type' => 'fieldset',
# Line 73  function mail_settings() { Line 71  function mail_settings() {
71      '#default_value' => variable_get('mail_receipt', 0),      '#default_value' => variable_get('mail_receipt', 0),
72    );    );
73    
74    return $form;    return system_settings_form($form);
75  }  }
76    
77  /**  /**
# Line 84  function mail_node_info() { Line 82  function mail_node_info() {
82  }  }
83    
84  /**  /**
  * Define user permissions.  
  */  
 function mail_perm() {  
   return array('send mail to users');  
 }  
   
 /**  
  * Define access restrictions  
  */  
 function mail_access($op, $node) {  
   global $user;  
   
   if ($op == 'view') {  
     return $node->status;  
   }  
   
   if ($op == 'create') {  
     return user_access('send mail to users');  
   }  
   
 }  
   
 /**  
85   * Implementation of hook_menu().   * Implementation of hook_menu().
86   */   */
87  function mail_menu($may_cache) {  function mail_menu() {
88    $items = array();    $items = array();
89    
90    if ($may_cache) {    $items['admin/settings/mail'] = array (
91      $items[] = array(      'title' => 'Mail',
92        'path' => 'node/add/mail',      'description' => 'Enables site administrator or users to send emails to registered users.',
93        'title' => t('mail'),      'page callback' => 'drupal_get_form',
94        'access' => user_access('send mail to users'),      'page arguments' => array('mail_admin_settings'),
95      );      'access callback' => 'user_access',
96    }      'access arguments' => array('access administration pages'),
97        'type' => MENU_NORMAL_ITEM,
   return $items;  
 }  
   
 /**  
  * Display a node editing form  
  */  
 function mail_form(&$node) {  
   $form['title'] = array(  
     '#type' => 'textfield',  
     '#title' => t('Title'),  
     '#required' => TRUE,  
     '#default_value' => $node->title,  
     '#weight' => -5  
98    );    );
99    $form['body_filter']['body'] = array(    return $items;
     '#type' => 'textarea',  
     '#title' => t('Message'),  
     '#description' => t('This will be the body of your email message.')  .' '. t('Available variables are:') .' %username, %site, %uri, %uri_brief, %mailto, %login_uri, %edit_uri.',  
     '#default_value' => $node->body,  
     '#rows' => 20,  
     '#required' => TRUE  
   );  
   $form['body_filter']['format'] = filter_form($node->format);  
   return $form;  
100  }  }
101    
102  /**  /**
103   * Implementation of hook_form_alter().   * Implementation of hook_form_alter().
104   */   */
105  function mail_form_alter($form_id, &$form) {  function mail_form_alter(&$form, $form_state, $form_id) {
106      // don't let users change the machine readable type since the uninstaller is dependent on it being named mail
107      if (isset($form['#node_type']->type) && $form['#node_type']->type == 'mail' && arg(0) == 'admin' && arg(1) == 'content' && arg(2) == 'types' && arg(3) == 'mail') {
108        $form['identity']['type'] += array(
109          '#disabled' => true
110        );
111      }
112    
113    if (isset($form['type'])) {    if (isset($form['type'])) {
114      if ($form['type']['#value'] .'_node_settings' == $form_id) {      if ($form['type']['#value'] .'_node_settings' == $form_id) {
115        $form['workflow']['mail_'. $form['type']['#value']] = array(        $form['workflow']['mail_'. $form['type']['#value']] = array(
# Line 161  function mail_form_alter($form_id, &$for Line 121  function mail_form_alter($form_id, &$for
121      }      }
122    
123      $node = $form['#node'];      $node = $form['#node'];
124      if ($form['type']['#value'] .'_node_form' == $form_id && variable_get("mail_$node->type", FALSE) && user_access('send mail to users')) {      if ($form['type']['#value'] == 'mail' && variable_get("mail_$node->type", FALSE)) {
125    
126        $form['mail_options'] = array(        $form['mail_options'] = array(
127          '#type' => 'fieldset',          '#type' => 'fieldset',
# Line 171  function mail_form_alter($form_id, &$for Line 131  function mail_form_alter($form_id, &$for
131        $form['mail_options']['mail_out'] = array(        $form['mail_options']['mail_out'] = array(
132          '#type' => 'checkbox',          '#type' => 'checkbox',
133          '#title' => t('Email out a copy of this post'),          '#title' => t('Email out a copy of this post'),
134          '#default_value' => $node->mail_out,          '#default_value' => empty($node->mail_out) ? false : $node->mail_out,
       );  
       $roles = user_roles(TRUE);  
       $form['mail_options']['mail_roles_selected'] = array(  
         '#type' => 'checkboxes',  
         '#title' => t('Roles'),  
         '#default_value' => $node->mail_roles_selected ? $node->mail_roles_selected : array(),  
         '#options' => $roles,  
         '#description' => t('Select roles to send the email to.  At least one role is required for email to be sent.'),  
135        );        );
136        if (user_access('administer users')) {        if (user_access('administer users')) {
137          $form['mail_options']['mail_override_opt'] = array(          $form['mail_options']['mail_override_opt'] = array(
138            '#type' => 'checkbox',            '#type' => 'checkbox',
139            '#title' => t('Send to users who haven\'t opted in'),            '#title' => t('Send to users who haven\'t opted in'),
140            '#default_value' => $node->mail_override_opt,            '#default_value' => empty($node->mail_override_opt) ? false : $node->mail_override_opt,
141            '#description' => t('Checking this option will send mail to users in the selected roles even if they have not opted in to receiving mail.'),            '#description' => t('Checking this option will send mail to users in the selected roles even if they have not opted in to receiving mail.'),
142          );          );
143        }        }
144          $form['mail_options']['roles'] = array(
145            '#type' => 'fieldset',
146            '#title' => t('Roles'),
147            '#description' => t('Select roles to send the email to.  At least one role is required for email to be sent.')
148          );
149          $roles = user_roles(TRUE);
150          $form['mail_options']['roles']['mail_roles_selected'] = array(
151            '#type' => 'checkboxes',
152            '#default_value' => empty($node->mail_roles_selected) ? array() : $node->mail_roles_selected,
153            '#options' => $roles,
154          );
155        $form['mail_options']['mail_format'] = array(        $form['mail_options']['mail_format'] = array(
156          '#type' => 'select',          '#type' => 'select',
157          '#title' => t('Format'),          '#title' => t('Format'),
158          '#description' => t('Select the default email sending format.'),          '#description' => t('Select the default email sending format.'),
159          '#options' => array('plain' => t('plain'), 'html' => t('html')),          '#options' => array('plain' => t('plain'), 'html' => t('html')),
160          '#default_value' => $node->mail_format ? $node->mail_format : variable_get('mail_format', 'plain'),          '#default_value' => empty($node->mail_format) ? variable_get('mail_format', 'plain') : $node->mail_format,
161        );        );
162        $form['mail_options']['mail_priority'] = array(        $form['mail_options']['mail_priority'] = array(
163          '#type' => 'select',          '#type' => 'select',
164          '#title' => t('Priority'),          '#title' => t('Priority'),
165          '#description' => t('Note that email priority is ignored by a lot of email programs.'),          '#description' => t('Note that email priority is ignored by a lot of email programs.'),
166          '#options' => array(0 => t('none'), 1 => t('highest'), 2 => t('high'), 3 => t('normal'), 4 => t('low'), 5 => t('lowest')),          '#options' => array(0 => t('none'), 1 => t('highest'), 2 => t('high'), 3 => t('normal'), 4 => t('low'), 5 => t('lowest')),
167          '#default_value' => $node->mail_priority ? $node->mail_priority : variable_get('mail_priority', 0),          '#default_value' => empty($node->mail_priority) ? variable_get('mail_priority', 0) : $node->mail_priority,
       );  
       $form['mail_options']['mail_character_set'] = array(  
         '#type' => 'select',  
         '#title' => t('Character set'),  
         '#description' => t('Different languages may need a different character set to have email messages displayed correctly. The default for English is (en) iso-8859-1. For more details visit the <a href=\"http://www.w3.org/International/O-charset-lang.html\">w3.org character set guide</a>.'),  
         '#options' => drupal_map_assoc(array('iso-8859-1','iso-8859-7','iso-8859-5', 'UTF-8')),  
         '#default_value' => $node->mail_character_set ? $node->mail_character_set : variable_get('mail_character_set', 'iso-8859-1'),  
168        );        );
169        $form['mail_options']['mail_receipt'] = array(        $form['mail_options']['mail_receipt'] = array(
170          '#type' => 'checkbox',          '#type' => 'checkbox',
171          '#title' => t('Request receipt'),          '#title' => t('Request receipt'),
172          '#description' => t('Request a Read Receipt from your mails. A lot of email programmes ignore these so it is not a definitive indication of how many people have read your mailout.'),          '#description' => t('Request a Read Receipt from your mails. A lot of email programs ignore or do not support these so it is not a definitive indication of how many people have read your mailout.'),
173          '#default_value' => $node->mail_receipt ? $node->mail_receipt : variable_get('mail_receipt', 0),          '#default_value' => empty($node->mail_receipt) ? variable_get('mail_receipt', 0) : $node->mail_receipt,
174        );        );
175      }      }
176    }    }
# Line 224  function mail_form_alter($form_id, &$for Line 180  function mail_form_alter($form_id, &$for
180   * Implementation of _nodeapi hook.   * Implementation of _nodeapi hook.
181   * Provides the ability to add mailing functionality to any node type.   * Provides the ability to add mailing functionality to any node type.
182   */   */
183  function mail_nodeapi(&$node, $op, $arg) {  function mail_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
184    global $db_url;    global $db_url;
185    if (variable_get('mail_'. $node->type, FALSE) && user_access('send mail to users')) {    if (variable_get('mail_'. $node->type, FALSE)) {
186      switch ($op) {      switch ($op) {
187        case 'validate':        case 'validate':
188          if ($node->mail_out && !count(array_filter($node->mail_roles_selected))) {          if ($node->mail_out && !count(array_filter($node->mail_roles_selected))) {
# Line 235  function mail_nodeapi(&$node, $op, $arg) Line 191  function mail_nodeapi(&$node, $op, $arg)
191          break;          break;
192        case 'insert':        case 'insert':
193        case 'update':        case 'update':
194          if($node->mail_out && $node->status && !$node->moderate) {          if ($node->mail_out && $node->status && empty($node->moderate)) {
195            $node->mail_roles_selected = array_filter($node->mail_roles_selected);            $node->mail_roles_selected = array_filter($node->mail_roles_selected);
196            _mail_dispatch($node);            _mail_dispatch($node);
197            $db_type = substr($db_url, 0, strpos($db_url, '://'));            $db_type = substr($db_url, 0, strpos($db_url, '://'));
# Line 264  function mail_content($node, $main = 0) Line 220  function mail_content($node, $main = 0)
220   * Provide form element to opt in to content mailouts.   * Provide form element to opt in to content mailouts.
221   */   */
222  function mail_user($type, $edit, &$user, $category = NULL) {  function mail_user($type, $edit, &$user, $category = NULL) {
223    if ($type == 'register' || ($type == 'form' && $category == 'account')) {    if ($type == 'form' && $category == 'account') {
224      $form['mail'] = array(      $form = array();
225        '#type' => 'fieldset',  
226        '#title' => t('Email settings'),      // $form['mail'] is set in the mimemail module, this just adds to it
227        '#weight' => 5,      $form['mail']['mail_accept'] = array(
228        '#collapsible' => TRUE,        '#type' => 'checkbox',
     );  
     $form['mail']['mail_accept'] = array('#type' => 'checkbox',  
229        '#title' => t('Accept email'),        '#title' => t('Accept email'),
230        '#default_value' => $edit['mail_accept'],        '#default_value' => empty($edit['mail_accept']) ? false : $edit['mail_accept'],
231        '#description' => t('Allow users posting content to send it to you by email. Note that your e-mail address is not made public and that privileged users such as site administrators are able to email you even if you choose not to enable this feature.'),        '#description' => t('Allow users posting content to send it to you by email. Note that your e-mail address is not made public and that privileged users such as site administrators are able to email you even if you choose not to enable this feature.'),
232      );      );
233      return $form;      return $form;
# Line 302  function theme_mail_message($node) { Line 256  function theme_mail_message($node) {
256    if (strlen(base_path()) > 1) {    if (strlen(base_path()) > 1) {
257      $base = substr($base, 0, strpos($base, substr(base_path(), 0, strlen(base_path()) - 1)));      $base = substr($base, 0, strpos($base, substr(base_path(), 0, strlen(base_path()) - 1)));
258    }    }
259    $output .= '<base  href="'. $base .'" />';    $output .= '<base href="'. $base .'" />';
260    /*    /*
261    $output .= '<style type="text/css" src="'. base_path() .'/misc/drupal.css" />';    $output .= '<style type="text/css" src="'. base_path() .'/misc/drupal.css" />';
262    foreach (theme_add_style() as $style) {    foreach (theme_add_style() as $style) {
# Line 315  function theme_mail_message($node) { Line 269  function theme_mail_message($node) {
269    $output .= '<table border="0" cellspacing="4" cellpadding="4"><tr><td style="vertical-align: top;">';    $output .= '<table border="0" cellspacing="4" cellpadding="4"><tr><td style="vertical-align: top;">';
270    
271    $output .= "\n<!-- begin content -->\n";    $output .= "\n<!-- begin content -->\n";
272    $output .= '<p>' . t('Sent by <a href="%name-url">%name</a> from %site.', array('%name' => $user->name, '%name-url' => url("user/$user->uid", NULL, NULL, TRUE), '%site' => variable_get('site_name', 'drupal'))) . '</p>';    $output .= '<p>' . t('Sent by <a href="%name-url">%name</a> from %site.', array('%name' => $user->name, '%name-url' => url("user/$user->uid", array('absolute' => TRUE)), '%site' => variable_get('site_name', 'Drupal'))) . '</p>';
273    $output .= '<p>' . t('If you don\'t wish to receive such e-mails, you can <a href="%url">change your account settings</a>.', array('%url' => url('user/' . $node->uid, NULL, NULL, TRUE))) . '</p>';    $output .= '<p>' . t('If you don\'t wish to receive such e-mails, you can <a href="%url">change your account settings</a>.', array('%url' => url('user/' . $node->uid, array('absolute' => TRUE)))) . '</p>';
274    $output .= node_view($node, FALSE, FALSE, FALSE);    $output .= node_view($node, FALSE, FALSE, FALSE);
275    $output .= "\n<!-- end content -->\n";    $output .= "\n<!-- end content -->\n";
276    
# Line 331  function theme_mail_message($node) { Line 285  function theme_mail_message($node) {
285   */   */
286  function _mail_dispatch($node) {  function _mail_dispatch($node) {
287    global $user, $base_url;    global $user, $base_url;
288    
289    $node->from_address = ($user->uid == 1 ? variable_get('site_mail', ini_get('sendmail_from')) : $user->mail);    $node->from_address = ($user->uid == 1 ? variable_get('site_mail', ini_get('sendmail_from')) : $user->mail);
290    $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'))));
   $node->encode = 'quoted-printable';  
291    $roles = $node->mail_roles_selected;    $roles = $node->mail_roles_selected;
292    if (empty($roles)) {    if (empty($roles)) {
293      $roles = array();      $roles = array();
# Line 344  function _mail_dispatch($node) { Line 298  function _mail_dispatch($node) {
298      foreach ($roles as $rid) {      foreach ($roles as $rid) {
299        $roles_where[] = 's.rid = ' . $rid;        $roles_where[] = 's.rid = ' . $rid;
300      }      }
301      (count($roles_where)) ? $where = ' AND (' . implode(' OR ', $roles_where) . ') ' : $where = '';      $where = (count($roles_where)) ? ' AND (' . implode(' OR ', $roles_where) . ') ' : '';
302      $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);      $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);
303    }    }
304    else {    else {
# Line 353  function _mail_dispatch($node) { Line 307  function _mail_dispatch($node) {
307    $success = TRUE;    $success = TRUE;
308    while ($uid = db_fetch_object($result)) {    while ($uid = db_fetch_object($result)) {
309      $account = user_load(array('uid' => $uid->uid, 'status' => 1));      $account = user_load(array('uid' => $uid->uid, 'status' => 1));
310      if (!$account->mail_accept && !($node->mail_override_opt && user_access('administer users'))) {      if (empty($account->mail_accept) && !($node->mail_override_opt && user_access('administer users'))) {
311        //continue;        //continue;
312      }      }
313      if ($account->mail) {      if ($account->mail) {
314        $node->to = $account->mail;        $node->to = $account->mail;
315        $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', array('absolute' => TRUE)), '%edit_uri' => url('user/edit', array('absolute' => TRUE)));
316          if (!isset($node->teaser)) {
317            $node->teaser = ''; // probably not a proper solution
318          }
319        $message = strtr(node_view($node, FALSE, FALSE, FALSE), $variables);        $message = strtr(node_view($node, FALSE, FALSE, FALSE), $variables);
320        if ($node->mail_format == 'plain') {        if ($node->mail_format == 'plain') {
321          $node->message = strip_tags($message);          $node->message = trim(strip_tags($message));
322        }        }
323        else {        else {
324          $node->message = theme('mail_message', $node);          $node->message = theme('mail_message', $node);
# Line 371  function _mail_dispatch($node) { Line 328  function _mail_dispatch($node) {
328        }        }
329      }      }
330    }    }
   if ($success) {  
     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)))));  
   }  
   else {  
     drupal_set_message(t('Error sending message %title to users with the following roles: %roles.', array('%title' => $node->title, '%roles' => implode(', ', _mail_get_roles_names($roles)))));  
   }  
331  }  }
332    
333  /**  /**
334   * Send mail   * Send mail
335     *
336     * @param object $mail
337   */   */
338  function mail_send($mail) {  function mail_send($mail) {
339    require_once('activeMailLib.php');    $sender['name'] = $mail->from_name;
340    $email = new activeMailLib($mail->mail_format);    $sender['mail'] = $mail->from_address;
341    $email->From($mail->from_name . ' <' . $mail->from_address . '>');  
342    $email->To($mail->to);    $headers = array();
343    $email->Subject($mail->title);  
   $email->Message($mail->message, $mail->mail_character_set, $mail->mail_encode);  
   $email->priority($mail->mail_priority);  
344    if ($mail->mail_receipt) {    if ($mail->mail_receipt) {
345      $email->Receipt($mail->from_address);      $headers["Disposition-Notification-To"] = $mail->from_address;
346        $headers["X-Confirm-Reading-To"] = $mail->from_address;
347    }    }
348    if ($mail->files) {    if ($mail->mail_priority) {
349      foreach ($mail->files as $file) {      switch($mail->mail_priority) {
350        $email->Attachment($file->filepath, $file->filename);        case 1:
351            $headers['X-Priority'] = '1';
352            $headers['Priority'] = 'High';
353            $headers['X-MSMail-Priority'] = 'Highest';
354            break;
355          case 2:
356            $headers['X-Priority'] = '2';
357            $headers['Priority'] = 'urgent';
358            $headers['X-MSMail-Priority'] = 'High';
359            break;
360          case 3:
361            $headers['X-Priority'] = '3';
362            $headers['Priority'] = 'normal';
363            $headers['X-MSMail-Priority'] = 'Normal';
364            break;
365          case 4:
366            $headers['X-Priority'] = '4';
367            $headers['Priority'] = 'non-urgent';
368            $headers['X-MSMail-Priority'] = 'Low';
369            break;
370          case 5:
371            $headers['X-Priority'] = '5';
372            $headers['Priority'] = 'non-urgent';
373            $headers['X-MSMail-Priority'] = 'Lowest';
374            break;
375      }      }
376    }    }
377    $email->Send();    reset($headers);
378    return $email->isSent($mail->to);    $attachments = array();
379      $plaintext = false;
380      $text = NULL;
381      mimemail(array('name' => $mail->from_name, 'mail' => $mail->from_address), $mail->to, $mail->title, $mail->message, $plaintext, $headers, $text, $attachments);
382  }  }
383    
384  /**  /**

Legend:
Removed from v.1.24  
changed lines
  Added in v.1.25

  ViewVC Help
Powered by ViewVC 1.1.2