Ignore:
Timestamp:
Nov 6, 2013, 4:43:41 PM (10 years ago)
Author:
mistic100
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/functions_mail.inc.php

    r25344 r25357  
    4848 * Returns an array of mail configuration parameters :
    4949 * - send_bcc_mail_webmaster
    50  * - allow_html_email
     50 * - mail_allow_html
    5151 * - use_smtp
    5252 * - smtp_host
     
    6565  $conf_mail = array(
    6666    'send_bcc_mail_webmaster' => $conf['send_bcc_mail_webmaster'],
    67     'allow_html_email' => $conf['allow_html_email'],
     67    'mail_allow_html' => $conf['mail_allow_html'],
    6868    'mail_theme' => $conf['mail_theme'],
    6969    'use_smtp' => !empty($conf['smtp_host']),
     
    254254
    255255/**
    256  * Returns email of all administrator
    257  *
    258  * @return string
    259  */
    260 /*
    261  * send en notification email to all administrators
    262  * if a administrator is doing action,
    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
    268  *   - send_technical_details: send user IP and browser
    269  *
    270  * @return boolean (Ok or not)
    271  */
    272 function pwg_mail_notification_admins($keyargs_subject, $keyargs_content, $send_technical_details=true)
    273 {
     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
    274280  global $conf, $user;
    275281 
    276   // Check arguments
    277   if (empty($keyargs_subject) or empty($keyargs_content))
     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))
    278318  {
    279319    return false;
    280320  }
    281321
     322  global $conf, $user;
    282323  $return = true;
    283324
    284   $admins = array();
    285 
     325  // get admins (except ourself)
    286326  $query = '
    287327SELECT
    288     u.'.$conf['user_fields']['username'].' AS username,
    289     u.'.$conf['user_fields']['email'].' AS mail_address
     328    u.'.$conf['user_fields']['username'].' AS name,
     329    u.'.$conf['user_fields']['email'].' AS email
    290330  FROM '.USERS_TABLE.' AS u
    291     JOIN '.USER_INFOS_TABLE.' AS i ON i.user_id =  u.'.$conf['user_fields']['id'].'
     331    JOIN '.USER_INFOS_TABLE.' AS i
     332    ON i.user_id =  u.'.$conf['user_fields']['id'].'
    292333  WHERE i.status in (\'webmaster\',  \'admin\')
    293     AND '.$conf['user_fields']['email'].' IS NOT NULL
     334    AND u.'.$conf['user_fields']['email'].' IS NOT NULL
    294335    AND i.user_id <> '.$user['id'].'
    295336  ORDER BY username
    296337;';
    297 
    298   $datas = pwg_query($query);
    299   if (!empty($datas))
    300   {
    301     while ($admin = pwg_db_fetch_assoc($datas))
    302     {
    303       if (!empty($admin['mail_address']))
    304       {
    305         $admins[] = format_email($admin['username'], $admin['mail_address']);
    306       }
    307     }
    308   }
    309 
    310   if (count($admins) > 0)
    311   {
    312     switch_lang_to(get_default_language());
    313 
    314     $content = l10n_args($keyargs_content)."\n";
    315     if ($send_technical_details)
    316     {
    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      
    323       $content.= "\n".l10n_args($keyargs_content_admin_info)."\n";
    324     }
    325 
    326     $return = pwg_mail(
    327       implode(', ', $admins),
    328       array(
    329         'subject' => '['.$conf['gallery_title'].'] '.l10n_args($keyargs_subject),
    330         'content' => $content,
    331         'content_format' => 'text/plain',
    332         'email_format' => 'text/html',
    333         )
    334       );
    335    
    336     switch_lang_back();
    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();
    338350
    339351  return $return;
    340352}
    341353
    342 /*
    343  * send en email to user's group
     354/**
     355 * Send an email to a group
     356 * @see pwg_mail()
    344357 *
    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
    349  *   - tpl_shortname: short template name without extension
    350  *   - assign_vars: array used to assign_vars to mail template
    351  *   - language_selected: send mail only to user with this selected language
    352  *
    353  * @return boolean (Ok or not)
    354  */
    355 function pwg_mail_group(
    356   $group_id, $email_format, $keyargs_subject,
    357   $tpl_shortname,
    358   $assign_vars = array(), $language_selected = '')
    359 {
    360   // Check arguments
    361   if
    362     (
    363       empty($group_id) or
    364       empty($email_format) or
    365       empty($keyargs_subject) or
    366       empty($tpl_shortname)
    367     )
     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) ))
    368367  {
    369368    return false;
     
    373372  $return = true;
    374373
     374  // get distinct languages of targeted users
    375375  $query = '
    376 SELECT
    377   distinct language, theme
    378 FROM
    379   '.USER_GROUP_TABLE.' as ug
    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
    382 WHERE
    383         '.$conf['user_fields']['email'].' IS NOT NULL
    384     AND group_id = '.$group_id;
    385 
    386   if (!empty($language_selected))
     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']))
    387385  {
    388386    $query .= '
    389     AND language = \''.$language_selected.'\'';
     387    AND language = \''.$args['language_selected'].'\'';
    390388  }
    391389
    392390    $query .= '
    393391;';
    394 
    395   $result = pwg_query($query);
    396 
    397   if (pwg_db_num_rows($result) > 0)
    398   {
    399     $list = array();
    400     while ($row = pwg_db_fetch_assoc($result))
    401     {
    402       $list[] = $row;
    403     }
    404 
    405     foreach ($list as $elem)
    406     {
    407       $query = '
     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 = '
    408403SELECT
    409   u.'.$conf['user_fields']['username'].' as username,
    410   u.'.$conf['user_fields']['email'].' as mail_address
    411 FROM
    412   '.USER_GROUP_TABLE.' as ug
    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
    415 WHERE
    416         '.$conf['user_fields']['email'].' IS NOT NULL
    417     AND group_id = '.$group_id.'
    418     AND language = \''.$elem['language'].'\'
    419     AND theme = \''.$elem['theme'].'\'
     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.'\'
    420414;';
    421 
    422       $result = pwg_query($query);
    423 
    424       if (pwg_db_num_rows($result) > 0)
    425       {
    426         $Bcc = array();
    427         while ($row = pwg_db_fetch_assoc($result))
    428         {
    429           if (!empty($row['mail_address']))
    430           {
    431             $Bcc[] = format_email(stripslashes($row['username']), $row['mail_address']);
    432           }
    433         }
    434 
    435         if (count($Bcc) > 0)
    436         {
    437           switch_lang_to($elem['language']);
    438 
    439           $mail_template = get_mail_template($email_format, $elem['theme']);
    440           $mail_template->set_filename($tpl_shortname, $tpl_shortname.'.tpl');
    441 
    442           $mail_template->assign(
    443             trigger_event('mail_group_assign_vars', $assign_vars));
    444 
    445           $return = pwg_mail
    446           (
    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;
    458 
    459           switch_lang_back();
    460         }
    461       }
    462     }
     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();
    463433  }
    464434
     
    474444 *       o Cc: array of carbon copy receivers of the mail. [default value empty]
    475445 *       o Bcc: array of blind carbon copy receivers of the mail. [default value empty]
    476  *       o subject  [default value 'Piwigo']
    477  *       o content: content of mail    [default value '']
    478  *       o content_format: format of mail content  [default value 'text/plain']
    479  *       o email_format: global mail format  [default value $conf_mail['default_email_format']]
     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']]
    480450 *       o theme: theme to use [default value $conf_mail['mail_theme']]
    481451 *       o mail_title: main title of the mail [default value $conf['gallery_title']]
    482452 *       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)
    483457 *
    484458 * @return boolean
    485459 */
    486 function pwg_mail($to, $args = array())
     460function pwg_mail($to, $args=array(), $tpl=array())
    487461{
    488462  global $conf, $conf_mail, $lang_info, $page;
     
    572546    $args['content'] = '';
    573547  }
     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  }
    574558  if (!isset($args['mail_title']))
    575559  {
     
    588572
    589573  $content_type_list = array();
    590   if ($conf_mail['allow_html_email'] and @$args['email_format'] != 'text/plain')
     574  if ($conf_mail['mail_allow_html'] and @$args['email_format'] != 'text/plain')
    591575  {
    592576    $content_type_list[] = 'text/html';
     
    599583    // key compose of indexes witch allow to cache mail data
    600584    $cache_key = $content_type.'-'.$lang_info['code'];
    601     $cache_key.= '-'.crc32(@$args['mail_title'] . @$args['mail_subtitle']);
     585    $cache_key.= '-'.crc32(@$args['mail_title'] . @$args['mail_subtitle']); // TODO: find a way to not cache by mail_title
    602586
    603587    if (!isset($conf_mail[$cache_key]))
     
    609593        trigger_action('before_parse_mail_template', $cache_key, $content_type);
    610594      }
    611 
    612       $conf_mail[$cache_key]['theme']->set_filename('mail_header', 'header.tpl');
    613       $conf_mail[$cache_key]['theme']->set_filename('mail_footer', 'footer.tpl');
    614 
    615       $conf_mail[$cache_key]['theme']->assign(
     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(
    616601        array(
    617602          'GALLERY_URL' => get_gallery_home_url(),
     
    628613      if ($content_type == 'text/html')
    629614      {
    630         if ($conf_mail[$cache_key]['theme']->smarty->template_exists('global-mail-css.tpl'))
     615        if ($template->smarty->template_exists('global-mail-css.tpl'))
    631616        {
    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');
     617          $template->set_filename('css', 'global-mail-css.tpl');
     618          $template->assign_var_from_handle('GLOBAL_MAIL_CSS', 'css');
    634619        }
    635620
    636         if ($conf_mail[$cache_key]['theme']->smarty->template_exists('mail-css-'. $args['theme'] .'.tpl'))
     621        if ($template->smarty->template_exists('mail-css-'. $args['theme'] .'.tpl'))
    637622        {
    638           $conf_mail[$cache_key]['theme']->set_filename('css', 'mail-css-'. $args['theme'] .'.tpl');
    639           $conf_mail[$cache_key]['theme']->assign_var_from_handle('MAIL_CSS', 'css');
     623          $template->set_filename('css', 'mail-css-'. $args['theme'] .'.tpl');
     624          $template->assign_var_from_handle('MAIL_CSS', 'css');
    640625        }
    641626      }
    642627
    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);
     628      $conf_mail[$cache_key]['header'] = $template->parse('mail_header', true);
     629      $conf_mail[$cache_key]['footer'] = $template->parse('mail_footer', true);
    645630    }
    646631
     
    649634
    650635    // 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
    651638    if ($args['content_format'] == 'text/plain' and $content_type == 'text/html')
    652639    {
    653640      // convert plain text to html
    654       $contents[$content_type].=
     641      $mail_content =
    655642        '<p>'.
    656643        nl2br(
     
    666653    {
    667654      // convert html text to plain text
    668       $contents[$content_type].= strip_tags($args['content']);
     655      $mail_content = strip_tags($args['content']);
    669656    }
    670657    else
    671658    {
    672       $contents[$content_type].= $args['content'];
     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;
    673688    }
    674689
     
    751766function pwg_send_mail($result, $to, $subject, $content, $headers)
    752767{
    753   trigger_error('pwg_send_mail function is deprecated', E_USER_NOTICE);
     768  if (is_admin())
     769  {
     770    trigger_error('pwg_send_mail function is deprecated', E_USER_NOTICE);
     771  }
    754772 
    755773  if (!$result)
     
    764782    return $result;
    765783  }
    766 }
    767 
    768 /**
    769  * @deprecated 2.6
    770  */
    771 function move_ccs_rules_to_body($content)
    772 {
    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);
    776784}
    777785
Note: See TracChangeset for help on using the changeset viewer.