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

Last change on this file since 25344 was 25344, checked in by mistic100, 10 years ago

feature 2965: apply new template on pwg_mail()
TODO : review other mail functions, configuration GUI

  • Property svn:eol-style set to LF
File size: 22.0 KB
RevLine 
[1021]1<?php
2// +-----------------------------------------------------------------------+
[8728]3// | Piwigo - a PHP based photo gallery                                    |
[2297]4// +-----------------------------------------------------------------------+
[19703]5// | Copyright(C) 2008-2013 Piwigo Team                  http://piwigo.org |
[2297]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// +-----------------------------------------------------------------------+
[1019]23
[25344]24/**
25 * Returns the name of the mail sender
[2284]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
[25344]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/**
[1021]48 * Returns an array of mail configuration parameters :
[19225]49 * - send_bcc_mail_webmaster
[25344]50 * - allow_html_email
[19225]51 * - use_smtp
52 * - smtp_host
53 * - smtp_user
54 * - smtp_password
[25344]55 * - smtp_secure
[19225]56 * - email_webmaster
[25344]57 * - name_webmaster
[1021]58 *
59 * @return array
[1019]60 */
[1021]61function get_mail_configuration()
62{
63  global $conf;
[1019]64
[1021]65  $conf_mail = array(
[2106]66    'send_bcc_mail_webmaster' => $conf['send_bcc_mail_webmaster'],
[25344]67    'allow_html_email' => $conf['allow_html_email'],
68    'mail_theme' => $conf['mail_theme'],
[2106]69    'use_smtp' => !empty($conf['smtp_host']),
70    'smtp_host' => $conf['smtp_host'],
71    'smtp_user' => $conf['smtp_user'],
[19225]72    'smtp_password' => $conf['smtp_password'],
[24951]73    'smtp_secure' => $conf['smtp_secure'],
[25344]74    'email_webmaster' => get_mail_sender_email(),
75    'name_webmaster' => get_mail_sender_name(),
[1021]76    );
77
78  return $conf_mail;
79}
80
[1019]81/**
[1021]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{
[2280]88  $cvt_email = trim(preg_replace('#[\n\r]+#s', '', $email));
[2284]89  $cvt_name = trim(preg_replace('#[\n\r]+#s', '', $name));
[2280]90
[2284]91  if ($cvt_name!="")
[1021]92  {
[24951]93    $cvt_name = '"'.addcslashes($cvt_name,'"').'"'.' ';
[2284]94  }
[1531]95
[2284]96  if (strpos($cvt_email, '<') === false)
97  {
98    return $cvt_name.'<'.$cvt_email.'>';
[1021]99  }
100  else
101  {
[2284]102    return $cvt_name.$cvt_email;
[1021]103  }
104}
105
[2106]106/**
[25344]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/**
[2284]137 * Returns an email address list with minimal email string
[25344]138 * @param string $email_list - comma separated
139 * @return string
[2284]140 */
141function get_strict_email_list($email_list)
142{
143  $result = array();
144  $list = explode(',', $email_list);
[25344]145
[2284]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
[19225]155  return implode(',', array_unique($result));
[2284]156}
157
[2106]158
159/**
160 * Return an new mail template
[25344]161 * @param string $email_format - text/html or text/plain
162 * @return Template
[2106]163 */
[25344]164function &get_mail_template($email_format)
[2106]165{
[25344]166  $template = new Template(PHPWG_ROOT_PATH.'themes', 'default', 'template/mail/'.$email_format);
167  return $template;
[2106]168}
169
170/**
[25344]171 * Switch language to specified language
[1904]172 * All entries are push on language stack
[25344]173 * @param string $language
[1904]174 */
175function switch_lang_to($language)
176{
[23823]177  global $switch_lang, $user, $lang, $lang_info, $language_files;
[1904]178
[4908]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']]))
[1904]187  {
[4908]188    $switch_lang['initialisation'] = true;
189    $switch_lang['language'][$user['language']]['lang_info'] = $lang_info;
190    $switch_lang['language'][$user['language']]['lang'] = $lang;
[1904]191  }
192
[4908]193  // Change current infos
194  $switch_lang['stack'][] = $user['language'];
195  $user['language'] = $language;
[1904]196
[4908]197  // Load new data if necessary
198  if (!isset($switch_lang['language'][$language]))
[1904]199  {
[4908]200    // Re-Init language arrays
201    $lang_info = array();
202    $lang  = array();
[1904]203
[4908]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) );
[23823]209   
[25344]210    // Reload all plugins files (see load_language declaration)
[23823]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   
[4908]218    trigger_action('loading_lang');
[8722]219    load_language('lang', PHPWG_ROOT_PATH.PWG_LOCAL_DIR,
[5208]220      array('language'=>$language, 'no_fallback'=>true, 'local'=>true)
221    );
[1904]222
[4908]223    $switch_lang['language'][$language]['lang_info'] = $lang_info;
224    $switch_lang['language'][$language]['lang'] = $lang;
[1904]225  }
[4908]226  else
227  {
228    $lang_info = $switch_lang['language'][$language]['lang_info'];
229    $lang = $switch_lang['language'][$language]['lang'];
230  }
[1904]231}
232
[25344]233/**
[1904]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  {
[4908]242    // Get last value
243    $language = array_pop($switch_lang['stack']);
[1904]244
[4908]245    // Change current infos
246    if (isset($switch_lang['language'][$language]))
[1904]247    {
[4908]248      $lang_info = $switch_lang['language'][$language]['lang_info'];
249      $lang = $switch_lang['language'][$language]['lang'];
[1904]250    }
[2140]251    $user['language'] = $language;
[1904]252  }
253}
[1908]254
255/**
256 * Returns email of all administrator
257 *
258 * @return string
259 */
260/*
[2106]261 * send en notification email to all administrators
[2121]262 * if a administrator is doing action,
[1908]263 * he's be removed to email list
264 *
265 * @param:
266 *   - keyargs_subject: mail subject on l10n_args format
267 *   - keyargs_content: mail content on l10n_args format
[19225]268 *   - send_technical_details: send user IP and browser
[1908]269 *
270 * @return boolean (Ok or not)
271 */
[9360]272function pwg_mail_notification_admins($keyargs_subject, $keyargs_content, $send_technical_details=true)
[1908]273{
[9360]274  global $conf, $user;
275 
[2140]276  // Check arguments
[9360]277  if (empty($keyargs_subject) or empty($keyargs_content))
[2140]278  {
279    return false;
280  }
281
[1908]282  $return = true;
283
284  $admins = array();
285
286  $query = '
[9360]287SELECT
288    u.'.$conf['user_fields']['username'].' AS username,
289    u.'.$conf['user_fields']['email'].' AS mail_address
290  FROM '.USERS_TABLE.' AS u
291    JOIN '.USER_INFOS_TABLE.' AS i ON i.user_id =  u.'.$conf['user_fields']['id'].'
292  WHERE i.status in (\'webmaster\',  \'admin\')
293    AND '.$conf['user_fields']['email'].' IS NOT NULL
294    AND i.user_id <> '.$user['id'].'
295  ORDER BY username
296;';
[1908]297
298  $datas = pwg_query($query);
299  if (!empty($datas))
300  {
[4325]301    while ($admin = pwg_db_fetch_assoc($datas))
[1908]302    {
303      if (!empty($admin['mail_address']))
304      {
[25018]305        $admins[] = format_email($admin['username'], $admin['mail_address']);
[1908]306      }
307    }
308  }
309
[1914]310  if (count($admins) > 0)
[2106]311  {
[1926]312    switch_lang_to(get_default_language());
[1908]313
[9360]314    $content = l10n_args($keyargs_content)."\n";
315    if ($send_technical_details)
316    {
[19225]317      $keyargs_content_admin_info = array(
318        get_l10n_args('Connected user: %s', stripslashes($user['username'])),
319        get_l10n_args('IP: %s', $_SERVER['REMOTE_ADDR']),
320        get_l10n_args('Browser: %s', $_SERVER['HTTP_USER_AGENT'])
321        );
322     
[9360]323      $content.= "\n".l10n_args($keyargs_content_admin_info)."\n";
324    }
325
326    $return = pwg_mail(
327      implode(', ', $admins),
328      array(
[1914]329        'subject' => '['.$conf['gallery_title'].'] '.l10n_args($keyargs_subject),
[9360]330        'content' => $content,
331        'content_format' => 'text/plain',
[24951]332        'email_format' => 'text/html',
[9360]333        )
334      );
335   
[1914]336    switch_lang_back();
337  }
338
[1908]339  return $return;
340}
[2106]341
[1904]342/*
343 * send en email to user's group
344 *
[2106]345 * @param:
346 *   - group_id: mail are sent to group with this Id
347 *   - email_format: mail format
348 *   - keyargs_subject: mail subject on l10n_args format
[1915]349 *   - tpl_shortname: short template name without extension
[2106]350 *   - assign_vars: array used to assign_vars to mail template
351 *   - language_selected: send mail only to user with this selected language
[1908]352 *
353 * @return boolean (Ok or not)
354 */
[2106]355function pwg_mail_group(
[2121]356  $group_id, $email_format, $keyargs_subject,
[2629]357  $tpl_shortname,
[1915]358  $assign_vars = array(), $language_selected = '')
[1904]359{
[2140]360  // Check arguments
[2278]361  if
[2140]362    (
363      empty($group_id) or
364      empty($email_format) or
365      empty($keyargs_subject) or
366      empty($tpl_shortname)
367    )
368  {
369    return false;
370  }
371
[1904]372  global $conf;
[1908]373  $return = true;
[1904]374
375  $query = '
376SELECT
[5123]377  distinct language, theme
[2121]378FROM
379  '.USER_GROUP_TABLE.' as ug
[1904]380  INNER JOIN '.USERS_TABLE.' as u  ON '.$conf['user_fields']['id'].' = ug.user_id
381  INNER JOIN '.USER_INFOS_TABLE.' as ui  ON ui.user_id = ug.user_id
[2121]382WHERE
[1904]383        '.$conf['user_fields']['email'].' IS NOT NULL
384    AND group_id = '.$group_id;
385
386  if (!empty($language_selected))
387  {
388    $query .= '
389    AND language = \''.$language_selected.'\'';
390  }
391
392    $query .= '
393;';
394
[2106]395  $result = pwg_query($query);
[1904]396
[4325]397  if (pwg_db_num_rows($result) > 0)
[2106]398  {
399    $list = array();
[4325]400    while ($row = pwg_db_fetch_assoc($result))
[2106]401    {
402      $list[] = $row;
403    }
404
[2129]405    foreach ($list as $elem)
406    {
407      $query = '
[1904]408SELECT
409  u.'.$conf['user_fields']['username'].' as username,
410  u.'.$conf['user_fields']['email'].' as mail_address
[2121]411FROM
412  '.USER_GROUP_TABLE.' as ug
[1904]413  INNER JOIN '.USERS_TABLE.' as u  ON '.$conf['user_fields']['id'].' = ug.user_id
414  INNER JOIN '.USER_INFOS_TABLE.' as ui  ON ui.user_id = ug.user_id
[2121]415WHERE
[1904]416        '.$conf['user_fields']['email'].' IS NOT NULL
417    AND group_id = '.$group_id.'
[2106]418    AND language = \''.$elem['language'].'\'
[5123]419    AND theme = \''.$elem['theme'].'\'
[1904]420;';
421
[2129]422      $result = pwg_query($query);
[1904]423
[4325]424      if (pwg_db_num_rows($result) > 0)
[1904]425      {
[2129]426        $Bcc = array();
[4325]427        while ($row = pwg_db_fetch_assoc($result))
[1904]428        {
[2129]429          if (!empty($row['mail_address']))
430          {
[25018]431            $Bcc[] = format_email(stripslashes($row['username']), $row['mail_address']);
[2129]432          }
[1904]433        }
434
[2129]435        if (count($Bcc) > 0)
436        {
437          switch_lang_to($elem['language']);
[1904]438
[5123]439          $mail_template = get_mail_template($email_format, $elem['theme']);
[2629]440          $mail_template->set_filename($tpl_shortname, $tpl_shortname.'.tpl');
[1904]441
[2278]442          $mail_template->assign(
[2140]443            trigger_event('mail_group_assign_vars', $assign_vars));
444
[2129]445          $return = pwg_mail
[2106]446          (
[2129]447            '',
448            array
449            (
450              'Bcc' => $Bcc,
451              'subject' => l10n_args($keyargs_subject),
452              'email_format' => $email_format,
453              'content' => $mail_template->parse($tpl_shortname, true),
454              'content_format' => $email_format,
455              'theme' => $elem['theme']
456            )
457          ) and $return;
[1904]458
[2129]459          switch_lang_back();
460        }
[1914]461      }
[1904]462    }
463  }
[1908]464
465  return $return;
[1904]466}
467
[25344]468/**
[2339]469 * sends an email, using Piwigo specific informations
[1809]470 *
[25344]471 * @param string|string[] $to
472 * @param array $args
[2106]473 *       o from: sender [default value webmaster email]
474 *       o Cc: array of carbon copy receivers of the mail. [default value empty]
475 *       o Bcc: array of blind carbon copy receivers of the mail. [default value empty]
[2339]476 *       o subject  [default value 'Piwigo']
[1809]477 *       o content: content of mail    [default value '']
478 *       o content_format: format of mail content  [default value 'text/plain']
[2106]479 *       o email_format: global mail format  [default value $conf_mail['default_email_format']]
[25344]480 *       o theme: theme to use [default value $conf_mail['mail_theme']]
481 *       o mail_title: main title of the mail [default value $conf['gallery_title']]
482 *       o mail_subtitle: subtitle of the mail [default value subject]
[1908]483 *
[25344]484 * @return boolean
[2106]485 */
[1809]486function pwg_mail($to, $args = array())
[1019]487{
[1809]488  global $conf, $conf_mail, $lang_info, $page;
[2106]489
490  if (empty($to) and empty($args['Cc']) and empty($args['Bcc']))
491  {
492    return true;
493  }
[2121]494
[1021]495  if (!isset($conf_mail))
496  {
497    $conf_mail = get_mail_configuration();
498  }
[2106]499
[24952]500  include_once(PHPWG_ROOT_PATH.'include/phpmailer/class.phpmailer.php');
[24951]501
502  $mail = new PHPMailer;
503
[25344]504  $recipients = !is_array($to) ? explode(',', $to) : $to;
505  foreach ($recipients as $recipient)
[2106]506  {
[25344]507    $recipient = unformat_email($recipient);
508    $mail->addAddress($recipient['email'], $recipient['name']);
[2106]509  }
[25344]510
[24951]511  $mail->WordWrap = 76;
512  $mail->CharSet = 'UTF-8';
513 
[1676]514  // Compute root_path in order have complete path
[3938]515  set_make_full_url();
[2106]516
[1809]517  if (empty($args['from']))
[1021]518  {
[25344]519    $from = array(
520      'email' => $conf_mail['email_webmaster'],
521      'name' => $conf_mail['name_webmaster'],
522      );
[1021]523  }
524  else
525  {
[25344]526    $from = unformat_email($args['from']);
[1021]527  }
[25344]528  $mail->setFrom($from['email'], $from['name']);
529  $mail->addReplyTo($from['email'], $from['name']);
[1021]530
[24951]531  // Subject
[1809]532  if (empty($args['subject']))
533  {
[2339]534    $args['subject'] = 'Piwigo';
[1809]535  }
[24951]536  $args['subject'] = trim(preg_replace('#[\n\r]+#s', '', $args['subject']));
537  $mail->Subject = $args['subject'];
538
539  // Cc
540  if (!empty($args['Cc']))
[1809]541  {
[25344]542    foreach ($args['Cc'] as $cc)
543    {
544      $cc = unformat_email($cc);
545      $mail->addCC($cc['email'], $cc['name']);
546    }
[1809]547  }
548
[24951]549  // Bcc
550  if ($conf_mail['send_bcc_mail_webmaster'])
[1809]551  {
[25344]552    $args['Bcc'][] = get_webmaster_mail_address();
[1809]553  }
[24951]554  if (!empty($args['Bcc']))
[2106]555  {
[24951]556    foreach ($args['Bcc'] as $bcc)
557    {
[25344]558      $bcc = unformat_email($bcc);
559      $mail->addBCC($bcc['email'], $bcc['name']);
[24951]560    }
[2106]561  }
562
[25344]563  // theme
564  if (empty($args['theme']) or !in_array($args['theme'], array('clear','dark')))
[5123]565  {
[25344]566    $args['theme'] = $conf_mail['mail_theme'];
[5123]567  }
[2106]568
[25344]569  // content
[24951]570  if (!isset($args['content']))
571  {
572    $args['content'] = '';
573  }
[25344]574  if (!isset($args['mail_title']))
575  {
576    $args['mail_title'] = $conf['gallery_title'];
577  }
578  if (!isset($args['mail_subtitle']))
579  {
580    $args['mail_subtitle'] = $args['subject'];
581  }
[1818]582
[25344]583  // content type
[24951]584  if (empty($args['content_format']))
[2106]585  {
[24951]586    $args['content_format'] = 'text/plain';
[2106]587  }
588
[25344]589  $content_type_list = array();
590  if ($conf_mail['allow_html_email'] and @$args['email_format'] != 'text/plain')
[2106]591  {
[25344]592    $content_type_list[] = 'text/html';
[2106]593  }
[25344]594  $content_type_list[] = 'text/plain';
[2106]595
[24951]596  $contents = array();
[25344]597  foreach ($content_type_list as $content_type)
[3938]598  {
[25344]599    // key compose of indexes witch allow to cache mail data
600    $cache_key = $content_type.'-'.$lang_info['code'];
601    $cache_key.= '-'.crc32(@$args['mail_title'] . @$args['mail_subtitle']);
[2129]602
[3938]603    if (!isset($conf_mail[$cache_key]))
[2106]604    {
[25344]605      // instanciate a new Template
[5123]606      if (!isset($conf_mail[$cache_key]['theme']))
[3938]607      {
[25344]608        $conf_mail[$cache_key]['theme'] = get_mail_template($content_type);
609        trigger_action('before_parse_mail_template', $cache_key, $content_type);
[3938]610      }
[2106]611
[5123]612      $conf_mail[$cache_key]['theme']->set_filename('mail_header', 'header.tpl');
613      $conf_mail[$cache_key]['theme']->set_filename('mail_footer', 'footer.tpl');
[2106]614
[5123]615      $conf_mail[$cache_key]['theme']->assign(
[3938]616        array(
[6411]617          'GALLERY_URL' => get_gallery_home_url(),
[24951]618          'GALLERY_TITLE' => isset($page['gallery_title']) ? $page['gallery_title'] : $conf['gallery_title'],
[3938]619          'VERSION' => $conf['show_version'] ? PHPWG_VERSION : '',
[25344]620          'PHPWG_URL' => defined('PHPWG_URL') ? PHPWG_URL : '',
621          'CONTENT_ENCODING' => get_pwg_charset(),
622          'CONTACT_MAIL' => $conf_mail['email_webmaster'],
623          'MAIL_TITLE' => $args['mail_title'],
624          'MAIL_SUBTITLE' => $args['mail_subtitle'],
[24951]625          )
626        );
[2106]627
[3938]628      if ($content_type == 'text/html')
[2106]629      {
[5125]630        if ($conf_mail[$cache_key]['theme']->smarty->template_exists('global-mail-css.tpl'))
[3938]631        {
[5123]632          $conf_mail[$cache_key]['theme']->set_filename('css', 'global-mail-css.tpl');
633          $conf_mail[$cache_key]['theme']->assign_var_from_handle('GLOBAL_MAIL_CSS', 'css');
[3938]634        }
[2106]635
[25344]636        if ($conf_mail[$cache_key]['theme']->smarty->template_exists('mail-css-'. $args['theme'] .'.tpl'))
[3938]637        {
[25344]638          $conf_mail[$cache_key]['theme']->set_filename('css', 'mail-css-'. $args['theme'] .'.tpl');
[5123]639          $conf_mail[$cache_key]['theme']->assign_var_from_handle('MAIL_CSS', 'css');
[3938]640        }
[2106]641      }
642
[24951]643      $conf_mail[$cache_key]['header'] = $conf_mail[$cache_key]['theme']->parse('mail_header', true);
644      $conf_mail[$cache_key]['footer'] = $conf_mail[$cache_key]['theme']->parse('mail_footer', true);
[3938]645    }
[1809]646
[3938]647    // Header
[24951]648    $contents[$content_type] = $conf_mail[$cache_key]['header'];
[1019]649
[3938]650    // Content
[25344]651    if ($args['content_format'] == 'text/plain' and $content_type == 'text/html')
[3938]652    {
[25344]653      // convert plain text to html
654      $contents[$content_type].=
655        '<p>'.
656        nl2br(
657          preg_replace(
658            '/(https?:\/\/([-\w\.]+[-\w])+(:\d+)?(\/([\w\/_\.\#-]*(\?\S+)?[^\.\s])?)?)/i',
659            '<a href="$1">$1</a>',
660            htmlspecialchars($args['content'])
661            )
662          ).
663        '</p>';
[3938]664    }
[25344]665    else if ($args['content_format'] == 'text/html' and $content_type == 'text/plain')
[3938]666    {
667      // convert html text to plain text
[24951]668      $contents[$content_type].= strip_tags($args['content']);
[3938]669    }
670    else
671    {
[24951]672      $contents[$content_type].= $args['content'];
[3938]673    }
[1809]674
[3938]675    // Footer
[24951]676    $contents[$content_type].= $conf_mail[$cache_key]['footer'];
677  }
[1809]678
[24951]679  // Undo Compute root_path in order have complete path
680  unset_make_full_url();
681
[25344]682  // Send content to PHPMailer
[24951]683  if (isset($contents['text/html']))
684  {
[25344]685    $mail->isHTML(true);
686    $mail->Body = move_css_to_body($contents['text/html']);
[24951]687   
688    if (isset($contents['text/plain']))
689    {
690      $mail->AltBody = $contents['text/plain'];
691    }
[3938]692  }
[24951]693  else
694  {
695    $mail->isHTML(false);
696    $mail->Body = $contents['text/plain'];
697  }
[1809]698
[24951]699  if ($conf_mail['use_smtp'])
700  {
701    // now we need to split port number
702    if (strpos($conf_mail['smtp_host'], ':') !== false)
703    {
704      list($smtp_host, $smtp_port) = explode(':', $conf_mail['smtp_host']);
705    }
706    else
707    {
708      $smtp_host = $conf_mail['smtp_host'];
709      $smtp_port = 25;
710    }
[3938]711
[24951]712    $mail->IsSMTP();
[1642]713
[24951]714    // enables SMTP debug information (for testing) 2 - debug, 0 - no message
715    $mail->SMTPDebug = 0;
716   
717    $mail->Host = $smtp_host;
718    $mail->Port = $smtp_port;
719
720    if (!empty($conf_mail['smtp_secure']) and in_array($conf_mail['smtp_secure'], array('ssl', 'tls')))
721    {
722      $mail->SMTPSecure = $conf_mail['smtp_secure'];
723    }
724   
725    if (!empty($conf_mail['smtp_user']))
726    {
727      $mail->SMTPAuth = true;
728      $mail->Username = $conf_mail['smtp_user'];
729      $mail->Password = $conf_mail['smtp_password'];
730    }
731  }
[24966]732
[25344]733  $ret = true;
734  $pre_result = trigger_event('before_send_mail', true, $to, $args, $mail);
735
736  if ($pre_result)
[24951]737  {
[25344]738    $ret = $mail->send();
739    if (!$ret and is_admin())
740    {
741      trigger_error('Mailer Error: ' . $mail->ErrorInfo, E_USER_WARNING);
742    }
[24951]743  }
[25344]744
[24966]745  return $ret;
[2140]746}
747
[25344]748/**
749 * @deprecated 2.6
[2140]750 */
[25344]751function pwg_send_mail($result, $to, $subject, $content, $headers)
[2140]752{
[25344]753  trigger_error('pwg_send_mail function is deprecated', E_USER_NOTICE);
754 
[2280]755  if (!$result)
756  {
[25344]757    return pwg_mail($to, array(
758        'content' => $content,
759        'subject' => $subject,
760      ));
[2280]761  }
762  else
763  {
764    return $result;
765  }
[2140]766}
767
[25344]768/**
769 * @deprecated 2.6
770 */
[5695]771function move_ccs_rules_to_body($content)
772{
[25344]773  trigger_error('move_ccs_rules_to_body function is deprecated, use move_css_to_body', E_USER_NOTICE);
774 
775  return move_css_to_body($content);
776}
[5695]777
[25344]778/**
779 * Moves CSS rules contained in the <style> tag to inline CSS
780 * (for compatibility with Gmail and such clients)
781 * @since 2.6
782 * @param string $content
783 * @return string
784 */
785function move_css_to_body($content)
786{
787  include_once(PHPWG_ROOT_PATH.'include/emogrifier.class.php');
[5695]788
[25344]789  $e = new Emogrifier($content);
790  $e->preserveStyleTag = true;
791  return $e->emogrify();
792}
[5695]793
[25344]794/**
795 * Saves a copy of the mail if _data/tmp
796 * @param boolean $result
797 * @param string $to
798 * @param array $args
799 * @param PHPMailer $mail
800 * @return boolean $result
801 */
802function pwg_send_mail_test($result, $to, $args, $mail)
803{
804  global $conf, $user, $lang_info;
805 
806  $dir = PHPWG_ROOT_PATH.$conf['data_location'].'tmp';
807  if (mkgetdir($dir, MKGETDIR_DEFAULT&~MKGETDIR_DIE_ON_ERROR))
808  {
809    $filename = $dir.'/mail.'.stripslashes($user['username']).'.'.$lang_info['code'].'.'.$args['theme'].'-'.date('YmdHis');
810    if ($args['content_format'] == 'text/plain')
[5695]811    {
[25344]812      $filename .= '.txt';
[5695]813    }
814    else
815    {
[25344]816      $filename .= '.html';
[5695]817    }
[25344]818   
819    $file = fopen($filename, 'w+');
820    fwrite($file, implode(', ', $to) ."\n");
821    fwrite($file, $mail->Subject ."\n");
822    fwrite($file, $mail->createHeader() ."\n");
823    fwrite($file, $mail->createBody());
824    fclose($file);
[5695]825  }
[25344]826 
827  return $result;
[5695]828}
829
[25344]830if ($conf['debug_mail'])
[2140]831{
[25344]832  add_event_handler('before_send_mail', 'pwg_send_mail_test', EVENT_HANDLER_PRIORITY_NEUTRAL+10, 4);
[1021]833}
[1105]834
[2140]835trigger_action('functions_mail_included');
836
[25344]837?>
Note: See TracBrowser for help on using the repository browser.