aboutsummaryrefslogtreecommitdiffstats
path: root/include/functions_mail.inc.php
blob: 4c572b665f8ec080ad35387b3a9cfe243bfebc7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
<?php
// +-----------------------------------------------------------------------+
// | Piwigo - a PHP based photo gallery                                    |
// +-----------------------------------------------------------------------+
// | Copyright(C) 2008-2014 Piwigo Team                  http://piwigo.org |
// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
// +-----------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or modify  |
// | it under the terms of the GNU General Public License as published by  |
// | the Free Software Foundation                                          |
// |                                                                       |
// | This program is distributed in the hope that it will be useful, but   |
// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
// | General Public License for more details.                              |
// |                                                                       |
// | You should have received a copy of the GNU General Public License     |
// | along with this program; if not, write to the Free Software           |
// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
// | USA.                                                                  |
// +-----------------------------------------------------------------------+

/**
 * @package functions\mail
 */


/**
 * Returns the name of the mail sender
 *
 * @return string
 */
function get_mail_sender_name()
{
  global $conf;

  return (empty($conf['mail_sender_name']) ? $conf['gallery_title'] : $conf['mail_sender_name']);
}

/**
 * Returns the email of the mail sender
 *
 * @since 2.6
 * @return string
 */
function get_mail_sender_email()
{
  global $conf;

  return (empty($conf['mail_sender_email']) ? get_webmaster_mail_address() : $conf['mail_sender_email']);
}

/**
 * Returns an array of mail configuration parameters.
 * - send_bcc_mail_webmaster
 * - mail_allow_html
 * - use_smtp
 * - smtp_host
 * - smtp_user
 * - smtp_password
 * - smtp_secure
 * - email_webmaster
 * - name_webmaster
 *
 * @return array
 */
function get_mail_configuration()
{
  global $conf;

  $conf_mail = array(
    'send_bcc_mail_webmaster' => $conf['send_bcc_mail_webmaster'],
    'mail_allow_html' => $conf['mail_allow_html'],
    'mail_theme' => $conf['mail_theme'],
    'use_smtp' => !empty($conf['smtp_host']),
    'smtp_host' => $conf['smtp_host'],
    'smtp_user' => $conf['smtp_user'],
    'smtp_password' => $conf['smtp_password'],
    'smtp_secure' => $conf['smtp_secure'],
    'email_webmaster' => get_mail_sender_email(),
    'name_webmaster' => get_mail_sender_name(),
    );

  return $conf_mail;
}

/**
 * Returns an email address with an associated real name.
 * Can return either:
 *    - email@domain.com
 *    - name <email@domain.com>
 *
 * @param string $name
 * @param string $email
 * @return string
 */
function format_email($name, $email)
{
  $cvt_email = trim(preg_replace('#[\n\r]+#s', '', $email));
  $cvt_name = trim(preg_replace('#[\n\r]+#s', '', $name));

  if ($cvt_name!="")
  {
    $cvt_name = '"'.addcslashes($cvt_name,'"').'"'.' ';
  }

  if (strpos($cvt_email, '<') === false)
  {
    return $cvt_name.'<'.$cvt_email.'>';
  }
  else
  {
    return $cvt_name.$cvt_email;
  }
}

/**
 * Returns the email and the name from a formatted address.
 * @since 2.6
 *
 * @param string|string[] $input - if is an array must contain email[, name]
 * @return array email, name
 */
function unformat_email($input)
{
  if (is_array($input))
  {
    if (!isset($input['name']))
    {
      $input['name'] = '';
    }
    return $input;
  }

  if (preg_match('/(.*)<(.*)>.*/', $input, $matches))
  {
    return array(
      'email' => trim($matches[2]),
      'name' => trim($matches[1]),
      );
  }
  else
  {
    return array(
      'email' => trim($input),
      'name' => '',
      );
  }
}

/**
 * Return a clean array of hashmaps (email, name) removing duplicates.
 * It accepts various inputs:
 *    - comma separated list
 *    - array of emails
 *    - single hashmap (email[, name])
 *    - array of incomplete hashmaps
 * @since 2.6
 *
 * @param mixed $data
 * @return string[][]
 */
