欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

给多个地址发邮件的类

程序员文章站 2022-06-22 15:34:01
<?php  

////////////////////////////////////////////////////////////  
//   emailclass 0.5  
//   class for sending mail  
//  
//   paul schreiber  
//   php@paulschreiber.com  
//   http://paulschreiber.com/  
//  
//   parameters  
//   ----------  
//   - subject, message, sendername, senderemail and tolist are required  
//   - cclist, bcclist and replyto are optional  
//   - tolist, cclist and bcclist can be strings or arrays of strings  
//     (those strings should be valid email addresses  
//  
//   example  
//   -------  
//   $m = new email ( "hello there",            // subject  
//                    "how are you?",           // message body  
//                    "paul",                   // sender's name  
//                    "foo@foobar.com",         // sender's email  
//                    array("paul@foobar.com", "foo@bar.com"), // to: recipients  
//                    "paul@whereever.com"      // cc: recipient  
//                   );  
//  
//       print "mail sent, result was" . $m->send();  
//  
//  
//  

if ( ! defined( 'mail_class_defined' ) ) {  
        define('mail_class_defined', 1 );  

class email {  

        // the constructor!  
        function email ( $subject, $message, $sendername, $senderemail, $tolist, $cclist=0, $bcclist=0, $replyto=0) {  
                $this->sender = $sendername . " <$senderemail>";  
                $this->replyto = $replyto;  
                $this->subject = $subject;  
                $this->message = $message;  

                // set the to: recipient(s)  
                if ( is_array($tolist) ) {  
                        $this->to = join( $tolist, "," );  
                } else {  
                        $this->to = $tolist;  
                }  

                // set the cc: recipient(s)  
                if ( is_array($cclist) && sizeof($cclist) ) {  
                        $this->cc = join( $cclist, "," );  
                } elseif ( $cclist ) {  
                        $this->cc = $cclist;  
                }  

                // set the bcc: recipient(s)  
                if ( is_array($bcclist) && sizeof($bcclist) ) {  
                        $this->bcc = join( $bcclist, "," );  
                } elseif ( $bcclist ) {  
                        $this->bcc = $bcclist;  
                }  

        }  

        // send the message; this is actually just a wrapper for   
        // php's mail() function; heck, it's php's mail function done right :-)  
        // you could override this method to:  
        // (a) use sendmail directly  
        // (b) do smtp with sockets  
        function send () {  
                // create the headers needed by php's mail() function  

                // sender  
                $this->headers = "from: " . $this->sender . "\n";  

                // reply-to address  
                if ( $this->replyto ) {  
                        $this->headers .= "reply-to: " . $this->replyto . "\n";  
                }  

                // cc: recipient(s)  
                if ( $this->cc ) {  
                        $this->headers .= "cc: " . $this->cc . "\n";  
                }  

                // bcc: recipient(s)  
                if ( $this->bcc ) {  
                        $this->headers .= "bcc: " . $this->bcc . "\n";  
                }  

                return mail ( $this->to, $this->subject, $this->message, $this->headers );  
        }  
}  


}  
?>