Changeset 395 for trunk/Mail.php


Ignore:
Timestamp:
Mar 2, 2012, 6:12:11 PM (13 years ago)
Author:
chronos
Message:
  • Přidáno: Podpora pro alternativní HTML text pro odesílání emailů.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Mail.php

    r394 r395  
    11<?php
    22
    3 define(DISPOSITION_INLINE, 'inline');
    4 define(DISPOSITION_ATTACHMENT, 'attachment');
    5 
    6 define(CHARSET_ASCII, 'us-ascii');
    7 define(CHARSET_UTF8, 'utf-8');
     3define('DISPOSITION_INLINE', 'inline');
     4define('DISPOSITION_ATTACHMENT', 'attachment');
     5
     6define('CHARSET_ASCII', 'us-ascii');
     7define('CHARSET_UTF8', 'utf-8');
    88
    99class Mail
     
    1717  var $charset = 'us-ascii';
    1818  var $ctencoding = '7bit';
    19   var $receipt = 0;
     19  var $receipt = 0; 
    2020       
    2121  function __construct()
    2222  {
    2323    $this->autoCheck(true);
    24     $this->boundary = '--'.md5(uniqid('myboundary'));
     24    $this->Boundary = md5(uniqid('myboundary'));
    2525  }
    2626
     
    3939  {
    4040    if(!is_string($From))
    41     {
    4241      throw new Exception('Class Mail: error, From is not a string');
    43     }
    4442    $this->xheaders['From'] = $From;
    4543  }
     
    7674  function Bcc($bcc)
    7775  {
    78     if(is_array($bcc))
    79     {
    80       $this->abcc = $bcc;
    81     } else
    82     {
    83       $this->abcc[]= $bcc;
    84     }
    85 
    86     if($this->checkAddress == true)
    87     $this->CheckAdresses($this->abcc);
    88   }
    89 
    90   function Body($body, $charset = '')
    91   {
    92     $this->body = $body;
    93        
    94     if($charset != '')
    95     {
    96       $this->charset = strtolower($charset);
     76    if(is_array($bcc)) $this->abcc = $bcc;
     77      else $this->abcc[]= $bcc;
     78
     79    if($this->checkAddress == true) $this->CheckAdresses($this->abcc);
     80  }
     81
     82  function Body($Body, $Charset = '')
     83  {
     84    $this->Body = $Body;
     85       
     86    if($Charset != '')
     87    {
     88      $this->charset = strtolower($Charset);
    9789      if($this->charset != CHARSET_ASCII) $this->ctencoding = '8bit';
    9890    }
    9991  }
    10092
     93  function AlternateBody($Body)
     94  {
     95    $this->AlternateBody = $Body;
     96  }
     97
    10198  function Organization($org)
    10299  {
     
    104101  }
    105102
    106   function Priority($priority)
    107   {
    108     if(!intval($priority)) return(false);
    109                
    110     if(!isset($this->priorities[$priority - 1])) return(false);
    111 
    112     $this->xheaders['X-Priority'] = $this->priorities[$priority - 1];   
     103  function Priority($Priority)
     104  {
     105    if(!intval($Priority)) return(false);
     106               
     107    if(!isset($this->priorities[$Priority - 1])) return(false);
     108
     109    $this->xheaders['X-Priority'] = $this->priorities[$Priority - 1];   
    113110    return(true);
    114111  }
    115112
    116   function Attach($filename, $filetype = '', $disposition = DISPOSITION_ATTACHMENT)
     113  function Attach($Filename, $FileType = '', $Disposition = DISPOSITION_ATTACHMENT)
    117114  {
    118115    // TODO : si filetype="", alors chercher dans un tablo de MT connus / extension du fichier
    119     if($filetype == '') $filetype = 'application/x-unknown-content-type';
    120                
    121     $this->aattach[] = $filename;
    122     $this->actype[] = $filetype;
    123     $this->adispo[] = $disposition;
     116    if($FileType == '') $FileType = 'application/x-unknown-content-type';
     117               
     118    $this->Attachments[] = array('FileName' => $Filename, 'FileType' => $FileType,
     119      'Disposition' => $Disposition);
    124120  }
    125121
     
    127123  {
    128124    // build the headers
    129     $this->headers = "";
     125    $this->headers = '';
    130126    //  $this->xheaders['To'] = implode( ", ", $this->sendto );
    131127       
    132128    if(count($this->acc) > 0)
    133       $this->xheaders['CC'] = implode(', ', $this->acc );
     129      $this->xheaders['CC'] = implode(', ', $this->acc);
    134130       
    135131    if(count($this->abcc) > 0)
     
    150146    }
    151147
    152     $this->xheaders['X-Mailer'] = 'Php/libMailv1.3';
     148    $this->xheaders['X-Mailer'] = 'PHP/Mail';
    153149       
    154150    // include attached files
    155     if( count( $this->aattach ) > 0 )
    156     {
    157       $this->BuildAttachement();
    158     } else
    159     {
    160       $this->fullBody = $this->body;
    161     }
    162 
     151    if(count($this->Attachments) > 0) $this->BuildAttachement();
     152      else $this->fullBody = $this->Body;
     153   
    163154    reset($this->xheaders);
    164     while(list($hdr, $value) = each($this->xheaders))
    165     {
    166       if($hdr != 'Subject') $this->headers .= "$hdr: $value\n";
     155    foreach($this->xheaders as $Header => $Value)
     156    {
     157      if($Header != 'Subject') $this->headers .= $Header.': '.$Value."\n";
    167158    }
    168159  }
     
    171162  {
    172163    $this->BuildMail();
    173     $this->strTo = implode(', ', $this->SendTo );
    174        
    175     // envoie du mail
    176     $res = @mail($this->strTo, $this->xheaders['Subject'], $this->fullBody, $this->headers);
     164    $this->strTo = implode(', ', $this->SendTo);
     165       
     166    $res = mail($this->strTo, $this->xheaders['Subject'], $this->fullBody, $this->headers);
     167    return($res);
    177168  }
    178169
     
    180171  {
    181172    $this->BuildMail();
    182     $mail = "To: " . $this->strTo . "\n";
    183     $mail .= $this->headers . "\n";
    184     $mail .= $this->fullBody;
    185     return($mail);
    186   }
    187 
    188   function ValidEmail($address)
    189   {
    190     if(ereg( ".*<(.+)>", $address, $regs))
    191     {
    192       $address = $regs[1];
    193     }
    194     if(ereg( "^[^@  ]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2}|net|com|gov|mil|org|edu|int)\$",$address))
     173    $Mail = "To: " . $this->strTo."\n";
     174    $Mail .= $this->headers."\n";
     175    $Mail .= $this->fullBody;
     176    return($Mail);
     177  }
     178
     179  function ValidEmail($Address)
     180  {
     181    if(ereg(".*<(.+)>", $Address, $regs)) $Address = $regs[1];
     182    if(ereg("^[^@  ]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2}|net|com|gov|mil|org|edu|int)\$", $Address))
    195183      return(true);
    196184      else return(false);
    197185  }
    198186
    199   function CheckAdresses( $aad )
    200   {
    201     for($i=0; $i< count($aad); $i++)
    202     {
    203       if(!$this->ValidEmail($aad[$i]))
    204       {
    205         throw new Exception('Class Mail, method Mail : invalid address '.$aad[$i]);
    206       }
     187  function CheckAdresses($Addresses)
     188  {
     189    foreach($Addresses as $Address)
     190    {
     191      if(!$this->ValidEmail($Address))
     192        throw new Exception('Class Mail, method Mail : invalid address '.$Address);
    207193    }
    208194  }
     
    210196  private function BuildAttachement()
    211197  {
    212     $this->xheaders['Content-Type'] = "multipart/mixed;\n boundary=\"$this->boundary\"";
    213 
    214     $this->fullBody = "This is a multi-part message in MIME format.\n--$this->boundary\n";
    215     $this->fullBody .= "Content-Type: text/plain; charset=$this->charset\nContent-Transfer-Encoding: $this->ctencoding\n\n" . $this->body ."\n";
    216        
    217     $sep = chr(13).chr(10);
    218        
    219     $ata = array();
    220     $k = 0;
    221        
    222     // for each attached file, do...
    223     for($i = 0; $i < count($this->aattach); $i++)
    224     {
    225       $filename = $this->aattach[$i];
    226       $basename = basename($filename);
    227       $ctype = $this->actype[$i];       // content-type
    228       $disposition = $this->adispo[$i];
    229                
    230       if(!file_exists($filename))
    231       {
    232         throw new Exception('Class Mail, method attach : file '.$filename.' can\'t be found');
    233       }
    234       $subhdr = '--'.$this->boundary."\nContent-type: ".$ctype.";\n name=\"".$basename.
    235         "\"\nContent-Transfer-Encoding: base64\nContent-Disposition: ".$disposition.
    236         ";\n  filename=\"".$basename."\"\n";
    237       $ata[$k++] = $subhdr;
    238       // non encoded line length
    239       $linesz = filesize($filename) + 1;
    240       $fp = fopen( $filename, 'r');
    241       $ata[$k++] = chunk_split(base64_encode(fread($fp, $linesz)));
    242       fclose($fp);
    243     }
    244     $this->fullBody .= implode($sep, $ata);
     198    $this->xheaders['Content-Type'] = "multipart/mixed;\n boundary=\"PHP-mixed-".$this->Boundary."\"";
     199
     200    $this->fullBody = "This is a multi-part message in MIME format.\n--PHP-mixed-".$this->Boundary."\n";
     201    if($this->AlternateBody != '')
     202    {
     203      $this->fullBody .= 'Content-Type: multipart/alternative; boundary="PHP-alt-'.$this->Boundary.'"';
     204      $this->fullBody .= "\n\n".'--PHP-alt-'.$this->Boundary."\n";
     205      $this->fullBody .= "Content-Type: text/plain; charset=".$this->charset.
     206        "\nContent-Transfer-Encoding: ".$this->ctencoding."\n\n".$this->Body."\n";
     207     
     208      $this->fullBody .= "\n\n".'--PHP-alt-'.$this->Boundary."\n";
     209      $this->fullBody .= "Content-Type: text/html; charset=".$this->charset.
     210        "\nContent-Transfer-Encoding: ".$this->ctencoding."\n\n".$this->AlternateBody."\n";
     211      //$this->fullBody .= "\n\n".'--PHP-alt-'.$this->Boundary."\n\n";
     212    } else
     213    $this->fullBody .= "Content-Type: text/plain; charset=".$this->charset.
     214      "\nContent-Transfer-Encoding: ".$this->ctencoding."\n\n".$this->Body."\n";
     215       
     216    $ata = array(); 
     217    foreach($this->Attachments as $Attachment)
     218    {
     219      $FileName = $Attachment['FileName'];
     220      $BaseName = basename($FileName);
     221      $ctype = $Attachment['FileType'];
     222      $Disposition = $Attachment['Disposition'];;
     223               
     224      if(!file_exists($FileName))
     225        throw new Exception('Mail: Attached file '.$FileName.' can\'t be found');
     226      $subhdr = "\n".'--PHP-mixed-'.$this->Boundary."\nContent-type: ".$ctype.";\n name=\"".$BaseName.
     227        "\"\nContent-Transfer-Encoding: base64\nContent-Disposition: ".$Disposition.
     228        ";\n  filename=\"".$BaseName."\"\n";
     229      $ata[] = $subhdr;
     230      $ata[] = chunk_split(base64_encode(file_get_contents($FileName)));     
     231    }
     232    $this->fullBody .= implode("\r\n", $ata);
    245233  }
    246234}
Note: See TracChangeset for help on using the changeset viewer.