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

Contents of /contributions/modules/smtp/smtp.module

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


Revision 1.23 - (show annotations) (download) (as text)
Thu Jul 17 15:06:14 2008 UTC (16 months, 1 week ago) by oadaeh
Branch: MAIN
CVS Tags: HEAD
Changes since 1.22: +25 -4 lines
File MIME type: text/x-php
#281599 by jcwatson11: Added from e-mail address validation and parsing.
1 <?php
2 // $Id: smtp.module,v 1.22 2008/07/12 17:03:10 oadaeh Exp $
3
4 /**
5 * @file
6 * Enables Drupal to send e-mail directly to an SMTP server.
7 *
8 * This module uses the PHPMailer class, originally by Brent R. Matzelle, now
9 * maintained by Codeworx Tech.
10 *
11 * Overriding mail handling in Drupal requires setting the smtp_library
12 * variable with the filename of a file containing a drupal_mail_wrapper()
13 * function. This module sets the smtp_library value to point back to this
14 * file which contains the drupal_mail_wrapper() function that uses the
15 * PHPMailer class to send e-mail instead of the PHP mail() function.
16 *
17 * @link http://phpmailer.codeworxtech.com/
18 * @link http://sourceforge.net/projects/phpmailer/
19 */
20
21 /**
22 * Implementation of hook_help().
23 */
24 function smtp_help($path, $arg) {
25 switch ($path) {
26 case 'admin/help#smtp':
27 return t('Allows the sending of site e-mail through an SMTP server of your choice.');
28 }
29 } // End of smtp_help().
30
31
32
33 /**
34 * Implementation of hook_menu().
35 */
36 function smtp_menu() {
37 $items['admin/settings/smtp'] = array(
38 'title' => 'SMTP Authentication Support',
39 'page callback' => 'drupal_get_form',
40 'page arguments' => array('smtp_admin_settings'),
41 'access arguments' => array('administer site configuration'),
42 'description' => 'Allows the sending of site e-mail through an SMTP server of your choice.',
43 );
44
45 return $items;
46 } // End of smtp_menu().
47
48
49
50 /**
51 * Administrative settings.
52 *
53 * @return
54 * An array containing form items to place on the module settings page.
55 */
56 function smtp_admin_settings() {
57 // Override the smtp_library variable.
58 if (variable_get('smtp_on', 0)) {
59 $smtp_path = drupal_get_filename('module', 'smtp');
60 if ($smtp_path) {
61 variable_set('smtp_library', $smtp_path);
62 drupal_set_message(t('SMTP.module is active.'));
63 }
64 // If drupal can't find the path to the module, display an error.
65 else {
66 drupal_set_message(t("SMTP.module error: Can't find file."), 'error');
67 }
68 }
69 // If this module is turned off, delete the variable.
70 else {
71 variable_del('smtp_library');
72 drupal_set_message(t('SMTP.module is INACTIVE.'));
73 }
74
75 $form['onoff'] = array(
76 '#type' => 'fieldset',
77 '#title' => t('Install options'),
78 );
79 $form['onoff']['smtp_on'] = array(
80 '#type' => 'radios',
81 '#title' => t('Turn this module on or off'),
82 '#default_value' => variable_get('smtp_on', 0),
83 '#options' => array(1 => t('On'), 0 => t('Off')),
84 '#description' => t('To uninstall this module you must turn it off here first.'),
85 );
86
87 $form['server'] = array(
88 '#type' => 'fieldset',
89 '#title' => t('SMTP server settings'),
90 );
91 $form['server']['smtp_host'] = array(
92 '#type' => 'textfield',
93 '#title' => t('SMTP server'),
94 '#default_value' => variable_get('smtp_host', ''),
95 '#description' => t('The address of your outgoing SMTP server.'),
96 );
97 $form['server']['smtp_hostbackup'] = array(
98 '#type' => 'textfield',
99 '#title' => t('SMTP backup server'),
100 '#default_value' => variable_get('smtp_hostbackup', ''),
101 '#description' => t('The address of your outgoing SMTP backup server. If the primary server can\'t be found this one will be tried. This is optional.'),
102 );
103 $form['server']['smtp_port'] = array(
104 '#type' => 'textfield',
105 '#title' => t('SMTP port'),
106 '#size' => 6,
107 '#maxlength' => 6,
108 '#default_value' => variable_get('smtp_port', '25'),
109 '#description' => t('The default SMTP port is 25, if that is being blocked try 80. Gmail uses 465. See !url for more information on configuring for use with Gmail.', array('!url' => l(t('this page'), 'http://gmail.google.com/support/bin/answer.py?answer=13287'))),
110 );
111 // Only display the option if openssl is installed.
112 if (function_exists('openssl_open')) {
113 $encryption_options = array(
114 'standard' => t('No'),
115 'ssl' => t('Use SSL'),
116 'tls' => t('Use TLS'),
117 );
118 $encryption_description = t('This allows connection to an SMTP server that requires SSL encryption such as Gmail.');
119 }
120 // If openssl is not installed, use normal protocol.
121 else {
122 variable_set('smtp_protocol', 'standard');
123 $encryption_options = array('standard' => t('No'));
124 $encryption_description = t('Your PHP installation does not have SSL enabled. See the !url page on php.net for more information. Gmail requires SSL.', array('!url' => l(t('OpenSSL Functions'), 'http://php.net/openssl')));
125 }
126 $form['server']['smtp_protocol'] = array(
127 '#type' => 'select',
128 '#title' => t('Use encrypted protocol'),
129 '#default_value' => variable_get('smtp_protocol', 'standard'),
130 '#options' => $encryption_options,
131 '#description' => $encryption_description,
132 );
133
134 $form['auth'] = array(
135 '#type' => 'fieldset',
136 '#title' => t('SMTP Authentication'),
137 '#description' => t('Leave blank if your SMTP server does not require authentication.'),
138 );
139 $form['auth']['smtp_username'] = array(
140 '#type' => 'textfield',
141 '#title' => t('Username'),
142 '#default_value' => variable_get('smtp_username', ''),
143 '#description' => t('SMTP Username.'),
144 );
145 $form['auth']['smtp_password'] = array(
146 '#type' => 'textfield',
147 '#title' => t('Password'),
148 '#default_value' => variable_get('smtp_password', ''),
149 '#description' => t('SMTP password.'),
150 );
151
152 $form['email_options'] = array(
153 '#type' => 'fieldset',
154 '#title' => t('E-mail options'),
155 );
156 $form['email_options']['smtp_from'] = array(
157 '#type' => 'textfield',
158 '#title' => t('E-mail from address'),
159 '#default_value' => variable_get('smtp_from', ''),
160 '#description' => t('The e-mail address that all e-mails will be from.'),
161 );
162 $form['email_options']['smtp_fromname'] = array(
163 '#type' => 'textfield',
164 '#title' => t('E-mail from name'),
165 '#default_value' => variable_get('smtp_fromname', ''),
166 '#description' => t('The name that all e-mails will be from. If left blank will use the site name of: ') . variable_get('site_name', 'Drupal powered site'),
167 );
168
169 // If an address was given, send a test e-mail message.
170 $test_address = variable_get('smtp_test_address', '');
171 if ($test_address != '') {
172 // Clear the variable so only one message is sent.
173 variable_del('smtp_test_address');
174 global $language;
175 $params['subject'] = t('Drupal test e-mail');
176 $params['body'] = t('If you receive this message it means your site is capable of sending e-mail.');
177 drupal_mail('smtp', 'smtp-test', $test_address, $language, $params);
178 drupal_set_message(t('A test e-mail has been sent to @email. You may want to !check for any error messages.', array('@email' => $test_address, '!check' => l(t('check the logs'), 'admin/reports/watchdog'))));
179 }
180 $form['email_test'] = array(
181 '#type' => 'fieldset',
182 '#title' => t('Send test e-mail'),
183 );
184 $form['email_test']['smtp_test_address'] = array(
185 '#type' => 'textfield',
186 '#title' => t('E-mail address to send a test e-mail to'),
187 '#default_value' => '',
188 '#description' => t('Type in an address to have a test e-mail sent there.'),
189 );
190
191 $form['smtp_debugging'] = array(
192 '#type' => 'checkbox',
193 '#title' => t('Enable debugging'),
194 '#default_value' => variable_get('smtp_debugging', 0),
195 '#description' => t('Checking this box will print SMTP messages from the server for every e-mail that is sent.'),
196 );
197
198 return system_settings_form($form);
199 } // End of smtp_admin_settings().
200
201
202
203 /**
204 * Validataion for the administrative settings form.
205 *
206 * @param form
207 * An associative array containing the structure of the form.
208 * @param form_state
209 * A keyed array containing the current state of the form.
210 */
211 function smtp_admin_settings_validate($form, &$form_state) {
212 if ($form_state['values']['smtp_on'] == 1 && $form_state['values']['smtp_host'] == '') {
213 form_set_error('smtp_host', t('You must enter an SMTP server address.'));
214 }
215
216 if ($form_state['values']['smtp_on'] == 1 && $form_state['values']['smtp_port'] == '') {
217 form_set_error('smtp_port', t('You must enter an SMTP port number.'));
218 }
219
220 if ($form_state['values']['smtp_from'] && !valid_email_address($form_state['values']['smtp_from'])) {
221 form_set_error('smtp_from', t('The provided from e-mail address is not valid.'));
222 }
223 } // End of smtp_admin_settings_validate().
224
225
226
227 /**
228 * Sends out the e-mail.
229 *
230 * @param message
231 * An array with at least the following elements: id, to, subject, body and
232 * headers.
233 *
234 * @see http://api.drupal.org/api/function/drupal_mail_send/6
235 */
236 function drupal_mail_wrapper($message) {
237 $id = $message['id'];
238 $to = $message['to'];
239 $from = $message['from'];
240 $header = $message['headers'];
241 $subject = $message['subject'];
242 $body = $message['body'];
243
244 // Include the PHPMailer class (which includes the SMTP class).
245 require_once(drupal_get_path('module', 'smtp') .'/phpmailer/class.phpmailer.php');
246
247 // Create a new PHPMailer object.
248 $mail = new PHPMailer();
249
250 global $language;
251 if ($language) {
252 $mail->SetLanguage($language->language, drupal_get_path('module', 'smtp') .'/phpmailer/language/');
253 }
254
255 if (variable_get('smtp_debugging', 0) == 1) {
256 $mail->SMTPDebug = TRUE;
257 }
258
259 $username = variable_get('smtp_username', '');
260 $password = variable_get('smtp_password', '');
261
262 // Set the default name e-mails should be from.
263 // If value is not defined in settings, use site_name.
264 if (variable_get('smtp_fromname', '') != '') {
265 $from_name = variable_get('smtp_fromname', '');
266 }
267 else {
268 // Blank value will let the e-mail address appear.
269 $from_name = variable_get('site_name', '');
270 }
271
272 // If from e-mail address is blank, use smtp_from config option.
273 if ($from == NULL || $from == '') {
274 if (variable_get('smtp_from', '') != '') {
275 $from = variable_get('smtp_from', '');
276 }
277 else {
278 // If smtp_from config option is blank, use site_email.
279 $from = variable_get('site_email', '');
280 }
281 }
282
283 if (preg_match('/^".*"\s*<.*>$/', $from)) {
284 $from_name = preg_replace('/"(.*)"(.*)/i', '$1', $from); // It gives: Name
285 $from = preg_replace("/(.*)\<(.*)\>/i", '$2', $from); // It gives: name@domain.tld
286 }
287 else if (!valid_email_address($from)) {
288 $from_error_message = t('The submitted from address (@from) is not valid.', array('@from' => $from));
289 drupal_set_message($from_error_message, 'error');
290 watchdog('smtp', $from_error_message, WATCHDOG_ERROR);
291 return false;
292 }
293
294 // Defines the From value to what we expect.
295 // This should be done correctly by PHPMailer now.
296 // $mail->From = '"'. $from_name .'" <'. $from .'>';
297 $mail->From = $from;
298 $mail->FromName = $from_name;
299 $mail->Sender = $from;
300
301 // Decide whether to use SMTP Authorization.
302 if ($username != '' and $password != '') {
303 // Do so if username and password are given.
304 $auth = TRUE;
305 }
306 else {
307 $auth = FALSE;
308 }
309
310 // Force the initial value to be set to text/plain.
311 $mail->IsHTML(FALSE);
312
313 // Take care of the e-mail headers.
314 foreach ($header as $key => $value) {
315 //watchdog('error', 'Key: ' . $key . ' Value: ' . $value);
316 if (drupal_strtolower($key) == 'from') {
317 if ($from == NULL or $from == '') {
318 // If a from value was already given, then set based on header.
319 // Should be the most common situation since drupal_mail moves the
320 // from to headers.
321 $from = $value;
322 $mail->From = $value;
323 // then from can be out of sync with from_name !
324 $mail->FromName = '';
325 $mail->Sender = $value;
326 }
327 }
328 else if (drupal_strtolower($key) == 'content-type' && strpos(drupal_strtolower($value), 'text/html') !== FALSE) {
329 $mail->IsHTML(TRUE);
330 }
331 else if (drupal_strtolower($key) == 'content-type' && strpos(drupal_strtolower($value), 'multipart/mixed') !== FALSE) {
332 // $body passed to smtp should already be formatted. Add multipart
333 // header and tell phpmailer to leave it alone
334 $mail->AddCustomHeader($key .': '. $value);
335 $mail->message_type = "pre";
336 $mail->ContentType = 'multipart/mixed';
337 }
338 else if (drupal_strtolower($key) == 'reply-to') {
339 // Only add a "reply-to" if it's not the same as "return-path".
340 if ($value != $header['Return-Path']) {
341 $mail->AddReplyTo($value);
342 }
343 }
344 else if (drupal_strtolower($key) == 'return-path') {
345 if (trim($value) != '') {
346 // This is be set by SmtpSend()
347 // $mail->Sender = $value;
348 }
349 }
350 else if (drupal_strtolower($key) == 'content-transfer-encoding') {
351 $mail->Encoding = $value;
352 }
353 else if (drupal_strtolower($key) == 'mime-version') {
354 // Ommit MIME-Version, since it will be set by PHPMailer.
355 }
356 else if (drupal_strtolower($key) == 'x-mailer') {
357 // Ommit X-Mailer, since it will be set by PHPMailer.
358 }
359 else if (drupal_strtolower($key) == 'errors-to') {
360 // Prefer to keep control on this one, it will be set by PHPMailer.
361 }
362 else if (drupal_strtolower($key) == 'bcc') {
363 $bccrecipients = split(",", $value);
364 foreach ($bccrecipients as $bccrecipient) {
365 if (strpos($bccrecipient, '<') !== false) {
366 $bccparts = explode(" <", $bccrecipient);
367 $bccname = $bccparts[0];
368 $bccaddr = rtrim($bccparts[1], ">");
369 }
370 else {
371 $bccname = "";
372 $bccaddr = $bccrecipient;
373 }
374 $mail->AddBCC($bccaddr, $bccname);
375 }
376 }
377 // Else the header key is not special.
378 else {
379 // Add header line.
380 $mail->AddCustomHeader($key .': '. $value);
381 }
382 }
383
384
385 // Set the correct protocol prefix to append to the smtp host.
386 switch (variable_get('smtp_protocol', 'standard')) {
387 case "ssl":
388 $mail->SMTPSecure = 'ssl';
389 break;
390 case "tls":
391 $mail->SMTPSecure = 'tls';
392 break;
393 case "standard":
394 $mail->SMTPSecure = '';
395 }
396
397 $mail->Host = variable_get('smtp_host', '') .';'. variable_get('smtp_hostbackup', '');
398 $mail->Port = variable_get('smtp_port', '25');
399 $mail->Mailer = 'smtp';
400 $mail->SMTPAuth = $auth;
401 $mail->Username = $username;
402 $mail->Password = $password;
403 $mail->CharSet = 'utf-8';
404 $mail->AddCustomHeader('Errors-To: '. $from);
405
406 $torecipients = split(',', $to);
407 foreach ($torecipients as $torecipient) {
408 if (strpos($torecipient, '<') !== false) {
409 $toparts = explode(' <', $torecipient);
410 $toname = $toparts[0];
411 $toaddr = rtrim($toparts[1], '>');
412 }
413 else {
414 $toname = '';
415 $toaddr = $torecipient;
416 }
417 $mail->AddAddress($toaddr, $toname);
418 }
419
420 $mail->Subject = $subject;
421 $mail->Body = $body;
422
423 watchdog('smtp', 'Sending mail to: @to', array('@to' => $to));
424
425 // Try to send e-mail. If it fails, set watchdog entry.
426 if (!$mail->Send()) {
427 watchdog('smtp', 'Error sending e-mail from @from to @to : !error_message', array('@from' => $from, '@to' => $to, '!error_message' => $mail->ErrorInfo), WATCHDOG_ERROR);
428 return false;
429 }
430
431 $mail->SmtpClose();
432 return true;
433 } // End of drupal_mail_wrapper().
434
435
436
437 /**
438 * Implementation of hook_mail().
439 */
440 function smtp_mail($key, &$message, $params) {
441 if ($key == 'smtp-test') {
442 $message['subject'] = $params['subject'];
443 $message['body'] = $params['body'];
444 }
445 } // End of smtp_mail().

  ViewVC Help
Powered by ViewVC 1.1.2