function get_clean_recipients_list($data)
{
  if (empty($data))
  {
    return array();
  }
  else if (is_array($data))
  {
    $values = array_values($data);
    if (!is_array($values[0]))
    {
      $keys = array_keys($data);
      if (is_int($keys[0]))
      { // simple array of emails
        foreach ($data as &$item)
        {
          $item = array(
            'email' => trim($item),
            'name' => '',
            );
        }
        unset($item);
      }
      else
      { // hashmap of one recipient
        $data = array(unformat_email($data));
      }
    }
    else
    { // array of hashmaps
      $data = array_map('unformat_email', $data);
    }
  }
  else
  {
    $data = explode(',', $data);
    $data = array_map('unformat_email', $data);
  }

  $existing = array();
  foreach ($data as $i => $entry)
  {
    if (isset($existing[ $entry['email'] ]))
    {
      unset($data[$i]);
    }
    else
    {
      $existing[ $entry['email'] ] = true;
    }
  }

  return array_values($data);
}

/**
 * Returns an email address list with minimal email string.
 * @deprecated 2.6
 *
 * @param string $email_list - comma separated
 * @return string
 */
function get_strict_email_list($email_list)
{
  $result = array();
  $list = explode(',', $email_list);

  foreach ($list as $email)
  {
    if (strpos($email, '<') !== false)
    {
       $email = preg_replace('/.*<(.*)>.*/i', '$1', $email);
    }
    $result[] = trim($email);
  }

  return implode(',', array_unique($result));
}

/**
 * Return an new mail template.
 *
 * @param string $email_format - text/html or text/plain
 * @return Template
 */
function &get_mail_template($email_format)
{
  $template = new Template(PHPWG_ROOT_PATH.'themes', 'default', 'template/mail/'.$email_format);
  return $template;
}

/**
 * Return string email format (text/html or text/plain).
 *
 * @param bool $is_html
 * @return string
 */
function get_str_email_format($is_html)
{
  return ($is_html ? 'text/html' : 'text/plain');
}

/**
 * Switch language to specified language.
 * All entries are push on language stack
 *
 * @param string $language
 */
function switch_lang_to($language)
{
  global $switch_lang, $user, $lang, $lang_info, $language_files;

  // explanation of switch_lang
  // $switch_lang['language'] contains data of language
  // $switch_lang['stack'] contains stack LIFO
  // $switch_lang['initialisation'] allow to know if it's first call

  // Treatment with current user
  // Language of current user is saved (it's considered OK on firt call)
  if (!isset($switch_lang['initialisation']) and !isset($switch_lang['language'][$user['language']]))
  {
    $switch_lang['initialisation'] = true;
    $switch_lang['language'][$user['language']]['lang_info'] = $lang_info;
    $switch_lang['language'][$user['language']]['lang'] = $lang;
  }

  // Change current infos
  $switch_lang['stack'][] = $user['language'];
  $user['language'] = $language;

  // Load new data if necessary
  if (!isset($switch_lang['language'][$language]))
  {
    // Re-Init language arrays
    $lang_info = array();
    $lang  = array();

    // language files
    load_language('common.lang', '', array('language'=>$language) );
    // No test admin because script is checked admin (user selected no)
    // Translations are in admin file too
    load_language('admin.lang', '', array('language'=>$language) );
    
    // Reload all plugins files (see load_language declaration)
    if (!empty($language_files))
    {
      foreach ($language_files as $dirname => $files)
        foreach ($files as $filename)
          load_language($filename, $dirname, array('language'=>$language) );
    }
    
    trigger_notify('loading_lang');
    load_language('lang', PHPWG_ROOT_PATH.PWG_LOCAL_DIR,
      array('language'=>$language, 'no_fallback'=>true, 'local'=>true)
    );

    $switch_lang['language'][$language]['lang_info'] = $lang_info;
    $switch_lang['language'][$language]['lang'] = $lang;
  }
  else
  {
    $lang_info = $switch_lang['language'][$language]['lang_info'];
    $lang = $switch_lang['language'][$language]['lang'];
  }
}

/**
 * Switch back language pushed with switch_lang_to() function.
 * @see switch_lang_to()
 */
