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

Contents of /contributions/modules/mail/mail.module

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


Revision 1.25 - (show annotations) (download) (as text)
Mon Nov 26 18:08:53 2007 UTC (2 years ago) by deekayen
Branch: MAIN
CVS Tags: HEAD
Changes since 1.24: +102 -123 lines
File MIME type: text/x-php
first round of updates to use the Mime Mail module in Drupal 6
1 <?php
2
3 // $Id: mail.module,v 1.24 2006/10/24 23:18:09 nedjo Exp $
4
5 /**
6 * Provide online user help
7 */
8 function mail_help($section = 'admin/help#story') {
9 switch ($section) {
10 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.');
12 case 'admin/help#mail':
13 return t("
14 <p>The mail module lets your users with appropriate permissions send mail to registered site users. Emails are also saved to the site database for future reference. The module provides a mail content type, and also lets other content types be emailed.</p>
15 <p>Mails sent from the admin account are tagged with the site name and come from the site admin email address. Mail sent from other accounts have that account's email as a return address and are tagged with a name in the form \"username at site\" where username is the user name of the user doing the posting and site is the name of the site.</p>
16 <h3>Setting up content types for mailing</h3>
17 <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>
18 <h3>Accepting emails</h3>
19 <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>
20 <h3>User access permissions for mail</h3>
21 <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>
22 <h3>Settings</h3>
23 <p>Use the mail <a href=\"%mail-settings\">mail settings page</a> to set default sending options. These include format (plain text or HTML), priority, character set, and receipt request. These options can be overridden for a particular mail post on the node editing page.</p>
24 <h3>Attachments</h3>
25 <p>To allow users to send attachments with email messages, enable the upload module and ensure that uploads are enabled for mail posts. Any files uploaded with a mail post will then be sent as attachments.</p>
26 ", array("%node-configuration" => url("admin/settings/content-types"), "%mail-settings" => url("admin/settings/mail")));
27 case 'node/add/mail':
28 if (!variable_get('mail_mail', 0)) {
29 return t('Emailing is not yet enabled for mail posts. An administrator must enable it in the <a href="%configure">mail content configuration page</a>.', array("%configure" => url("admin/settings/content-types/mail")));
30 }
31 case 'node/add#mail':
32 return t('A mail post can be sent to users of selected roles.');
33 }
34 }
35
36 /**
37 * Declare administrative settings for a module.
38 */
39 function mail_admin_settings() {
40 $form = array();
41 $form['options'] = array(
42 '#type' => 'fieldset',
43 '#title' => t('Default mail sending options'),
44 '#description' => t('These options will be the defaults for new mail messages, but can be overridden in the mail editing form.'),
45 );
46 $form['options']['mail_format'] = array(
47 '#type' => 'select',
48 '#title' => t('Format'),
49 '#description' => t('Select the default email sending format.'),
50 '#options' => array('plain' => t('plain'), 'html' => t('html')),
51 '#default_value' => variable_get('mail_format', 'plain'),
52 );
53 $form['options']['mail_priority'] = array(
54 '#type' => 'select',
55 '#title' => t('Priority'),
56 '#description' => t('Note that email priority is ignored by a lot of email programs.'),
57 '#options' => array(0 => t('none'), 1 => t('highest'), 2 => t('high'), 3 => t('normal'), 4 => t('low'), 5 => t('lowest')),
58 '#default_value' => variable_get('mail_priority', 0),
59 );
60 $form['options']['mail_character_set'] = array(
61 '#type' => 'select',
62 '#title' => t('Character set'),
63 '#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>.'),
64 '#options' => drupal_map_assoc(array('iso-8859-1','iso-8859-7','iso-8859-5', 'UTF-8')),
65 '#default_value' => variable_get('mail_character_set', 'iso-8859-1'),
66 );
67 $form['options']['mail_receipt'] = array(
68 '#type' => 'checkbox',
69 '#title' => t('Request receipt'),
70 '#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.'),
71 '#default_value' => variable_get('mail_receipt', 0),
72 );
73
74 return system_settings_form($form);
75 }
76
77 /**
78 * Implementation of hook_node_info().
79 */
80 function mail_node_info() {
81 return array('mail' => array('name' => t('mail'), 'base' => 'mail'));
82 }
83
84 /**
85 * Implementation of hook_menu().
86 */
87 function mail_menu() {
88 $items = array();
89
90 $items['admin/settings/mail'] = array (
91 'title' => 'Mail',
92 'description' => 'Enables site administrator or users to send emails to registered users.',
93 'page callback' => 'drupal_get_form',
94 'page arguments' => array('mail_admin_settings'),
95 'access callback' => 'user_access',
96 'access arguments' => array('access administration pages'),
97 'type' => MENU_NORMAL_ITEM,
98 );
99 return $items;
100 }
101
102 /**
103 * Implementation of hook_form_alter().
104 */
105 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'])) {
114 if ($form['type']['#value'] .'_node_settings' == $form_id) {
115 $form['workflow']['mail_'. $form['type']['#value']] = array(
116 '#type' => 'radios',
117 '#title' => t('Allow pages of this content type to be mailed'),
118 '#default_value' => variable_get('mail_'. $form['type']['#value'], 0),
119 '#options' => array(t('Disabled'), t('Enabled')),
120 );
121 }
122
123 $node = $form['#node'];
124 if ($form['type']['#value'] == 'mail' && variable_get("mail_$node->type", FALSE)) {
125
126 $form['mail_options'] = array(
127 '#type' => 'fieldset',
128 '#title' => t('Mail sending options'),
129 '#collapsible' => TRUE,
130 );
131 $form['mail_options']['mail_out'] = array(
132 '#type' => 'checkbox',
133 '#title' => t('Email out a copy of this post'),
134 '#default_value' => empty($node->mail_out) ? false : $node->mail_out,
135 );
136 if (user_access('administer users')) {
137 $form['mail_options']['mail_override_opt'] = array(
138 '#type' => 'checkbox',
139 '#title' => t('Send to users who haven\'t opted in'),
140 '#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.'),
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(
156 '#type' => 'select',
157 '#title' => t('Format'),
158 '#description' => t('Select the default email sending format.'),
159 '#options' => array('plain' => t('plain'), 'html' => t('html')),
160 '#default_value' => empty($node->mail_format) ? variable_get('mail_format', 'plain') : $node->mail_format,
161 );
162 $form['mail_options']['mail_priority'] = array(
163 '#type' => 'select',
164 '#title' => t('Priority'),
165 '#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')),
167 '#default_value' => empty($node->mail_priority) ? variable_get('mail_priority', 0) : $node->mail_priority,
168 );
169 $form['mail_options']['mail_receipt'] = array(
170 '#type' => 'checkbox',
171 '#title' => t('Request receipt'),
172 '#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' => empty($node->mail_receipt) ? variable_get('mail_receipt', 0) : $node->mail_receipt,
174 );
175 }
176 }
177 }
178
179 /**
180 * Implementation of _nodeapi hook.
181 * Provides the ability to add mailing functionality to any node type.
182 */
183 function mail_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
184 global $db_url;
185 if (variable_get('mail_'. $node->type, FALSE)) {
186 switch ($op) {
187 case 'validate':
188 if ($node->mail_out && !count(array_filter($node->mail_roles_selected))) {
189 form_set_error('mail_roles_selected', t('You need to select at least one role to send the mail to.'));
190 }
191 break;
192 case 'insert':
193 case 'update':
194 if ($node->mail_out && $node->status && empty($node->moderate)) {
195 $node->mail_roles_selected = array_filter($node->mail_roles_selected);
196 _mail_dispatch($node);
197 $db_type = substr($db_url, 0, strpos($db_url, '://'));
198 if ($db_type != 'pgsql') {
199 $body = '<p>' . t('This email was sent to users with the following roles: ') . implode(', ', _mail_get_roles_names($node->mail_roles_selected)) . "</p>\n";
200 $node->body = $body . $node->body;
201 $node->mail_out = FALSE;
202 node_save($node);
203 }
204 }
205 break;
206 }
207 }
208 }
209
210 /**
211 * Prepare a node's body content for viewing
212 */
213 function mail_content($node, $main = 0) {
214 return node_prepare($node, $main);
215 }
216
217 /**
218 * Implementation of hook_user().
219 *
220 * Provide form element to opt in to content mailouts.
221 */
222 function mail_user($type, $edit, &$user, $category = NULL) {
223 if ($type == 'form' && $category == 'account') {
224 $form = array();
225
226 // $form['mail'] is set in the mimemail module, this just adds to it
227 $form['mail']['mail_accept'] = array(
228 '#type' => 'checkbox',
229 '#title' => t('Accept email'),
230 '#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.'),
232 );
233 return $form;
234 }
235 elseif ($type == 'validate') {
236 return array('mail_accept' => $edit['mail_accept']);
237 }
238 }
239
240 /**
241 * Return an HTML page to be sent out as an email message body.
242 *
243 * @param $node
244 * The node object.
245 * @return
246 * A string containing the entire HTML page.
247 */
248 function theme_mail_message($node) {
249 global $user, $base_url;
250 $output = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
251 $output .= '<html xmlns="http://www.w3.org/1999/xhtml">';
252 $output .= '<head>';
253 $output .= ' <title>'. check_plain($node->title) .'</title>';
254 $output .= "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n";
255 $base = $base_url;
256 if (strlen(base_path()) > 1) {
257 $base = substr($base, 0, strpos($base, substr(base_path(), 0, strlen(base_path()) - 1)));
258 }
259 $output .= '<base href="'. $base .'" />';
260 /*
261 $output .= '<style type="text/css" src="'. base_path() .'/misc/drupal.css" />';
262 foreach (theme_add_style() as $style) {
263 $style->path = $base_url .'/'. substr($style->path, strlen(base_path()), strlen($style->path));
264 $output .= '<style type="text/css" src="'. $style->path .'" />';
265 }
266 */
267 $output .= '</head>';
268 $output .= '<body style="background-color: #fff; color: #000;">';
269 $output .= '<table border="0" cellspacing="4" cellpadding="4"><tr><td style="vertical-align: top;">';
270
271 $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", 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, array('absolute' => TRUE)))) . '</p>';
274 $output .= node_view($node, FALSE, FALSE, FALSE);
275 $output .= "\n<!-- end content -->\n";
276
277 $output .= '</td></tr></table>';
278 $output .= '</body></html>';
279
280 return $output;
281 }
282
283 /**
284 * Send mail to specified users
285 */
286 function _mail_dispatch($node) {
287 global $user, $base_url;
288
289 $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'))));
291 $roles = $node->mail_roles_selected;
292 if (empty($roles)) {
293 $roles = array();
294 }
295 $roles_where = array();
296 // If 'authenticated user' role is selected, we don't filter by role as we're sending to all roles.
297 if (!in_array(DRUPAL_AUTHENTICATED_RID, $roles)) {
298 foreach ($roles as $rid) {
299 $roles_where[] = 's.rid = ' . $rid;
300 }
301 $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);
303 }
304 else {
305 $result = db_query('SELECT uid FROM {users} WHERE status != 0');
306 }
307 $success = TRUE;
308 while ($uid = db_fetch_object($result)) {
309 $account = user_load(array('uid' => $uid->uid, 'status' => 1));
310 if (empty($account->mail_accept) && !($node->mail_override_opt && user_access('administer users'))) {
311 //continue;
312 }
313 if ($account->mail) {
314 $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', 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);
320 if ($node->mail_format == 'plain') {
321 $node->message = trim(strip_tags($message));
322 }
323 else {
324 $node->message = theme('mail_message', $node);
325 }
326 if (!mail_send($node)) {
327 $success = FALSE;
328 }
329 }
330 }
331 }
332
333 /**
334 * Send mail
335 *
336 * @param object $mail
337 */
338 function mail_send($mail) {
339 $sender['name'] = $mail->from_name;
340 $sender['mail'] = $mail->from_address;
341
342 $headers = array();
343
344 if ($mail->mail_receipt) {
345 $headers["Disposition-Notification-To"] = $mail->from_address;
346 $headers["X-Confirm-Reading-To"] = $mail->from_address;
347 }
348 if ($mail->mail_priority) {
349 switch($mail->mail_priority) {
350 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 reset($headers);
378 $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 /**
385 * Generate an array of role names
386 */
387 function _mail_get_roles_names($roles = array()) {
388 $roles_names = array();
389 if (count($roles)) {
390 $result = db_query('SELECT name FROM {role} WHERE rid in (' . implode(',', $roles) . ') ORDER BY name');
391 while ($role = db_fetch_object($result)) {
392 $roles_names[] = $role->name;
393 }
394 }
395 return $roles_names;
396 }

  ViewVC Help
Powered by ViewVC 1.1.2