[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/inc/ -> mail.php (source)

   1  <?php
   2  /**
   3   * Mail functions
   4   *
   5   * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
   6   * @author     Andreas Gohr <andi@splitbrain.org>
   7   */
   8  
   9    if(!defined('DOKU_INC')) define('DOKU_INC',fullpath(dirname(__FILE__).'/../').'/');
  10    require_once (DOKU_INC.'inc/utf8.php');
  11  
  12    // end of line for mail lines - RFC822 says CRLF but postfix (and other MTAs?)
  13    // think different
  14    if(!defined('MAILHEADER_EOL')) define('MAILHEADER_EOL',"\n");
  15    #define('MAILHEADER_ASCIIONLY',1);
  16  
  17  /**
  18   * UTF-8 autoencoding replacement for PHPs mail function
  19   *
  20   * Email address fields (To, From, Cc, Bcc can contain a textpart and an address
  21   * like this: 'Andreas Gohr <andi@splitbrain.org>' - the text part is encoded
  22   * automatically. You can seperate receivers by commas.
  23   *
  24   * @param string $to      Receiver of the mail (multiple seperated by commas)
  25   * @param string $subject Mailsubject
  26   * @param string $body    Messagebody
  27   * @param string $from    Sender address
  28   * @param string $cc      CarbonCopy receiver (multiple seperated by commas)
  29   * @param string $bcc     BlindCarbonCopy receiver (multiple seperated by commas)
  30   * @param string $headers Additional Headers (seperated by MAILHEADER_EOL
  31   * @param string $params  Additonal Sendmail params (passed to mail())
  32   *
  33   * @author Andreas Gohr <andi@splitbrain.org>
  34   * @see    mail()
  35   */
  36  function mail_send($to, $subject, $body, $from='', $cc='', $bcc='', $headers=null, $params=null){
  37  
  38    $message = compact('to','subject','body','from','cc','bcc','headers','params');
  39    return trigger_event('MAIL_MESSAGE_SEND',$message,'_mail_send_action');
  40  }
  41  
  42  function _mail_send_action($data) {
  43  
  44    // retrieve parameters from event data, $to, $subject, $body, $from, $cc, $bcc, $headers, $params
  45    $to = $data['to'];
  46    $subject = $data['subject'];
  47    $body = $data['body'];
  48  
  49    // add robustness in case plugin removes any of these optional values
  50    $from = isset($data['from']) ? $data['from'] : '';
  51    $cc = isset($data['cc']) ? $data['cc'] : '';
  52    $bcc = isset($data['bcc']) ? $data['bcc'] : '';
  53    $headers = isset($data['headers']) ? $data['headers'] : null;
  54    $params = isset($data['params']) ? $data['params'] : null;
  55  
  56    // end additional code to support event ... original mail_send() code from here
  57  
  58    if(defined('MAILHEADER_ASCIIONLY')){
  59      $subject = utf8_deaccent($subject);
  60      $subject = utf8_strip($subject);
  61    }
  62  
  63    if(!utf8_isASCII($subject)) {
  64      $subject = '=?UTF-8?Q?'.mail_quotedprintable_encode($subject,0).'?=';
  65      // Spaces must be encoded according to rfc2047. Use the "_" shorthand
  66      $subject = preg_replace('/ /', '_', $subject);
  67    }
  68  
  69    $header  = '';
  70  
  71    // No named recipients for To: in Windows (see FS#652)
  72    $usenames = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? false : true;
  73  
  74    $to = mail_encode_address($to,'',$usenames);
  75    $header .= mail_encode_address($from,'From');
  76    $header .= mail_encode_address($cc,'Cc');
  77    $header .= mail_encode_address($bcc,'Bcc');
  78    $header .= 'MIME-Version: 1.0'.MAILHEADER_EOL;
  79    $header .= 'Content-Type: text/plain; charset=UTF-8'.MAILHEADER_EOL;
  80    $header .= 'Content-Transfer-Encoding: quoted-printable'.MAILHEADER_EOL;
  81    $header .= $headers;
  82    $header  = trim($header);
  83  
  84    $body = mail_quotedprintable_encode($body);
  85  
  86    if($params == null){
  87      return @mail($to,$subject,$body,$header);
  88    }else{
  89      return @mail($to,$subject,$body,$header,$params);
  90    }
  91  }
  92  
  93  /**
  94   * Encodes an email address header
  95   *
  96   * Unicode characters will be deaccented and encoded
  97   * quoted_printable for headers.
  98   * Addresses may not contain Non-ASCII data!
  99   *
 100   * Example:
 101   *   mail_encode_address("föö <foo@bar.com>, me@somewhere.com","TBcc");
 102   *
 103   * @param string  $string Multiple adresses separated by commas
 104   * @param string  $header Name of the header (To,Bcc,Cc,...)
 105   * @param boolean $names  Allow named Recipients?
 106   */
 107  function mail_encode_address($string,$header='',$names=true){
 108    $headers = '';
 109    $parts = split(',',$string);
 110    foreach ($parts as $part){
 111      $part = trim($part);
 112  
 113      // parse address
 114      if(preg_match('#(.*?)<(.*?)>#',$part,$matches)){
 115        $text = trim($matches[1]);
 116        $addr = $matches[2];
 117      }else{
 118        $addr = $part;
 119      }
 120  
 121      // skip empty ones
 122      if(empty($addr)){
 123        continue;
 124      }
 125  
 126      // FIXME: is there a way to encode the localpart of a emailaddress?
 127      if(!utf8_isASCII($addr)){
 128        msg(htmlspecialchars("E-Mail address <$addr> is not ASCII"),-1);
 129        continue;
 130      }
 131  
 132      if(!mail_isvalid($addr)){
 133        msg(htmlspecialchars("E-Mail address <$addr> is not valid"),-1);
 134        continue;
 135      }
 136  
 137      // text was given
 138      if(!empty($text) && $names){
 139        // add address quotes
 140        $addr = "<$addr>";
 141  
 142        if(defined('MAILHEADER_ASCIIONLY')){
 143          $text = utf8_deaccent($text);
 144          $text = utf8_strip($text);
 145        }
 146  
 147        if(!utf8_isASCII($text)){
 148          $text = '=?UTF-8?Q?'.mail_quotedprintable_encode($text,0).'?=';
 149        }
 150      }else{
 151        $text = '';
 152      }
 153  
 154      // add to header comma seperated and in new line to avoid too long headers
 155      if($headers != '') $headers .= ','.MAILHEADER_EOL.' ';
 156      $headers .= $text.' '.$addr;
 157    }
 158  
 159    if(empty($headers)) return null;
 160  
 161    //if headername was given add it and close correctly
 162    if($header) $headers = $header.': '.$headers.MAILHEADER_EOL;
 163  
 164    return $headers;
 165  }
 166  
 167  /**
 168   * Uses a regular expresion to check if a given mail address is valid
 169   *
 170   * May not be completly RFC conform!
 171   * @link    http://www.faqs.org/rfcs/rfc2822.html    (paras 3.4.1 & 3.2.4)
 172   *
 173   * @author  Chris Smith <chris@jalakai.co.uk>
 174   *
 175   * @param   string $email the address to check
 176   * @return  bool          true if address is valid
 177   */
 178  
 179  // patterns for use in email detection and validation
 180  // NOTE: there is an unquoted '/' in RFC2822_ATEXT, it must remain unquoted to be used in the parser
 181  //       the pattern uses non-capturing groups as captured groups aren't allowed in the parser
 182  //       select pattern delimiters with care!
 183  if (!defined('RFC2822_ATEXT')) define('RFC2822_ATEXT',"0-9a-zA-Z!#$%&'*+/=?^_`{|}~-");
 184  if (!defined('PREG_PATTERN_VALID_EMAIL')) define('PREG_PATTERN_VALID_EMAIL', '['.RFC2822_ATEXT.']+(?:\.['.RFC2822_ATEXT.']+)*@(?i:[0-9a-z][0-9a-z-]*\.)+(?i:[a-z]{2,4}|museum|travel)');
 185  
 186  function mail_isvalid($email){
 187    return preg_match('<^'.PREG_PATTERN_VALID_EMAIL.'$>i', $email);
 188  }
 189  
 190  /**
 191   * Quoted printable encoding
 192   *
 193   * @author umu <umuAThrz.tu-chemnitz.de>
 194   * @link   http://www.php.net/manual/en/function.imap-8bit.php#61216
 195   */
 196  function mail_quotedprintable_encode($sText,$maxlen=74,$bEmulate_imap_8bit=true) {
 197    // split text into lines
 198    $aLines= preg_split("/(?:\r\n|\r|\n)/", $sText);
 199  
 200    for ($i=0;$i<count($aLines);$i++) {
 201      $sLine =& $aLines[$i];
 202      if (strlen($sLine)===0) continue; // do nothing, if empty
 203  
 204      $sRegExp = '/[^\x09\x20\x21-\x3C\x3E-\x7E]/e';
 205  
 206      // imap_8bit encodes x09 everywhere, not only at lineends,
 207      // for EBCDIC safeness encode !"#$@[\]^`{|}~,
 208      // for complete safeness encode every character :)
 209      if ($bEmulate_imap_8bit)
 210        $sRegExp = '/[^\x20\x21-\x3C\x3E-\x7E]/e';
 211  
 212      $sReplmt = 'sprintf( "=%02X", ord ( "$0" ) ) ;';
 213      $sLine = preg_replace( $sRegExp, $sReplmt, $sLine );
 214  
 215      // encode x09,x20 at lineends
 216      {
 217        $iLength = strlen($sLine);
 218        $iLastChar = ord($sLine{$iLength-1});
 219  
 220        //              !!!!!!!!
 221        // imap_8_bit does not encode x20 at the very end of a text,
 222        // here is, where I don't agree with imap_8_bit,
 223        // please correct me, if I'm wrong,
 224        // or comment next line for RFC2045 conformance, if you like
 225        if (!($bEmulate_imap_8bit && ($i==count($aLines)-1)))
 226  
 227        if (($iLastChar==0x09)||($iLastChar==0x20)) {
 228          $sLine{$iLength-1}='=';
 229          $sLine .= ($iLastChar==0x09)?'09':'20';
 230        }
 231      }    // imap_8bit encodes x20 before chr(13), too
 232      // although IMHO not requested by RFC2045, why not do it safer :)
 233      // and why not encode any x20 around chr(10) or chr(13)
 234      if ($bEmulate_imap_8bit) {
 235        $sLine=str_replace(' =0D','=20=0D',$sLine);
 236        //$sLine=str_replace(' =0A','=20=0A',$sLine);
 237        //$sLine=str_replace('=0D ','=0D=20',$sLine);
 238        //$sLine=str_replace('=0A ','=0A=20',$sLine);
 239      }
 240  
 241      // finally split into softlines no longer than $maxlen chars,
 242      // for even more safeness one could encode x09,x20
 243      // at the very first character of the line
 244      // and after soft linebreaks, as well,
 245      // but this wouldn't be caught by such an easy RegExp
 246      if($maxlen){
 247        preg_match_all( '/.{1,'.($maxlen - 2).'}([^=]{0,2})?/', $sLine, $aMatch );
 248        $sLine = implode( '=' . MAILHEADER_EOL, $aMatch[0] ); // add soft crlf's
 249      }
 250    }
 251  
 252    // join lines into text
 253    return implode(MAILHEADER_EOL,$aLines);
 254  }
 255  
 256  
 257  //Setup VIM: ex: et ts=2 enc=utf-8 :


Generated: Thu Aug 28 01:30:02 2008 Cross-referenced by PHPXref 0.7