function switch_lang_back()
{
  global $switch_lang, $user, $lang, $lang_info;

  if (count($switch_lang['stack']) > 0)
  {
    // Get last value
    $language = array_pop($switch_lang['stack']);

    // Change current infos
    if (isset($switch_lang['language'][$language]))
    {
      $lang_info = $switch_lang['language'][$language]['lang_info'];
      $lang = $switch_lang['language'][$language]['lang'];
    }
    $user['language'] = $language;
  }
}

/**
 * Send a notification email to all administrators.
 * current user (if admin) is not notified
 *
 * @param string|array $subject
 * @param string|array $content
 * @param boolean $send_technical_details - send user IP and browser
 * @return boolean
 */
function pwg_mail_notification_admins($subject, $content, $send_technical_details=true)
{
  if (empty($subject) or empty($content))
  {
    return false;
  }

  global $conf, $user;

  if (is_array($subject) or is_array($content))
  {
    switch_lang_to(get_default_language());

    if (is_array($subject))
    {
      $subject = l10n_args($subject);
    }
    if (is_array($content))
    {
      $content = l10n_args($content);
    }

    switch_lang_back();
  }

  $tpl_vars = array();
  if ($send_technical_details)
  {
    $tpl_vars['TECHNICAL'] = array(
      'username' => stripslashes($user['username']),
      'ip' => $_SERVER['REMOTE_ADDR'],
      'user_agent' => $_SERVER['HTTP_USER_AGENT'],
      );
  }

  return pwg_mail_admins(
    array(
      'subject' => '['. $conf['gallery_title'] .'] '. $subject,
      'mail_title' => $conf['gallery_title'],
      'mail_subtitle' => $subject,
      'content' => $content,
      'content_format' => 'text/plain',
      ),
    array(
      'filename' => 'notification_admin',
      'assign' => $tpl_vars,
      )
    );
}

/**
 * Send a email to all administrators.
 * current user (if admin) is excluded
 * @see pwg_mail()
 * @since 2.6
 *
 * @param array $args - as in pwg_mail()
 * @param array $tpl - as in pwg_mail()
 * @return boolean
 */
