Changeset 24951 for trunk


Ignore:
Timestamp:
Oct 17, 2013, 3:05:30 PM (11 years ago)
Author:
plg
Message:

feature 2965: replace custom mail functions by PHPMailer (version 5.2.7).
Based on patch by msakik.

function pwg_send_mail no longer used (all is done in pwg_mail)

new param $confsmtp_secure

work still in progress, some obsolete code must be removed

Location:
trunk
Files:
3 added
4 edited

Legend:

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

    r23499 r24951  
    263263$conf['smtp_password'] = '';
    264264
     265// 'ssl' or 'tls'
     266$conf['smtp_secure'] = null;
    265267
    266268// check_upgrade_feed: check if there are database upgrade required. Set to
  • trunk/include/functions_mail.inc.php

    r23823 r24951  
    2626// +-----------------------------------------------------------------------+
    2727
    28 
    29 /**
    30  * Encodes a string using Q form if required (RFC2045)
    31  * mail headers MUST contain only US-ASCII characters
    32  */
    33 function encode_mime_header($str)
    34 {
    35   $x = preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches);
    36   if ($x==0)
    37   {
    38     return $str;
    39   }
    40   // Replace every high ascii, control =, ? and _ characters
    41   $str = preg_replace('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/e',
    42                   "'='.sprintf('%02X', ord('\\1'))", $str);
    43 
    44   // Replace every spaces to _ (more readable than =20)
    45   $str = str_replace(" ", "_", $str);
    46 
    47   global $lang_info;
    48   return '=?'.get_pwg_charset().'?Q?'.$str.'?=';
    49 }
    50 
    5128/*
    5229 * Returns the name of the mail sender :
     
    9168    'smtp_user' => $conf['smtp_user'],
    9269    'smtp_password' => $conf['smtp_password'],
    93     'boundary_key' => generate_key(32),
     70    'smtp_secure' => $conf['smtp_secure'],
    9471    );
    9572
     
    9875
    9976  // name of the webmaster is the title of the gallery
    100   $conf_mail['formated_email_webmaster'] =
    101     format_email(get_mail_sender_name(), $conf_mail['email_webmaster']);
     77  $conf_mail['formated_email_webmaster'] = format_email(get_mail_sender_name(), $conf_mail['email_webmaster']);
    10278
    10379  return $conf_mail;
     
    11894  if ($cvt_name!="")
    11995  {
    120     $cvt_name = encode_mime_header(
    121               '"'
    122               .addcslashes($cvt_name,'"')
    123               .'"');
    124     $cvt_name .= ' ';
     96    $cvt_name = '"'.addcslashes($cvt_name,'"').'"'.' ';
    12597  }
    12698
     
    349321        'content' => $content,
    350322        'content_format' => 'text/plain',
    351         'email_format' => 'text/plain',
     323        'email_format' => 'text/html',
    352324        )
    353325      );
     
    516488  }
    517489
    518   if (empty($args['email_format']))
    519   {
    520     $args['email_format'] = $conf_mail['default_email_format'];
    521   }
    522 
     490  include_once(PHPWG_ROOT_PATH.'include/class.phpmailer.php');
     491
     492  $mail = new PHPMailer;
     493
     494  foreach (explode(',', get_strict_email_list($to)) as $recipient)
     495  {
     496    $mail->addAddress($recipient);
     497  }
     498 
     499  $mail->WordWrap = 76;
     500  $mail->CharSet = 'UTF-8';
     501 
    523502  // Compute root_path in order have complete path
    524503  set_make_full_url();
     
    526505  if (empty($args['from']))
    527506  {
    528     $args['from'] = $conf_mail['formated_email_webmaster'];
     507    $mail->From = get_webmaster_mail_address();
     508    $mail->FromName = get_mail_sender_name();
     509
     510    $mail->addReplyTo(get_webmaster_mail_address(), get_mail_sender_name());
    529511  }
    530512  else
    531513  {
    532     $args['from'] = format_email('', $args['from']);
    533   }
    534 
     514    $mail->From = $args['from'];
     515    $mail->addReplyTo($args['from']);
     516  }
     517
     518  // Subject
    535519  if (empty($args['subject']))
    536520  {
    537521    $args['subject'] = 'Piwigo';
    538522  }
    539   // Spring cleaning
    540   $cvt_subject = trim(preg_replace('#[\n\r]+#s', '', $args['subject']));
    541   // Ascii convertion
    542   $cvt_subject = encode_mime_header($cvt_subject);
     523 
     524  $args['subject'] = trim(preg_replace('#[\n\r]+#s', '', $args['subject']));
     525
     526  $mail->Subject = $args['subject'];
     527
     528  // Cc
     529  if (!empty($args['Cc']))
     530  {
     531    $mail->addCC($args['Cc']);
     532  }
     533
     534  // Bcc
     535  if ($conf_mail['send_bcc_mail_webmaster'])
     536  {
     537    $args['Bcc'][] = get_webmaster_mail_address();;
     538  }
     539
     540  if (!empty($args['Bcc']))
     541  {
     542    $headers.= 'Bcc: '.implode(',', $args['Bcc'])."\n";
     543
     544    foreach ($args['Bcc'] as $bcc)
     545    {
     546      $mail->addBCC($bcc);
     547    }
     548  }
     549
     550  // content
     551  if (empty($args['email_format']))
     552  {
     553    $args['email_format'] = $conf_mail['default_email_format'];
     554  }
    543555
    544556  if (!isset($args['content']))
     
    552564  }
    553565
    554   if ($conf_mail['send_bcc_mail_webmaster'])
    555   {
    556     $args['Bcc'][] = $conf_mail['formated_email_webmaster'];
    557   }
    558 
    559566  if (empty($args['theme']))
    560567  {
     
    562569  }
    563570
    564   $headers = 'From: '.$args['from']."\n";
    565   $headers.= 'Reply-To: '.$args['from']."\n";
    566 
    567   if (!empty($args['Cc']))
    568   {
    569     $headers.= 'Cc: '.implode(',', $args['Cc'])."\n";
    570   }
    571 
    572   if (!empty($args['Bcc']))
    573   {
    574     $headers.= 'Bcc: '.implode(',', $args['Bcc'])."\n";
    575   }
    576 
    577   $headers.= 'Content-Type: multipart/alternative;'."\n";
    578   $headers.= '  boundary="---='.$conf_mail['boundary_key'].'";'."\n";
    579   $headers.= '  reply-type=original'."\n";
    580   $headers.= 'MIME-Version: 1.0'."\n";
    581   $headers.= 'X-Mailer: Piwigo Mailer'."\n";
    582 
    583   // List on content-type
    584571  $content_type_list[] = $args['email_format'];
    585572  if (!empty($conf_mail['alternative_email_format']))
     
    588575  }
    589576
    590   $content = '';
     577  $contents = array();
    591578
    592579  foreach (array_unique($content_type_list) as $content_type)
     
    607594      $conf_mail[$cache_key]['theme']->assign(
    608595        array(
    609           //Header
    610           'BOUNDARY_KEY' => $conf_mail['boundary_key'],
    611           'CONTENT_TYPE' => $content_type,
    612           'CONTENT_ENCODING' => get_pwg_charset(),
    613 
    614           // Footer
    615596          'GALLERY_URL' => get_gallery_home_url(),
    616           'GALLERY_TITLE' =>
    617             isset($page['gallery_title']) ?
    618                   $page['gallery_title'] : $conf['gallery_title'],
     597          'GALLERY_TITLE' => isset($page['gallery_title']) ? $page['gallery_title'] : $conf['gallery_title'],
    619598          'VERSION' => $conf['show_version'] ? PHPWG_VERSION : '',
    620599          'PHPWG_URL' => PHPWG_URL,
    621 
    622600          'TITLE_MAIL' => urlencode(l10n('A comment on your site')),
    623601          'MAIL' => get_webmaster_mail_address()
    624           ));
     602          )
     603        );
    625604
    626605      if ($content_type == 'text/html')
     
    641620
    642621      // what are displayed on the header of each mail ?
    643       $conf_mail[$cache_key]['header'] =
    644         $conf_mail[$cache_key]['theme']->parse('mail_header', true);
     622      $conf_mail[$cache_key]['header'] = $conf_mail[$cache_key]['theme']->parse('mail_header', true);
    645623
    646624      // what are displayed on the footer of each mail ?
    647       $conf_mail[$cache_key]['footer'] =
    648         $conf_mail[$cache_key]['theme']->parse('mail_footer', true);
     625      $conf_mail[$cache_key]['footer'] = $conf_mail[$cache_key]['theme']->parse('mail_footer', true);
    649626    }
    650627
    651628    // Header
    652     $content.= $conf_mail[$cache_key]['header'];
     629    $contents[$content_type] = $conf_mail[$cache_key]['header'];
    653630
    654631    // Content
    655632    if (($args['content_format'] == 'text/plain') and ($content_type == 'text/html'))
    656633    {
    657       $content.= '<p>'.
     634      $contents[$content_type].= '<p>'.
    658635                  nl2br(
    659636                    preg_replace("/(http:\/\/)([^\s,]*)/i",
     
    665642    {
    666643      // convert html text to plain text
    667       $content.= strip_tags($args['content']);
     644      $contents[$content_type].= strip_tags($args['content']);
    668645    }
    669646    else
    670647    {
    671       $content.= $args['content'];
     648      $contents[$content_type].= $args['content'];
    672649    }
    673650
    674651    // Footer
    675     $content.= $conf_mail[$cache_key]['footer'];
    676 
    677   // Close boundary
    678   $content.= "\n".'-----='.$conf_mail['boundary_key'].'--'."\n";
    679   }
    680 
    681   //~ // Close boundary
    682   //~ $content.= "\n".'-----='.$conf_mail['boundary_key'].'--'."\n";
    683 
    684    // Undo Compute root_path in order have complete path
     652    $contents[$content_type].= $conf_mail[$cache_key]['footer'];
     653  }
     654
     655  // Undo Compute root_path in order have complete path
    685656  unset_make_full_url();
    686657
    687   return
    688     trigger_event('send_mail',
    689       false, /* Result */
    690       trigger_event('send_mail_to', get_strict_email_list($to)),
    691       trigger_event('send_mail_subject', $cvt_subject),
    692       trigger_event('send_mail_content', $content),
    693       trigger_event('send_mail_headers', $headers),
    694       $args
    695     );
    696 }
    697 
    698 /*
     658  if (isset($contents['text/html']))
     659  {
     660    $mail->isHTML(true); // Set email format to HTML
     661    $mail->Body = $contents['text/html'];
     662   
     663    if (isset($contents['text/plain']))
     664    {
     665      $mail->AltBody = $contents['text/plain'];
     666    }
     667  }
     668  else
     669  {
     670    $mail->isHTML(false);
     671    $mail->Body = $contents['text/plain'];
     672  }
     673
     674  if ($conf_mail['use_smtp'])
     675  {
     676    // now we need to split port number
     677    if (strpos($conf_mail['smtp_host'], ':') !== false)
     678    {
     679      list($smtp_host, $smtp_port) = explode(':', $conf_mail['smtp_host']);
     680    }
     681    else
     682    {
     683      $smtp_host = $conf_mail['smtp_host'];
     684      $smtp_port = 25;
     685    }
     686
     687    $mail->IsSMTP();
     688
     689    // enables SMTP debug information (for testing) 2 - debug, 0 - no message
     690    $mail->SMTPDebug = 0;
     691   
     692    $mail->Host = $smtp_host;
     693    $mail->Port = $smtp_port;
     694
     695    if (!empty($conf_mail['smtp_secure']) and in_array($conf_mail['smtp_secure'], array('ssl', 'tls')))
     696    {
     697      $mail->SMTPSecure = $conf_mail['smtp_secure'];
     698    }
     699   
     700    if (!empty($conf_mail['smtp_user']))
     701    {
     702      $mail->SMTPAuth = true;
     703      $mail->Username = $conf_mail['smtp_user'];
     704      $mail->Password = $conf_mail['smtp_password'];
     705    }
     706  }
     707 
     708  if(!$mail->send())
     709  {
     710    // TODO use standard error
     711    echo 'Message could not be sent.';
     712    echo 'Mailer Error: ' . $mail->ErrorInfo;
     713    exit;
     714  }
     715}
     716
     717/* DEPRECATED
    699718 * pwg sendmail
    700719 *
     
    708727 * @return boolean (Ok or not)
    709728 */
    710 function pwg_send_mail($result, $to, $subject, $content, $headers)
     729function pwg_send_mail($result, $to, $subject, $contents, $headers)
    711730{
    712731  if (!$result)
    713732  {
     733    include_once(PHPWG_ROOT_PATH.'include/class.phpmailer.php');
     734   
    714735    global $conf_mail;
    715736
     
    724745    else
    725746    {
    726       if ($conf_mail['mail_options'])
    727       {
    728         $options = '-f '.$conf_mail['email_webmaster'];
    729         return mail($to, $subject, $content, $headers, $options);
     747      $mail = new PHPMailer;
     748
     749      $mail->From = 'plg@pigolabs.com';
     750      $mail->FromName = 'Pierrick en local';
     751      foreach (explode(',', $to) as $recipient)
     752      {
     753        $mail->addAddress($recipient);  // Add a recipient
     754      }
     755      // $mail->addReplyTo('plg@piwigo.org', 'Pierrick de Piwigo.org');
     756      // $mail->addCC('cc@example.com');
     757      // $mail->addBCC('bcc@example.com');
     758
     759      $mail->WordWrap = 76;                                // Set word wrap to 50 characters
     760
     761      if (isset($contents['text/html']))
     762      {
     763        $mail->isHTML(true);                                 // Set email format to HTML
     764        $mail->Body = $contents['text/html'];
     765
     766        if (isset($contents['text/plain']))
     767        {
     768          $mail->AltBody = $contents['text/plain'];
     769        }
    730770      }
    731771      else
    732772      {
    733         return mail($to, $subject, $content, $headers);
    734       }
     773        $mail->isHTML(false);
     774        $mail->Body = $contents['text/plain'];
     775      }
     776
     777      $mail->CharSet = 'UTF-8';
     778      $mail->Subject = $subject;
     779
     780      if(!$mail->send()) {
     781        echo 'Message could not be sent.';
     782        echo 'Mailer Error: ' . $mail->ErrorInfo;
     783        exit;
     784      }
     785
     786      // if ($conf_mail['mail_options'])
     787      // {
     788      //   $options = '-f '.$conf_mail['email_webmaster'];
     789      //   return mail($to, $subject, $content, $headers, $options);
     790      // }
     791      // else
     792      // {
     793      //   return mail($to, $subject, $content, $headers);
     794      // }
    735795    }
    736796  }
     
    743803function move_ccs_rules_to_body($content)
    744804{
     805  return $content;
    745806  // We search all css rules in style tags
    746807  preg_match('#<style>(.*?)</style>#s', $content, $matches);
  • trunk/themes/default/template/mail/text/html/header.tpl

    r5125 r24951  
    1 -----={$BOUNDARY_KEY}
    2 Content-Type: {$CONTENT_TYPE}; charset="{$CONTENT_ENCODING}";
    3 Content-Transfer-Encoding: 8bit
    4 
    51<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    62<html lang="{$lang_info.code}" dir="{$lang_info.direction}">
  • trunk/themes/default/template/mail/text/plain/header.tpl

    r5095 r24951  
    1 -----={$BOUNDARY_KEY}
    2 Content-Type: {$CONTENT_TYPE}; charset="{$CONTENT_ENCODING}";
    3 Content-Transfer-Encoding: 8bit{literal}
     1{literal}
    42
    53{/literal}{*note that there must be an empty line to separate the mime headers*}
Note: See TracChangeset for help on using the changeset viewer.