home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Programare / dotproject / classes / libmail.class.php < prev    next >
PHP Script  |  2003-10-24  |  10KB  |  435 lines

  1. <?php /* CLASSES $Id: libmail.class.php,v 1.7 2003/10/24 05:35:27 ajdonnison Exp $ */
  2. /**
  3.  *    @package dotproject
  4.  *    @subpackage utilites
  5. */
  6.  
  7. /**
  8.  *    This class encapsulates the PHP mail() function.
  9.  *
  10.  *    implements CC, Bcc, Priority headers
  11.  *    @version    1.3
  12.  *    <ul>
  13.  *    <li>added ReplyTo( $address ) method
  14.  *    <li>added Receipt() method - to add a mail receipt
  15.  *    <li>added optionnal charset parameter to Body() method. this should fix charset problem on some mail clients
  16.  *    </ul>
  17.  *    Example
  18.  *    <code>
  19.  *    include "libmail.php";
  20.  *
  21.  *    $m= new Mail; // create the mail
  22.  *    $m->From( "leo@isp.com" );
  23.  *    $m->To( "destination@somewhere.fr" );
  24.  *    $m->Subject( "the subject of the mail" );
  25.  *
  26.  *    $message= "Hello world!\nthis is a test of the Mail class\nplease ignore\nThanks.";
  27.  *    $m->Body( $message);    // set the body
  28.  *    $m->Cc( "someone@somewhere.fr");
  29.  *    $m->Bcc( "someoneelse@somewhere.fr");
  30.  *    $m->Priority(4) ;    // set the priority to Low
  31.  *    $m->Attach( "/home/leo/toto.gif", "image/gif" ) ;    // attach a file of type image/gif
  32.  *    $m->Send();    // send the mail
  33.  *    echo "the mail below has been sent:<br><pre>", $m->Get(), "</pre>";
  34.  *    </code>
  35.  
  36. LASTMOD
  37.     Fri Oct  6 15:46:12 UTC 2000
  38.  
  39.  *    @author    Leo West - lwest@free.fr
  40.  */
  41. class Mail
  42. {
  43. /**
  44.  *    list of To addresses
  45.  *    @var    array
  46.  */
  47.     var $sendto = array();
  48. /**
  49.  *    @var    array
  50. */
  51.     var $acc = array();
  52. /**
  53.  *    @var    array
  54. */
  55.     var $abcc = array();
  56. /**
  57.  *    paths of attached files
  58.  *    @var array
  59. */
  60.     var $aattach = array();
  61. /**
  62.  *    list of message headers
  63.  *    @var array
  64.     */
  65.     var $xheaders = array();
  66.     /**
  67.  *    message priorities referential
  68.  *    @var array
  69.     */
  70.     var $priorities = array( '1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)' );
  71.     /**
  72.  *    character set of message
  73.  *    @var string
  74.     */
  75.     var $charset = "us-ascii";
  76.     var $ctencoding = "7bit";
  77.     var $receipt = 0;
  78.  
  79.     var $useRawAddress = true;
  80.  
  81. /**
  82.  *    Mail constructor
  83. */
  84. function Mail()
  85. {
  86.     $this->autoCheck( true );
  87.     $this->boundary= "--" . md5( uniqid("myboundary") );
  88. }
  89.  
  90.  
  91. /**
  92.  *    activate or desactivate the email addresses validator
  93.  *
  94.  *    ex: autoCheck( true ) turn the validator on
  95.  *    by default autoCheck feature is on
  96.  
  97.  *    @param boolean    $bool set to true to turn on the auto validation
  98.  *    @access public
  99. */
  100. function autoCheck( $bool )
  101. {
  102.     if( $bool ) {
  103.         $this->checkAddress = true;
  104.     } else {
  105.         $this->checkAddress = false;
  106.     }
  107. }
  108.  
  109.  
  110. /**
  111.  *    Define the subject line of the email
  112.  *    @param string $subject any monoline string
  113.  *    @param string $charset encoding to be used for Quoted-Printable encoding of the subject 
  114. */
  115. function Subject( $subject, $charset='' ) {
  116.     if( $charset != "" ) {
  117.         $this->charset = strtolower($charset);
  118.     }
  119.     
  120.     if( $this->charset != "us-ascii" and function_exists('imap_8bit')) {
  121.         $subject = "=?".$this->charset."?Q?".
  122.             str_replace("=\r\n","",imap_8bit($subject))."?=";        
  123.     }
  124.     $this->xheaders['Subject'] = strtr( $subject, "\r\n" , "  " );
  125. }
  126.  
  127.  
  128. /**
  129.  *    set the sender of the mail
  130.  *    @param string $from should be an email address
  131.  
  132. */
  133.  
  134. function From( $from ) {
  135.     if( ! is_string($from) ) {
  136.         echo "Class Mail: error, From is not a string";
  137.         exit;
  138.     }
  139.     $this->xheaders['From'] = $from;
  140. }
  141.  
  142. /**
  143.  *    set the Reply-to header
  144.  *    @param string $email should be an email address
  145. */
  146. function ReplyTo( $address ) {
  147.     if (!is_string($address)) {
  148.         return false;
  149.     }
  150.     $this->xheaders["Reply-To"] = $address;
  151. }
  152.  
  153. /**
  154.  *    add a receipt to the mail ie.  a confirmation is returned to the "From" address (or "ReplyTo" if defined)
  155.  *    when the receiver opens the message.
  156.  *    @warning this functionality is *not* a standard, thus only some mail clients are compliants.
  157. */
  158.  
  159. function Receipt() {
  160.     $this->receipt = 1;
  161. }
  162.  
  163. /**
  164.  *    set the mail recipient
  165.  *
  166.  *    The optional reset parameter is useful when looping through records to send individual mails.
  167.  *    This prevents the 'to' array being continually stacked with additional addresses.
  168.  *
  169.  *    @param string $to email address, accept both a single address or an array of addresses
  170.  *    @param boolean $reset resets the current array
  171. */
  172.  
  173. function To( $to, $reset=false ) {
  174.  
  175.     // TODO : test validitΘ sur to
  176.     if( is_array( $to ) ) {
  177.         $this->sendto = $to;
  178.     } else {
  179.         if ($this->useRawAddress) {
  180.            if( preg_match( "/^(.*)\<(.+)\>$/", $to, $regs ) ) {
  181.               $to = $regs[2];
  182.            }
  183.         }
  184.         if ($reset) {
  185.             unset( $this->sendto );
  186.             $this->sendto = array();
  187.         }
  188.         $this->sendto[] = $to;
  189.     }
  190.  
  191.     if( $this->checkAddress == true )
  192.         $this->CheckAdresses( $this->sendto );
  193.  
  194. }
  195.  
  196.  
  197. /**
  198.  *    Cc()
  199.  *    set the CC headers ( carbon copy )
  200.  *    $cc : email address(es), accept both array and string
  201.  */
  202.  
  203. function Cc( $cc ) {
  204.     if( is_array($cc) )
  205.         $this->acc= $cc;
  206.     else
  207.         $this->acc= explode(',', $cc);
  208.  
  209.     if( $this->checkAddress == true )
  210.         $this->CheckAdresses( $this->acc );
  211.  
  212. }
  213.  
  214. /**
  215.  *    set the Bcc headers ( blank carbon copy ).
  216.  *    $bcc : email address(es), accept both array and string
  217.  */
  218.  
  219. function Bcc( $bcc ) {
  220.     if( is_array($bcc) ) {
  221.         $this->abcc = $bcc;
  222.     } else {
  223.         $this->abcc[]= $bcc;
  224.     }
  225.  
  226.     if( $this->checkAddress == true )
  227.         $this->CheckAdresses( $this->abcc );
  228. }
  229.  
  230. /**
  231.  *        set the body (message) of the mail
  232.  *        define the charset if the message contains extended characters (accents)
  233.  *        default to us-ascii
  234.  *        $mail->Body( "mΘl en franτais avec des accents", "iso-8859-1" );
  235.  */
  236. function Body( $body, $charset="" ) {
  237.     $this->body = $body;
  238.  
  239.     if( $charset != "" ) {
  240.         $this->charset = strtolower($charset);
  241.         if( $this->charset != "us-ascii" )
  242.             $this->ctencoding = "8bit";
  243.     }
  244. }
  245.  
  246. /**
  247.  *        set the Organization header
  248.  */
  249. function Organization( $org ) {
  250.     if( trim( $org != "" )  )
  251.         $this->xheaders['Organization'] = $org;
  252. }
  253.  
  254. /**
  255.  *        set the mail priority
  256.  *        $priority : integer taken between 1 (highest) and 5 ( lowest )
  257.  *        ex: $mail->Priority(1) ; => Highest
  258.  */
  259.  
  260. function Priority( $priority ) {
  261.     if( ! intval( $priority ) )
  262.         return false;
  263.  
  264.     if( ! isset( $this->priorities[$priority-1]) )
  265.         return false;
  266.  
  267.     $this->xheaders["X-Priority"] = $this->priorities[$priority-1];
  268.  
  269.     return true;
  270. }
  271.  
  272. /**
  273.  *    Attach a file to the mail
  274.  *
  275.  *    @param string $filename : path of the file to attach
  276.  *    @param string $filetype : MIME-type of the file. default to 'application/x-unknown-content-type'
  277.  *    @param string $disposition : instruct the Mailclient to display the file if possible ("inline") or always as a link ("attachment") possible values are "inline", "attachment"
  278.  */
  279. function Attach( $filename, $filetype = "", $disposition = "inline" ) {
  280.     // TODO : si filetype="", alors chercher dans un tablo de MT connus / extension du fichier
  281.     if( $filetype == "" )
  282.         $filetype = "application/x-unknown-content-type";
  283.  
  284.     $this->aattach[] = $filename;
  285.     $this->actype[] = $filetype;
  286.     $this->adispo[] = $disposition;
  287. }
  288.  
  289. /**
  290.  *    Build the email message
  291.  *    @access protected
  292. */
  293. function BuildMail() {
  294. // build the headers
  295.     $this->headers = "";
  296. //    $this->xheaders['To'] = implode( ", ", $this->sendto );
  297.  
  298.     if( count($this->acc) > 0 ) {
  299.         $this->xheaders['CC'] = implode( ", ", $this->acc );
  300.     }
  301.     if( count($this->abcc) > 0 ) {
  302.         $this->xheaders['BCC'] = implode( ", ", $this->abcc );
  303.     }
  304.  
  305.     if( $this->receipt ) {
  306.         if( isset($this->xheaders["Reply-To"] ) ) {
  307.             $this->xheaders["Disposition-Notification-To"] = $this->xheaders["Reply-To"];
  308.         } else {
  309.             $this->xheaders["Disposition-Notification-To"] = $this->xheaders['From'];
  310.         }
  311.     }
  312.  
  313.     if( $this->charset != "" ) {
  314.         $this->xheaders["Mime-Version"] = "1.0";
  315.         $this->xheaders["Content-Type"] = "text/plain; charset=$this->charset";
  316.         $this->xheaders["Content-Transfer-Encoding"] = $this->ctencoding;
  317.     }
  318.  
  319.     $this->xheaders["X-Mailer"] = "Php/libMailv1.3";
  320.  
  321.     // include attached files
  322.     if( count( $this->aattach ) > 0 ) {
  323.         $this->_build_attachement();
  324.     } else {
  325.         $this->fullBody = $this->body;
  326.     }
  327.  
  328.     reset($this->xheaders);
  329.     while( list( $hdr,$value ) = each( $this->xheaders )  ) {
  330.         if( $hdr != "Subject" )
  331.             $this->headers .= "$hdr: $value\n";
  332.     }
  333. }
  334.  
  335. /**
  336.  *    format and send the mail
  337.  *    @access public
  338. */
  339. function Send() {
  340.     $this->BuildMail();
  341.  
  342.     $this->strTo = implode( ", ", $this->sendto );
  343.  
  344.     // envoie du mailz
  345.     return @mail( $this->strTo, $this->xheaders['Subject'], $this->fullBody, $this->headers );
  346. }
  347.  
  348. /**
  349.  *    Returns the whole e-mail , headers + message
  350.  *
  351.  *    can be used for displaying the message in plain text or logging it
  352.  *
  353.  *    @return string
  354.  */
  355. function Get() {
  356.     $this->BuildMail();
  357.     $mail = "To: " . $this->strTo . "\n";
  358.     $mail .= $this->headers . "\n";
  359.     $mail .= $this->fullBody;
  360.     return $mail;
  361. }
  362.  
  363. /**
  364.  *    check an email address validity
  365.  *    @access public
  366.  *    @param string $address : email address to check
  367.  *    @return true if email adress is ok
  368.  */
  369. function ValidEmail($address) {
  370.    if( preg_match( "/^(.*)\<(.+)\>$/", $address, $regs ) ) {
  371.       $address = $regs[2];
  372.    }
  373.    if( preg_match( "/^[^@ ]+@([a-zA-Z0-9\-.]+)$/",$address) ) {
  374.       return true;
  375.    } else {
  376.       return false;
  377.    }
  378. }
  379.  
  380. /**
  381.  *    check validity of email addresses
  382.  *    @param    array $aad -
  383.  *    @return if unvalid, output an error message and exit, this may -should- be customized
  384.  */
  385.  
  386. function CheckAdresses( $aad ) {
  387.     for($i=0;$i< count( $aad); $i++ ) {
  388.         if( ! $this->ValidEmail( $aad[$i]) ) {
  389.             echo "Class Mail, method Mail : invalid address $aad[$i]";
  390.             exit;
  391.         }
  392.     }
  393. }
  394.  
  395. /**
  396.  *    check and encode attach file(s) . internal use only
  397.  *    @access private
  398. */
  399. function _build_attachement() {
  400.     $this->xheaders["Content-Type"] = "multipart/mixed;\n boundary=\"$this->boundary\"";
  401.  
  402.     $this->fullBody = "This is a multi-part message in MIME format.\n--$this->boundary\n";
  403.     $this->fullBody .= "Content-Type: text/plain; charset=$this->charset\nContent-Transfer-Encoding: $this->ctencoding\n\n" . $this->body ."\n";
  404.  
  405.     $sep= chr(13) . chr(10);
  406.  
  407.     $ata= array();
  408.     $k=0;
  409.  
  410.     // for each attached file, do...
  411.     for( $i=0; $i < count( $this->aattach); $i++ ) {
  412.         $filename = $this->aattach[$i];
  413.         $basename = basename($filename);
  414.         $ctype = $this->actype[$i];    // content-type
  415.         $disposition = $this->adispo[$i];
  416.  
  417.         if( ! file_exists( $filename) ) {
  418.             echo "Class Mail, method attach : file $filename can't be found"; exit;
  419.         }
  420.         $subhdr= "--$this->boundary\nContent-type: $ctype;\n name=\"$basename\"\nContent-Transfer-Encoding: base64\nContent-Disposition: $disposition;\n  filename=\"$basename\"\n";
  421.         $ata[$k++] = $subhdr;
  422.         // non encoded line length
  423.         $linesz= filesize( $filename)+1;
  424.         $fp= fopen( $filename, 'r' );
  425.         $ata[$k++] = chunk_split(base64_encode(fread( $fp, $linesz)));
  426.         fclose($fp);
  427.     }
  428.     $this->fullBody .= implode($sep, $ata);
  429. }
  430.  
  431. } // class Mail
  432.  
  433.  
  434. ?>
  435.