function pwg_mail_admins($args=array(), $tpl=array())
{
  if (empty($args['content']) and empty($tpl))
  {
    return false;
  }

  global $conf, $user;
  $return = true;

  // get admins (except ourself)
  $query = '
SELECT
    u.'.$conf['user_fields']['username'].' AS name,
    u.'.$conf['user_fields']['email'].' AS email
  FROM '.USERS_TABLE.' AS u
    JOIN '.USER_INFOS_TABLE.' AS i
    ON i.user_id =  u.'.$conf['user_fields']['id'].'
  WHERE i.status in (\'webmaster\',  \'admin\')
    AND u.'.$conf['user_fields']['email'].' IS NOT NULL
    AND i.user_id <> '.$user['id'].'
  ORDER BY name
;';
  $admins = array_from_query($query);

  if (empty($admins))
  {
    return $return;
  }

  switch_lang_to(get_default_language());

  $return = pwg_mail($admins, $args, $tpl);

  switch_lang_back();

  return $return;
}

/**
 * Send an email to a group.
 * @see pwg_mail()
 *
 * @param int $group_id
 * @param array $args - as in pwg_mail()
 *       o language_selected: filters users of the group by language [default value empty]
 * @param array $tpl - as in pwg_mail()
 * @return boolean
 */
function pwg_mail_group($group_id, $args=array(), $tpl=array())
{  
  if (empty($group_id) or ( empty($args['content']) and empty($tpl) ))
  {
    return false;
  }

  global $conf;
  $return = true;

  // get distinct languages of targeted users
  $query = '
SELECT DISTINCT language
  FROM '.USER_GROUP_TABLE.' AS ug
    INNER JOIN '.USERS_TABLE.' AS u
    ON '.$conf['user_fields']['id'].' = ug.user_id
    INNER JOIN '.USER_INFOS_TABLE.' AS ui
    ON ui.user_id = ug.user_id
  WHERE group_id = '.$group_id.'
    AND '.$conf['user_fields']['email'].' <> ""';
  if (!empty($args['language_selected']))
  {
    $query .= '
    AND language = \''.$args['language_selected'].'\'';
  }

    $query .= '
;';
  $languages = array_from_query($query, 'language');

  if (empty($languages))
  {
    return $return;
  }

  foreach ($languages as $language)
  {
    // get subset of users in this group for a specific language
    $query = '
SELECT
    u.'.$conf['user_fields']['username'].' AS name,
    u.'.$conf['user_fields']['email'].' AS email
  FROM '.USER_GROUP_TABLE.' AS ug
    INNER JOIN '.USERS_TABLE.' AS u
    ON '.$conf['user_fields']['id'].' = ug.user_id
    INNER JOIN '.USER_INFOS_TABLE.' AS ui
    ON ui.user_id = ug.user_id
  WHERE group_id = '.$group_id.'
    AND '.$conf['user_fields']['email'].' <> ""
    AND language = \''.$language.'\'
;';
    $users = array_from_query($query);

    if (empty($users))
    {
      continue;
    }

    switch_lang_to($language);

    $return&= pwg_mail(null,
      array_merge(
        $args,
        array('Bcc' => $users)
        ),
      $tpl
      );

    switch_lang_back();
  }

  return $return;
}

/**
 * Sends an email, using Piwigo specific informations.
 *
 * @param string|array $to
 * @param array $args
 *       o from: sender [default value webmaster email]
 *       o Cc: array of carbon copy receivers of the mail. [default value empty]
 *       o Bcc: array of blind carbon copy receivers of the mail. [default value empty]
 *       o subject [default value 'Piwigo']
 *       o content: content of mail [default value '']
 *       o content_format: format of mail content [default value 'text/plain']
 *       o email_format: global mail format [default value $conf_mail['default_email_format']]
 *       o theme: theme to use [default value $conf_mail['mail_theme']]
 *       o mail_title: main title of the mail [default value $conf['gallery_title']]
 *       o mail_subtitle: subtitle of the mail [default value subject]
 * @param array $tpl - use these options to define a custom content template file
 *       o filename
 *       o dirname (optional)
 *       o assign (optional)
 *
 * @return boolean
 */
function pwg_mail($to, $args=array(), $tpl=array())
{
  global $conf, $conf_mail, $lang_info, $page;

  if (empty($to) and empty($args['Cc']) and empty($args['Bcc']))
  {
    return true;
  }

  if (!isset($conf_mail))
  {
    $conf_mail = get_mail_configuration();
  }

  include_once(PHPWG_ROOT_PATH.'include/phpmailer/class.phpmailer.php');

  $mail = new PHPMailer;

  foreach (get_clean_recipients_list($to) as $recipient)
  {
    $mail->addAddress($recipient['email'], $recipient['name']);
  }

  $mail->WordWrap = 76;
  $mail->CharSet = 'UTF-8';
  
  // Compute root_path in order have complete path
  set_make_full_url();

  if (empty($args['from']))
  {
    $from = array(
      'email' => $conf_mail['email_webmaster'],
      'name' => $conf_mail['name_webmaster'],
      );
  }
  else
  {
    $from = unformat_email($args['from']);
  }
  $mail->setFrom($from['email'], $from['name']);
  $mail->addReplyTo($from['email'], $from['name']);

  // Subject
  if (empty($args['subject']))
  {
    $args['subject'] = 'Piwigo';
  }
  $args['subject'] = trim(preg_replace('#[\n\r]+#s', '', $args['subject']));
  $mail->Subject = $args['subject'];

  // Cc
  if (!empty($args['Cc']))
  {
    foreach (get_clean_recipients_list($args['Cc']) as $recipient)
    {
      $mail->addCC($recipient['email'], $recipient['name']);
    }
  }

  // Bcc
  $Bcc = get_clean_recipients_list(@$args['Bcc']);
  if ($conf_mail['send_bcc_mail_webmaster'])
  {
    $Bcc[] = array(
      'email' => get_webmaster_mail_address(),
      'name' => '',
      );
  }
  if (!empty($Bcc))
  {
    foreach ($Bcc as $recipient)
    {
      $mail->addBCC($recipient['email'], $recipient['name']);
    }
  }

  // theme
  if (empty($args['theme']) or !in_array($args['theme'], array('clear','dark')))
  {
    $args['theme'] = $conf_mail['mail_theme'];
  }

  // content
  if (!isset($args['content']))
  {
    $args['content'] = '';
  }
  
  // try to decompose subject like "[....] ...."
  if (!isset($args['mail_title']) and !isset($args['mail_subtitle']))
  {
    if (preg_match('#^\[(.*)\](.*)$#',  $args['subject'], $matches))
    {
      $args['mail_title'] = $matches[1];
      $args['mail_subtitle'] = $matches[2];
    }
  }
  if (!isset($args['mail_title']))
  {
    $args['mail_title'] = $conf['gallery_title'];
  }
  if (!isset($args['mail_subtitle']))
  {
    $args['mail_subtitle'] = $args['subject'];
  }

  // content type
  if (empty($args['content_format']))
  {
    $args['content_format'] = 'text/plain';
  }

  $content_type_list = array();
  if ($conf_mail['mail_allow_html'] and @$args['email_format'] != 'text/plain')
  {
    $content_type_list[] = 'text/html';
  }
  $content_type_list[] = 'text/plain';

  $contents = array();
  foreach ($content_type_list as $content_type)
  {
    // key compose of indexes witch allow to cache mail data
    $cache_key = $content_type.'-'.$lang_info['code'];

    if (!isset($conf_mail[$cache_key]))
    {
      // instanciate a new Template
      if (!isset($conf_mail[$cache_key]['theme']))
      {
        $conf_mail[$cache_key]['theme'] = get_mail_template($content_type);
        trigger_notify('before_parse_mail_template', $cache_key, $content_type);
      }
      $template = &$conf_mail[$cache_key]['theme'];

      $template->set_filename('mail_header', 'header.tpl');
      $template->set_filename('mail_footer', 'footer.tpl');

      $template->assign(
        array(
          'GALLERY_URL' => get_gallery_home_url(),
          'GALLERY_TITLE' => isset($page['gallery_title']) ? $page['gallery_title'] : $conf['gallery_title'],
          'VERSION' => $conf['show_version'] ? PHPWG_VERSION : '',
          'PHPWG_URL' => defined('PHPWG_URL') ? PHPWG_URL : '',
          'CONTENT_ENCODING' => get_pwg_charset(),
          'CONTACT_MAIL' => $conf_mail['email_webmaster'],
          )
        );

      if ($content_type == 'text/html')
      {
        if ($template->smarty->templateExists('global-mail-css.tpl'))
        {
          $template->set_filename('global-css', 'global-mail-css.tpl');
          $template->assign_var_from_handle('GLOBAL_MAIL_CSS', 'global-css');
        }

        if ($template->smarty->templateExists('mail-css-'. $args['theme'] .'.tpl'))
        {
          $template->set_filename('css', 'mail-css-'. $args['theme'] .'.tpl');
          $template->assign_var_from_handle('MAIL_CSS', 'css');
        }
      }
    }
    
    $template = &$conf_mail[$cache_key]['theme'];
    $template->assign(
      array(
        'MAIL_TITLE' => $args['mail_title'],
        'MAIL_SUBTITLE' => $args['mail_subtitle'],
        )
      );

    // Header
    $contents[$content_type] = $template->parse('mail_header', true);

    // Content
    // Stored in a temp variable, if a content template is used it will be assigned
    // to the $CONTENT template variable, otherwise it will be appened to the mail
    if ($args['content_format'] == 'text/plain' and $content_type == 'text/html')
    {
      // convert plain text to html
      $mail_content =
        '<p>'.
        nl2br(
          preg_replace(
            '/(https?:\/\/([-\w\.]+[-\w])+(:\d+)?(\/([\w\/_\.\#-]*(\?\S+)?[^\.\s])?)?)/i',
            '<a href="$1">$1</a>',
            htmlspecialchars($args['content'])
            )
          ).
        '</p>';
    }
    else if ($args['content_format'] == 'text/html' and $content_type == 'text/plain')
    {
      // convert html text to plain text
      $mail_content = strip_tags($args['content']);
    }
    else
    {
      $mail_content = $args['content'];
    }

    // Runtime template
    if (isset($tpl['filename']))
    {
      if (isset($tpl['dirname']))
      {
        $template->set_template_dir($tpl['dirname'] .'/'. $content_type);
      }
      if ($template->smarty->templateExists($tpl['filename'] .'.tpl'))
      {
        $template->set_filename($tpl['filename'], $tpl['filename'] .'.tpl');
        if (!empty($tpl['assign']))
        {
          $template->assign($tpl['assign']);
        }
        $template->assign('CONTENT', $mail_content);
        $contents[$content_type].= $template->parse($tpl['filename'], true);
      }
      else
      {
        $contents[$content_type].= $mail_content;
      }
    }
    else
    {
      $contents[$content_type].= $mail_content;
    }

    // Footer
    $contents[$content_type].= $template->parse('mail_footer', true);
  }

  // Undo Compute root_path in order have complete path
  unset_make_full_url();

  // Send content to PHPMailer
  if (isset($contents['text/html']))
  {
    $mail->isHTML(true);
    $mail->Body = move_css_to_body($contents['text/html']);
    
    if (isset($contents['text/plain']))
    {
      $mail->AltBody = $contents['text/plain'];
    }
  }
  else
  {
    $mail->isHTML(false);
    $mail->Body = $contents['text/plain'];
  }

  if ($conf_mail['use_smtp'])
  {
    // now we need to split port number
    if (strpos($conf_mail['smtp_host'], ':') !== false)
    {
      list($smtp_host, $smtp_port) = explode(':', $conf_mail['smtp_host']);
    }
    else
    {
      $smtp_host = $conf_mail['smtp_host'];
      $smtp_port = 25;
    }

    $mail->IsSMTP();

    // enables SMTP debug information (for testing) 2 - debug, 0 - no message
    $mail->SMTPDebug = 0;
    
    $mail->Host = $smtp_host;
    $mail->Port = $smtp_port;

    if (!empty($conf_mail['smtp_secure']) and in_array($conf_mail['smtp_secure'], array('ssl', 'tls')))
    {
      $mail->SMTPSecure = $conf_mail['smtp_secure'];
    }
    
    if (!empty($conf_mail['smtp_user']))
    {
      $mail->SMTPAuth = true;
      $mail->Username = $conf_mail['smtp_user'];
      $mail->Password = $conf_mail['smtp_password'];
    }
  }

  $ret = true;
  $pre_result = trigger_change('before_send_mail', true, $to, $args, $mail);

  if ($pre_result)
  {
    $ret = $mail->send();
    if (!$ret and (!ini_get('display_errors') or is_admin()))
    {
      trigger_error('Mailer Error: ' . $mail->ErrorInfo, E_USER_WARNING);
    }
    if ($conf['debug_mail'])
    {
      pwg_send_mail_test($ret, $mail, $args);
    }
  }

  return $ret;
}

/**
 * @deprecated 2.6
 */
function pwg_send_mail($result, $to, $subject, $content, $headers)
{
  if (is_admin())
  {
    trigger_error('pwg_send_mail function is deprecated', E_USER_NOTICE);
  }
  
  if (!$result)
  {
    return pwg_mail($to, array(
        'content' => $content,
        'subject' => $subject,
      ));
  }
  else
  {
    return $result;
  }
}

/**
 * Moves CSS rules contained in the <style> tag to inline CSS.
 * Used for compatibility with Gmail and such clients
 * @since 2.6
 *
 * @param string $content
 * @return string
 */
function move_css_to_body($content)
{
  include_once(PHPWG_ROOT_PATH.'include/emogrifier.class.php');

  $e = new Emogrifier($content);
  return @$e->emogrify();
}

/**
 * Saves a copy of the mail if _data/tmp.
 *
 * @param boolean $success
 * @param PHPMailer $mail
 * @param array $args
 */
function pwg_send_mail_test($success, $mail, $args)
{
  global $conf, $user, $lang_info;
  
  $dir = PHPWG_ROOT_PATH.$conf['data_location'].'tmp';
  if (mkgetdir($dir, MKGETDIR_DEFAULT&~MKGETDIR_DIE_ON_ERROR))
  {
    $filename = $dir.'/mail.'.stripslashes($user['username']).'.'.$lang_info['code'].'-'.date('YmdHis').($success ? '' : '.ERROR');
    if ($args['content_format'] == 'text/plain')
    {
      $filename .= '.txt';
    }
    else
    {
      $filename .= '.html';
    }
    
    $file = fopen($filename, 'w+');
    if (!$success)
    {
      fwrite($file, "ERROR: " . $mail->ErrorInfo . "\n\n");
    }
    fwrite($file, $mail->getSentMIMEMessage());
    fclose($file);
  }
}

trigger_notify('functions_mail_included');

?>