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

Contents of /contributions/modules/pmail/pmail.module

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


Revision 1.3 - (show annotations) (download) (as text)
Tue Dec 4 15:54:02 2007 UTC (23 months, 3 weeks ago) by smk
Branch: MAIN
CVS Tags: DRUPAL-5--1-0, HEAD
Branch point for: DRUPAL-5
Changes since 1.2: +3 -3 lines
File MIME type: text/x-php
- Fixed fallback when token replacment didn't yield a result.
- Added CHANGELOG.
1 <?php
2 // $Id: pmail.module,v 1.2 2007/08/31 22:55:17 smk Exp $
3
4 /**
5 * @file
6 * This module allows you to personalize all emails sent from your site by
7 * enhancing the From and Reply-To email addresses with display names in the
8 * form "Some Name <foo@example.com>".
9 * This is done in a fully configurable way by relying on the token module to
10 * provide on-the-fly substitutions, and contains a fall back on the user's
11 * login name if the token replacement did't yield a result (for example
12 * because a user didn't fill in the name on its profile).
13 */
14
15 /**
16 * Implementation of hook_help().
17 */
18 function pmail_help($section) {
19 switch ($section) {
20 case 'admin/modules#description':
21 return t('Provides system-wide personalized display names for email addresses.');
22 }
23 }
24
25 /**
26 * Implementation of hook_menu().
27 */
28 function pmail_menu($may_cache) {
29 $items = array();
30
31 if ($may_cache) {
32 $items[] = array(
33 'path' => 'admin/user/pmail',
34 'title' => t('Personalized e-mails'),
35 'description' => t('Manage personalized mail settings.'),
36 'callback' => 'drupal_get_form',
37 'callback arguments' => 'pmail_settings',
38 'access' => user_access('administer site configuration'),
39 'type' => MENU_NORMAL_ITEM,
40 );
41 }
42
43 return $items;
44 }
45
46 /**
47 * Menu callback; generates the settings form.
48 */
49 function pmail_settings() {
50 $form['pmail_pattern'] = array(
51 '#type' => 'textfield',
52 '#title' => t('Display name'),
53 '#default_value' => variable_get('pmail_pattern', '[user]'),
54 '#description' => t('Type the pattern to construct display names from. Use the syntax [token] to insert a replacement pattern.'),
55 );
56
57 $form['token_help'] = array(
58 '#title' => t('Replacement patterns'),
59 '#type' => 'fieldset',
60 '#collapsible' => TRUE,
61 '#collapsed' => TRUE,
62 );
63 $form['token_help']['help'] = array(
64 '#value' => theme('token_help', 'user'),
65 );
66
67 $form['pmail_static_mappings'] = array(
68 '#type' => 'textarea',
69 '#title' => t('Static display name mappings'),
70 '#default_value' => variable_get('pmail_static_mappings', _pmail_static_defaults()),
71 '#description' => t('Type in special static replacements in the form <em>email, replacement</em>. Separate multiple entries with newlines. The default entry maps the site email (@site-mail) to the site name (@site-name).', array('@site-mail' => '<em>'. variable_get('site_mail', ini_get('sendmail_from')) .'</em>', '@site-name' => '<em>'. variable_get('site_name', t('Drupal')) .' &lt;'. variable_get('site_mail', ini_get('sendmail_from')) .'&gt;</em>')),
72 );
73
74 return system_settings_form($form);
75 }
76
77 /**
78 * Implementation of hook_mail_alter().
79 *
80 * Enance From and Reply-To addresses with display names in the form
81 * Some Name <foo@example.com>.
82 */
83 function pmail_mail_alter($mailkey, $to, $subject, $body, &$from, &$headers) {
84 $from = personalize_email($from);
85 $headers['From'] = personalize_email($headers['From']);
86 $headers['Reply-To'] = personalize_email($headers['Reply-To']);
87 }
88
89 /**
90 * Enhance an email address with a display name as configured in the settings.
91 *
92 * @param $mail
93 * The email address to enhance.
94 * @return string
95 * The personalized email address.
96 */
97 function personalize_email($mail) {
98 global $user;
99 static $emails, $statics;
100
101 if (!$mail) {
102 return $mail;
103 }
104
105 if (!isset($emails[$mail])) {
106 if (_pmail_contains_display_name($mail)) {
107 // Already contains display name
108 $emails[$mail] = $mail;
109 }
110 else {
111 $name = '';
112
113 // Check if email matches a special entry
114 if (!isset($statics)) {
115 $_statics = variable_get('pmail_static_mappings', _pmail_static_defaults());
116 $_statics = str_replace(array("\r\n", "\r"), "\n", $_statics);
117 foreach (array_filter(explode("\n", $_statics)) as $line) {
118 list($s_mail, $s_name) = explode(',', $line);
119 // The name may be empty, the email must not!
120 $s_mail = trim($s_mail);
121 if ($s_mail) {
122 $statics[$s_mail] = trim($s_name);
123 }
124 }
125 }
126
127 if (isset($statics[$mail])) {
128 // Found a static mapping
129 $name = $statics[$mail];
130 }
131 else {
132 // Check to see if the email belongs to the current user
133 if ($user->mail == $mail) {
134 $account = $user;
135 }
136 else {
137 // Try to load the corresponding user
138 $account = user_load(array('mail' => $mail));
139 }
140 if ($account) {
141 // Perform pattern replacement
142 $pattern = variable_get('pmail_pattern', '[user]');
143 $name = trim(token_replace($pattern, 'user', $account));
144
145 // If the token replacement didn't yield a result, fall back on the
146 // user's login name
147 if ($name == '' || $name == $pattern) {
148 $name = $account->name;
149 }
150 }
151 }
152
153 if ($name != '') {
154 $emails[$mail] = $name .' <'. $mail .'>';
155 }
156 else {
157 $emails[$mail] = $mail;
158 }
159 }
160 }
161
162 return $emails[$mail];
163 }
164
165 /**
166 * Checks whether an email address already contains a display name in the form
167 * Some Name <foo@example.com>.
168 *
169 * @param $mail
170 * The email address to check.
171 * @return boolean
172 * Whether the email already contains a display name part.
173 */
174 function _pmail_contains_display_name($mail) {
175 $user = '[a-zA-Z0-9_\-\.\+\^!#\$%&*+\/\=\?\`\|\{\}~\']+';
176 $domain = '(?:(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.?)+';
177 $ipv4 = '[0-9]{1,3}(\.[0-9]{1,3}){3}';
178 $ipv6 = '[0-9a-fA-F]{1,4}(\:[0-9a-fA-F]{1,4}){7}';
179
180 return preg_match("/^(.*) $user@($domain|(\[($ipv4|$ipv6)\]))$/", trim($mail));
181 }
182
183 /**
184 * Return default static name mappings.
185 *
186 * @return string
187 * The default mapping contains one mapping: site email => site name.
188 */
189 function _pmail_static_defaults() {
190 return variable_get('site_mail', ini_get('sendmail_from')) .', '. variable_get('site_name', t('Drupal'));
191 }
192

  ViewVC Help
Powered by ViewVC 1.1.2