Advertisement
Guest User

Class Mail - fork

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