给多个地址发邮件的类

 1   
 2  
 3////////////////////////////////////////////////////////////   
 4// EmailClass 0.5   
 5// class for sending mail   
 6//   
 7// Paul Schreiber   
 8// [email protected]   
 9// http://paulschreiber.com/   
10//   
11// parameters   
12// ----------   
13// - subject, message, senderName, senderEmail and toList are required   
14// - ccList, bccList and replyTo are optional   
15// - toList, ccList and bccList can be strings or arrays of strings   
16// (those strings should be valid email addresses   
17//   
18// example   
19// -------   
20// $m = new email ( "hello there", // subject   
21// "how are you?", // message body   
22// "paul", // sender's name   
23// "[email protected]", // sender's email   
24// array("[email protected]", "[email protected]"), // To: recipients   
25// "[email protected]" // Cc: recipient   
26// );   
27//   
28// print "mail sent, result was" . $m->send();   
29//   
30//   
31//   
32  
33if ( ! defined( 'MAIL_CLASS_DEFINED' ) ) {   
34define('MAIL_CLASS_DEFINED', 1 );   
35  
36class email {   
37  
38// the constructor!   
39function email ( $subject, $message, $senderName, $senderEmail, $toList, $ccList=0, $bccList=0, $replyTo=0) {   
40$this->sender = $senderName . " <$senderEmail>";   
41$this->replyTo = $replyTo;   
42$this->subject = $subject;   
43$this->message = $message;   
44  
45// set the To: recipient(s)   
46if ( is_array($toList) ) {   
47$this->to = join( $toList, "," );   
48} else {   
49$this->to = $toList;   
50}   
51  
52// set the Cc: recipient(s)   
53if ( is_array($ccList) && sizeof($ccList) ) {   
54$this->cc = join( $ccList, "," );   
55} elseif ( $ccList ) {   
56$this->cc = $ccList;   
57}   
58  
59// set the Bcc: recipient(s)   
60if ( is_array($bccList) && sizeof($bccList) ) {   
61$this->bcc = join( $bccList, "," );   
62} elseif ( $bccList ) {   
63$this->bcc = $bccList;   
64}   
65  
66}   
67  
68// send the message; this is actually just a wrapper for   
69// PHP's mail() function; heck, it's PHP's mail function done right :-)   
70// you could override this method to:   
71// (a) use sendmail directly   
72// (b) do SMTP with sockets   
73function send () {   
74// create the headers needed by PHP's mail() function   
75  
76// sender   
77$this->headers = "From: " . $this->sender . "\n";   
78  
79// reply-to address   
80if ( $this->replyTo ) {   
81$this->headers .= "Reply-To: " . $this->replyTo . "\n";   
82}   
83  
84// Cc: recipient(s)   
85if ( $this->cc ) {   
86$this->headers .= "Cc: " . $this->cc . "\n";   
87}   
88  
89// Bcc: recipient(s)   
90if ( $this->bcc ) {   
91$this->headers .= "Bcc: " . $this->bcc . "\n";   
92}   
93  
94return mail ( $this->to, $this->subject, $this->message, $this->headers );   
95}   
96}   
97  
98  
99}   
Published At
Categories with Web编程
Tagged with
comments powered by Disqus