source: trunk/include/functions_mail.inc.php @ 25357

Last change on this file since 25357 was 25357, checked in by mistic100, 11 years ago

feature 2995: New email template
rewrite pwg_mail_group() and pwg_mail_notification_admins()
new function pwg_mail_admins()
add complete template management in pwg_mail()
TODO : font-size problem in Thunderbird

  • Property svn:eol-style set to LF
File size: 21.9 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2013 Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24/**
25 * Returns the name of the mail sender
26 * @return string
27 */
28function get_mail_sender_name()
29{
30  global $conf;
31
32  return (empty($conf['mail_sender_name']) ? $conf['gallery_title'] : $conf['mail_sender_name']);
33}
34
35/**
36 * Returns the email of the mail sender
37 * @since 2.6
38 * @return string
39 */
40function get_mail_sender_email()
41{
42  global $conf;
43
44  return (empty($conf['mail_sender_email']) ? get_webmaster_mail_address() : $conf['mail_sender_email']);
45}
46
47/**
48 * Returns an array of mail configuration parameters :
49 * - send_bcc_mail_webmaster
50 * - mail_allow_html
51 * - use_smtp
52 * - smtp_host
53 * - smtp_user
54 * - smtp_password
55 * - smtp_secure
56 * - email_webmaster
57 * - name_webmaster
58 *
59 * @return array
60 */
61function get_mail_configuration()
62{
63  global $conf;
64
65  $conf_mail = array(
66    'send_bcc_mail_webmaster' => $conf['send_bcc_mail_webmaster'],
67    'mail_allow_html' => $conf['mail_allow_html'],
68    'mail_theme' => $conf['mail_theme'],
69    'use_smtp' => !empty($conf['smtp_host']),
70    'smtp_host' => $conf['smtp_host'],
71    'smtp_user' => $conf['smtp_user'],
72    'smtp_password' => $conf['smtp_password'],
73    'smtp_secure' => $conf['smtp_secure'],
74    'email_webmaster' => get_mail_sender_email(),
75    'name_webmaster' => get_mail_sender_name(),
76    );
77
78  return $conf_mail;
79}
80
81/**
82 * Returns an email address with an associated real name
83 * @param string name
84 * @param string email
85 */
86function format_email($name, $email)
87{
88  $cvt_email = trim(preg_replace('#[\n\r]+#s', '', $email));
89  $cvt_name = trim(preg_replace('#[\n\r]+#s', '', $name));
90
91  if ($cvt_name!="")
92  {
93    $cvt_name = '"'.addcslashes($cvt_name,'"').'"'.' ';
94  }
95
96  if (strpos($cvt_email, '<') === false)
97  {
98    return $cvt_name.'<'.$cvt_email.'>';
99  }
100  else
101  {
102    return $cvt_name.$cvt_email;
103  }
104}
105
106/**
107 * Returns the mail and the name from a formatted address
108 * @since 2.6
109 * @param string|array $input
110 * @return array
111 */
112function unformat_email($input)
113{
114  if (is_array($input))
115  {
116    return $input;
117  }
118
119  if (preg_match('/(.*)<(.*)>.*/', $input, $matches))
120  {
121    return array(
122      'email' => trim($matches[2]),
123      'name' => trim($matches[1]),
124      );
125  }
126  else
127  {
128    return array(
129      'email' => trim($input),
130      'name' => '',
131      );
132  }
133}
134 
135
136/**
137 * Returns an email address list with minimal email string
138 * @param string $email_list - comma separated
139 * @return string
140 */
141function get_strict_email_list($email_list)
142{
143  $result = array();
144  $list = explode(',', $email_list);
145
146  foreach ($list as $email)
147  {
148    if (strpos($email, '<') !== false)
149    {
150       $email = preg_replace('/.*<(.*)>.*/i', '$1', $email);
151    }
152    $result[] = trim($email);
153  }
154
155  return implode(',', array_unique($result));
156}
157
158
159/**
160 * Return an new mail template
161 * @param string $email_format - text/html or text/plain
162 * @return Template
163 */
164function &get_mail_template($email_format)
165{
166  $template = new Template(PHPWG_ROOT_PATH.'themes', 'default', 'template/mail/'.$email_format);
167  return $template;
168}
169
170/**
171 * Switch language to specified language
172 * All entries are push on language stack
173 * @param string $language
174 */
175function switch_lang_to($language)
176{
177  global $switch_lang, $user, $lang, $lang_info, $language_files;
178
179  // explanation of switch_lang
180  // $switch_lang['language'] contains data of language
181  // $switch_lang['stack'] contains stack LIFO
182  // $switch_lang['initialisation'] allow to know if it's first call
183
184  // Treatment with current user
185  // Language of current user is saved (it's considered OK on firt call)
186  if (!isset($switch_lang['initialisation']) and !isset($switch_lang['language'][$user['language']]))
187  {
188    $switch_lang['initialisation'] = true;
189    $switch_lang['language'][$user['language']]['lang_info'] = $lang_info;
190    $switch_lang['language'][$user['language']]['lang'] = $lang;
191  }
192
193  // Change current infos
194  $switch_lang['stack'][] = $user['language'];
195  $user['language'] = $language;
196
197  // Load new data if necessary
198  if (!isset($switch_lang['language'][$language]))
199  {
200    // Re-Init language arrays
201    $lang_info = array();
202    $lang  = array();
203
204    // language files
205    load_language('common.lang', '', array('language'=>$language) );
206    // No test admin because script is checked admin (user selected no)
207    // Translations are in admin file too
208    load_language('admin.lang', '', array('language'=>$language) );
209   
210    // Reload all plugins files (see load_language declaration)
211    if (!empty($language_files))
212    {
213      foreach ($language_files as $dirname => $files)
214        foreach ($files as $filename)
215          load_language($filename, $dirname, array('language'=>$language) );
216    }
217   
218    trigger_action('loading_lang');
219    load_language('lang', PHPWG_ROOT_PATH.PWG_LOCAL_DIR,
220      array('language'=>$language, 'no_fallback'=>true, 'local'=>true)
221    );
222
223    $switch_lang['language'][$language]['lang_info'] = $lang_info;
224    $switch_lang['language'][$language]['lang'] = $lang;
225  }
226  else
227  {
228    $lang_info = $switch_lang['language'][$language]['lang_info'];
229    $lang = $switch_lang['language'][$language]['lang'];
230  }
231}
232
233/**
234 * Switch back language pushed with switch_lang_to function
235 */
236function switch_lang_back()
237{
238  global $switch_lang, $user, $lang, $lang_info;
239
240  if (count($switch_lang['stack']) > 0)
241  {
242    // Get last value
243    $language = array_pop($switch_lang['stack']);
244
245    // Change current infos
246    if (isset($switch_lang['language'][$language]))
247    {
248      $lang_info = $switch_lang['language'][$language]['lang_info'];
249      $lang = $switch_lang['language'][$language]['lang'];
250    }
251    $user['language'] = $language;
252  }
253}
254
255/**
256 * Send a notification email to all administrators
257 * current user (if admin) is not notified
258 * @param string $subject
259 * @param string $content
260 * @param boolean $send_technical_details - send user IP and browser
261 * @return boolean
262 */
263function pwg_mail_notification_admins($subject, $content, $send_technical_details=true)
264{
265  if (empty($subject) or empty($content))
266  {
267    return false;
268  }
269 
270  // for backward compatibility < 2.6
271  if (is_array($subject))
272  {
273    $subject = l10n_args($subject);
274  }
275  if (is_array($content))
276  {
277    $content = l10n_args($content);
278  }
279
280  global $conf, $user;
281 
282  $tpl_vars = array();
283  if ($send_technical_details)
284  {
285    $tpl_vars['TECHNICAL'] = 
286      l10n('Connected user: %s', stripslashes($user['username'])) ."\n".
287      l10n('IP: %s', $_SERVER['REMOTE_ADDR']) . "\n" .
288      l10n('Browser: %s', $_SERVER['HTTP_USER_AGENT']);
289  }
290
291  return pwg_mail_admins(
292    array(
293      'subject' => '['. $conf['gallery_title'] .'] '. $subject,
294      'mail_title' => $conf['gallery_title'],
295      'mail_subtitle' => $subject,
296      'content' => $content,
297      'content_format' => 'text/plain',
298      ),
299    array(
300      'filename' => 'notification_admin',
301      'assign' => $tpl_vars,
302      )
303    );
304}
305
306/**
307 * Send a email to all administrators
308 * current user (if admin) is excluded
309 * @see pwg_mail()
310 *
311 * @param array $args - as in pwg_mail()
312 * @param array $tpl - as in pwg_mail()
313 * @return boolean
314 */
315function pwg_mail_admins($args=array(), $tpl=array())
316{
317  if (empty($args['content']) and empty($tpl))
318  {
319    return false;
320  }
321
322  global $conf, $user;
323  $return = true;
324
325  // get admins (except ourself)
326  $query = '
327SELECT
328    u.'.$conf['user_fields']['username'].' AS name,
329    u.'.$conf['user_fields']['email'].' AS email
330  FROM '.USERS_TABLE.' AS u
331    JOIN '.USER_INFOS_TABLE.' AS i
332    ON i.user_id =  u.'.$conf['user_fields']['id'].'
333  WHERE i.status in (\'webmaster\',  \'admin\')
334    AND u.'.$conf['user_fields']['email'].' IS NOT NULL
335    AND i.user_id <> '.$user['id'].'
336  ORDER BY username
337;';
338  $admins = array_from_query($query);
339
340  if (empty($admins))
341  {
342    return $return;
343  }
344
345  switch_lang_to(get_default_language());
346
347  $return = pwg_mail($admins, $args, $tpl);
348
349  switch_lang_back();
350
351  return $return;
352}
353
354/**
355 * Send an email to a group
356 * @see pwg_mail()
357 *
358 * @param int $group_id
359 * @param array $args - as in pwg_mail()
360 *    @option string language_selected - filters users of the group by language
361 * @param array $tpl - as in pwg_mail()
362 * @return boolean
363 */
364function pwg_mail_group($group_id, $args=array(), $tpl=array())
365{ 
366  if (empty($group_id) or ( empty($args['content']) and empty($tpl) ))
367  {
368    return false;
369  }
370
371  global $conf;
372  $return = true;
373
374  // get distinct languages of targeted users
375  $query = '
376SELECT DISTINCT language
377  FROM '.USER_GROUP_TABLE.' AS ug
378    INNER JOIN '.USERS_TABLE.' AS u
379    ON '.$conf['user_fields']['id'].' = ug.user_id
380    INNER JOIN '.USER_INFOS_TABLE.' AS ui
381    ON ui.user_id = ug.user_id
382  WHERE group_id = '.$group_id.'
383    AND '.$conf['user_fields']['email'].' <> ""';
384  if (!empty($args['language_selected']))
385  {
386    $query .= '
387    AND language = \''.$args['language_selected'].'\'';
388  }
389
390    $query .= '
391;';
392  $languages = array_from_query($query, 'language');
393
394  if (empty($languages))
395  {
396    return $return;
397  }
398
399  foreach ($languages as $language)
400  {
401    // get subset of users in this group for a specific language
402    $query = '
403SELECT
404    u.'.$conf['user_fields']['username'].' AS name,
405    u.'.$conf['user_fields']['email'].' AS email
406  FROM '.USER_GROUP_TABLE.' AS ug
407    INNER JOIN '.USERS_TABLE.' AS u
408    ON '.$conf['user_fields']['id'].' = ug.user_id
409    INNER JOIN '.USER_INFOS_TABLE.' AS ui
410    ON ui.user_id = ug.user_id
411  WHERE group_id = '.$group_id.'
412    AND '.$conf['user_fields']['email'].' <> ""
413    AND language = \''.$language.'\'
414;';
415    $users = array_from_query($query);
416
417    if (empty($users))
418    {
419      continue;
420    }
421
422    switch_lang_to($language);
423
424    $return&= pwg_mail(null,
425      array_merge(
426        $args,
427        array('Bcc' => $users)
428        ),
429      $tpl
430      );
431
432    switch_lang_back();
433  }
434
435  return $return;
436}
437
438/**
439 * sends an email, using Piwigo specific informations
440 *
441 * @param string|string[] $to
442 * @param array $args
443 *       o from: sender [default value webmaster email]
444 *       o Cc: array of carbon copy receivers of the mail. [default value empty]
445 *       o Bcc: array of blind carbon copy receivers of the mail. [default value empty]
446 *       o subject [default value 'Piwigo']
447 *       o content: content of mail [default value '']
448 *       o content_format: format of mail content [default value 'text/plain']
449 *       o email_format: global mail format [default value $conf_mail['default_email_format']]
450 *       o theme: theme to use [default value $conf_mail['mail_theme']]
451 *       o mail_title: main title of the mail [default value $conf['gallery_title']]
452 *       o mail_subtitle: subtitle of the mail [default value subject]
453 * @param array $tpl - use these options to define a custom content template file
454 *       o filename
455 *       o dirname (optional)
456 *       o assign (optional)
457 *
458 * @return boolean
459 */
460function pwg_mail($to, $args=array(), $tpl=array())
461{
462  global $conf, $conf_mail, $lang_info, $page;
463
464  if (empty($to) and empty($args['Cc']) and empty($args['Bcc']))
465  {
466    return true;
467  }
468
469  if (!isset($conf_mail))
470  {
471    $conf_mail = get_mail_configuration();
472  }
473
474  include_once(PHPWG_ROOT_PATH.'include/phpmailer/class.phpmailer.php');
475
476  $mail = new PHPMailer;
477
478  $recipients = !is_array($to) ? explode(',', $to) : $to;
479  foreach ($recipients as $recipient)
480  {
481    $recipient = unformat_email($recipient);
482    $mail->addAddress($recipient['email'], $recipient['name']);
483  }
484
485  $mail->WordWrap = 76;
486  $mail->CharSet = 'UTF-8';
487 
488  // Compute root_path in order have complete path
489  set_make_full_url();
490
491  if (empty($args['from']))
492  {
493    $from = array(
494      'email' => $conf_mail['email_webmaster'],
495      'name' => $conf_mail['name_webmaster'],
496      );
497  }
498  else
499  {
500    $from = unformat_email($args['from']);
501  }
502  $mail->setFrom($from['email'], $from['name']);
503  $mail->addReplyTo($from['email'], $from['name']);
504
505  // Subject
506  if (empty($args['subject']))
507  {
508    $args['subject'] = 'Piwigo';
509  }
510  $args['subject'] = trim(preg_replace('#[\n\r]+#s', '', $args['subject']));
511  $mail->Subject = $args['subject'];
512
513  // Cc
514  if (!empty($args['Cc']))
515  {
516    foreach ($args['Cc'] as $cc)
517    {
518      $cc = unformat_email($cc);
519      $mail->addCC($cc['email'], $cc['name']);
520    }
521  }
522
523  // Bcc
524  if ($conf_mail['send_bcc_mail_webmaster'])
525  {
526    $args['Bcc'][] = get_webmaster_mail_address();
527  }
528  if (!empty($args['Bcc']))
529  {
530    foreach ($args['Bcc'] as $bcc)
531    {
532      $bcc = unformat_email($bcc);
533      $mail->addBCC($bcc['email'], $bcc['name']);
534    }
535  }
536
537  // theme
538  if (empty($args['theme']) or !in_array($args['theme'], array('clear','dark')))
539  {
540    $args['theme'] = $conf_mail['mail_theme'];
541  }
542
543  // content
544  if (!isset($args['content']))
545  {
546    $args['content'] = '';
547  }
548 
549  // try to decompose subject like "[....] ...."
550  if (!isset($args['mail_title']) and !isset($args['mail_subtitle']))
551  {
552    if (preg_match('#^\[(.*)\](.*)$#',  $args['subject'], $matches))
553    {
554      $args['mail_title'] = $matches[1];
555      $args['mail_subtitle'] = $matches[2];
556    }
557  }
558  if (!isset($args['mail_title']))
559  {
560    $args['mail_title'] = $conf['gallery_title'];
561  }
562  if (!isset($args['mail_subtitle']))
563  {
564    $args['mail_subtitle'] = $args['subject'];
565  }
566
567  // content type
568  if (empty($args['content_format']))
569  {
570    $args['content_format'] = 'text/plain';
571  }
572
573  $content_type_list = array();
574  if ($conf_mail['mail_allow_html'] and @$args['email_format'] != 'text/plain')
575  {
576    $content_type_list[] = 'text/html';
577  }
578  $content_type_list[] = 'text/plain';
579
580  $contents = array();
581  foreach ($content_type_list as $content_type)
582  {
583    // key compose of indexes witch allow to cache mail data
584    $cache_key = $content_type.'-'.$lang_info['code'];
585    $cache_key.= '-'.crc32(@$args['mail_title'] . @$args['mail_subtitle']); // TODO: find a way to not cache by mail_title
586
587    if (!isset($conf_mail[$cache_key]))
588    {
589      // instanciate a new Template
590      if (!isset($conf_mail[$cache_key]['theme']))
591      {
592        $conf_mail[$cache_key]['theme'] = get_mail_template($content_type);
593        trigger_action('before_parse_mail_template', $cache_key, $content_type);
594      }
595      $template = &$conf_mail[$cache_key]['theme'];
596
597      $template->set_filename('mail_header', 'header.tpl');
598      $template->set_filename('mail_footer', 'footer.tpl');
599
600      $template->assign(
601        array(
602          'GALLERY_URL' => get_gallery_home_url(),
603          'GALLERY_TITLE' => isset($page['gallery_title']) ? $page['gallery_title'] : $conf['gallery_title'],
604          'VERSION' => $conf['show_version'] ? PHPWG_VERSION : '',
605          'PHPWG_URL' => defined('PHPWG_URL') ? PHPWG_URL : '',
606          'CONTENT_ENCODING' => get_pwg_charset(),
607          'CONTACT_MAIL' => $conf_mail['email_webmaster'],
608          'MAIL_TITLE' => $args['mail_title'],
609          'MAIL_SUBTITLE' => $args['mail_subtitle'],
610          )
611        );
612
613      if ($content_type == 'text/html')
614      {
615        if ($template->smarty->template_exists('global-mail-css.tpl'))
616        {
617          $template->set_filename('css', 'global-mail-css.tpl');
618          $template->assign_var_from_handle('GLOBAL_MAIL_CSS', 'css');
619        }
620
621        if ($template->smarty->template_exists('mail-css-'. $args['theme'] .'.tpl'))
622        {
623          $template->set_filename('css', 'mail-css-'. $args['theme'] .'.tpl');
624          $template->assign_var_from_handle('MAIL_CSS', 'css');
625        }
626      }
627
628      $conf_mail[$cache_key]['header'] = $template->parse('mail_header', true);
629      $conf_mail[$cache_key]['footer'] = $template->parse('mail_footer', true);
630    }
631
632    // Header
633    $contents[$content_type] = $conf_mail[$cache_key]['header'];
634
635    // Content
636    // Stored in a temp variable, if a content template is used it will be assigned
637    // to the $CONTENT template variable, otherwise it will be appened to the mail
638    if ($args['content_format'] == 'text/plain' and $content_type == 'text/html')
639    {
640      // convert plain text to html
641      $mail_content =
642        '<p>'.
643        nl2br(
644          preg_replace(
645            '/(https?:\/\/([-\w\.]+[-\w])+(:\d+)?(\/([\w\/_\.\#-]*(\?\S+)?[^\.\s])?)?)/i',
646            '<a href="$1">$1</a>',
647            htmlspecialchars($args['content'])
648            )
649          ).
650        '</p>';
651    }
652    else if ($args['content_format'] == 'text/html' and $content_type == 'text/plain')
653    {
654      // convert html text to plain text
655      $mail_content = strip_tags($args['content']);
656    }
657    else
658    {
659      $mail_content = $args['content'];
660    }
661
662    // Runtime template
663    if (isset($tpl['filename']))
664    {
665      $template = &$conf_mail[$cache_key]['theme'];
666      if (isset($tpl['dirname']))
667      {
668        $template->set_template_dir($tpl['dirname'] .'/'. $content_type);
669      }
670      if ($template->smarty->template_exists($tpl['filename'] .'.tpl'))
671      {
672        $template->set_filename($tpl['filename'], $tpl['filename'] .'.tpl');
673        if (!empty($tpl['assign']))
674        {
675          $template->assign($tpl['assign']);
676        }
677        $template->assign('CONTENT', $mail_content);
678        $contents[$content_type].= $template->parse($tpl['filename'], true);
679      }
680      else
681      {
682        $contents[$content_type].= $mail_content;
683      }
684    }
685    else
686    {
687      $contents[$content_type].= $mail_content;
688    }
689
690    // Footer
691    $contents[$content_type].= $conf_mail[$cache_key]['footer'];
692  }
693
694  // Undo Compute root_path in order have complete path
695  unset_make_full_url();
696
697  // Send content to PHPMailer
698  if (isset($contents['text/html']))
699  {
700    $mail->isHTML(true);
701    $mail->Body = move_css_to_body($contents['text/html']);
702   
703    if (isset($contents['text/plain']))
704    {
705      $mail->AltBody = $contents['text/plain'];
706    }
707  }
708  else
709  {
710    $mail->isHTML(false);
711    $mail->Body = $contents['text/plain'];
712  }
713
714  if ($conf_mail['use_smtp'])
715  {
716    // now we need to split port number
717    if (strpos($conf_mail['smtp_host'], ':') !== false)
718    {
719      list($smtp_host, $smtp_port) = explode(':', $conf_mail['smtp_host']);
720    }
721    else
722    {
723      $smtp_host = $conf_mail['smtp_host'];
724      $smtp_port = 25;
725    }
726
727    $mail->IsSMTP();
728
729    // enables SMTP debug information (for testing) 2 - debug, 0 - no message
730    $mail->SMTPDebug = 0;
731   
732    $mail->Host = $smtp_host;
733    $mail->Port = $smtp_port;
734
735    if (!empty($conf_mail['smtp_secure']) and in_array($conf_mail['smtp_secure'], array('ssl', 'tls')))
736    {
737      $mail->SMTPSecure = $conf_mail['smtp_secure'];
738    }
739   
740    if (!empty($conf_mail['smtp_user']))
741    {
742      $mail->SMTPAuth = true;
743      $mail->Username = $conf_mail['smtp_user'];
744      $mail->Password = $conf_mail['smtp_password'];
745    }
746  }
747
748  $ret = true;
749  $pre_result = trigger_event('before_send_mail', true, $to, $args, $mail);
750
751  if ($pre_result)
752  {
753    $ret = $mail->send();
754    if (!$ret and is_admin())
755    {
756      trigger_error('Mailer Error: ' . $mail->ErrorInfo, E_USER_WARNING);
757    }
758  }
759
760  return $ret;
761}
762
763/**
764 * @deprecated 2.6
765 */
766function pwg_send_mail($result, $to, $subject, $content, $headers)
767{
768  if (is_admin())
769  {
770    trigger_error('pwg_send_mail function is deprecated', E_USER_NOTICE);
771  }
772 
773  if (!$result)
774  {
775    return pwg_mail($to, array(
776        'content' => $content,
777        'subject' => $subject,
778      ));
779  }
780  else
781  {
782    return $result;
783  }
784}
785
786/**
787 * Moves CSS rules contained in the <style> tag to inline CSS
788 * (for compatibility with Gmail and such clients)
789 * @since 2.6
790 * @param string $content
791 * @return string
792 */
793function move_css_to_body($content)
794{
795  include_once(PHPWG_ROOT_PATH.'include/emogrifier.class.php');
796
797  $e = new Emogrifier($content);
798  $e->preserveStyleTag = true;
799  return $e->emogrify();
800}
801
802/**
803 * Saves a copy of the mail if _data/tmp
804 * @param boolean $result
805 * @param string $to
806 * @param array $args
807 * @param PHPMailer $mail
808 * @return boolean $result
809 */
810function pwg_send_mail_test($result, $to, $args, $mail)
811{
812  global $conf, $user, $lang_info;
813 
814  $dir = PHPWG_ROOT_PATH.$conf['data_location'].'tmp';
815  if (mkgetdir($dir, MKGETDIR_DEFAULT&~MKGETDIR_DIE_ON_ERROR))
816  {
817    $filename = $dir.'/mail.'.stripslashes($user['username']).'.'.$lang_info['code'].'.'.$args['theme'].'-'.date('YmdHis');
818    if ($args['content_format'] == 'text/plain')
819    {
820      $filename .= '.txt';
821    }
822    else
823    {
824      $filename .= '.html';
825    }
826   
827    $file = fopen($filename, 'w+');
828    fwrite($file, implode(', ', $to) ."\n");
829    fwrite($file, $mail->Subject ."\n");
830    fwrite($file, $mail->createHeader() ."\n");
831    fwrite($file, $mail->createBody());
832    fclose($file);
833  }
834 
835  return $result;
836}
837
838if ($conf['debug_mail'])
839{
840  add_event_handler('before_send_mail', 'pwg_send_mail_test', EVENT_HANDLER_PRIORITY_NEUTRAL+10, 4);
841}
842
843trigger_action('functions_mail_included');
844
845?>
Note: See TracBrowser for help on using the repository browser.