Release 0.51
[awl.git] / inc / EMail.php
blob7c9d199640dc9825a4bc97a65e9ce7545e2c0dfe
1 <?php
2 /**
3 * Lightweight class for sending an e-mail.
4 * @package awl
5 * @subpackage EMail
6 * @author Andrew McMillan <andrew@mcmillan.net.nz>
7 * @copyright Catalyst IT Ltd
8 * @license http://gnu.org/copyleft/gpl.html GNU GPL v2 or later
9 */
11 require_once("AWLUtilities.php");
12 /**
13 * Lightweight class for sending an e-mail.
14 * @package awl
16 class EMail
18 /**#@+
19 * @access private
22 /**
23 * A comma-separated list of addresses to send To
24 * @var string
26 var $To; // To:
28 /**
29 * The visible sender of the e-mail.
30 * @var string
32 var $From; // etc...
34 /**
35 * A comma-separated list of addresses to carbon-copy to
36 * @var string
38 var $Cc;
40 /**
41 * A comma-separated list of addresses to blind carbon-copy to
42 * @var string
44 var $Bcc;
46 /**
47 * A comma-separated list of addresses to set as the Errors-to: header
48 * @var string
50 var $ErrorsTo;
52 /**
53 * A comma-separated list of addresses to set as the Reply-to: header
54 * @var string
56 var $ReplyTo;
58 /**
59 * The address to set as the sender of the e-mail.
60 * @var string
62 var $Sender;
64 /**
65 * The subject line of the email.
66 * @var string
68 var $Subject;
70 /**
71 * The body of the email.
72 * @var string
74 var $Body;
75 /**#@-*/
77 /**
78 * Create the e-mail, optionally assigning the subject and primary recipient.
79 * @param string $subject The subject line of the email.
80 * @param string $to A comma-separated list of addresses for the primary recipient(s).
82 function EMail( $subject = "", $to = "" ) {
83 // Initialise with some defaults
84 $this->From = "";
85 $this->Subject = $subject;
86 $this->To = $to;
87 $this->Cc = "";
88 $this->Bcc = "";
89 $this->ErrorsTo = "";
90 $this->ReplyTo = "";
91 $this->Sender = "";
92 $this->Body = "";
95 /**
96 * Append something with a comma delimter onto the existing referenced string
97 * @param stringref &$onto The string we will be appending to.
98 * @param string $extra What we will be appending
99 * @return string The new string.
101 function _AppendDelimited( &$onto, $extra ) {
102 if ( !isset($extra) || $extra == "" ) return false;
103 if ( $onto != "" ) $onto .= ", ";
104 $onto .= $extra;
105 return $onto;
109 * Add another recipient to the email
110 * @param string $recipient The email address to append.
111 * @return string The new recipient list.
113 function AddTo( $recipient ) {
114 return $this->_AppendDelimited($this->To, $recipient);
118 * Add another Cc recipient to the email
119 * @param string $recipient The email address to append.
120 * @return string The new Cc recipient list.
122 function AddCc( $recipient ) {
123 return $this->_AppendDelimited($this->Cc, $recipient);
127 * Add another Bcc recipient to the email
128 * @param string $recipient The email address to append.
129 * @return string The new Bcc recipient list.
131 function AddBcc( $recipient ) {
132 return $this->_AppendDelimited($this->Bcc, $recipient);
136 * Add another Reply-to address to the email
137 * @param string $recipient The email address to append.
138 * @return string The new Reply-to list.
140 function AddReplyTo( $recipient ) {
141 return $this->_AppendDelimited($this->ReplyTo, $recipient);
145 * Add another Error recipient to the email
146 * @param string $recipient The email address to append.
147 * @return string The new Error recipient list.
149 function AddErrorsTo( $recipient ) {
150 return $this->_AppendDelimited($this->ErrorsTo, $recipient);
155 * Set the visible From address for the e-mail.
156 * @param string $recipient The visible From address
157 * @return string The new From address
159 function SetFrom( $sender ) {
160 $this->From = $sender;
161 return $sender;
166 * Set the envelope sender address for the e-mail.
167 * @param string $recipient The e-mail address for the sender
168 * @return string The new envelope sender address.
170 function SetSender( $sender ) {
171 $this->Sender = $sender;
172 return $sender;
177 * Set the subject line for the email
178 * @param string $recipient The new subject line.
179 * @return string The new subject line.
181 function SetSubject( $subject ) {
182 $this->Subject = $subject;
183 return $subject;
188 * Set the body of the e-mail.
189 * @param string $recipient The email address to append.
190 * @return string The new body of the e-mail.
192 function SetBody( $body ) {
193 $this->Body = $body;
194 return $body;
199 * Actually send the email
200 * @param string $recipient The email address to append.
202 function Send() {
203 $additional_headers = "";
204 if ( "$this->From" != "" ) $additional_headers .= "From: $this->From\r\n";
205 if ( "$this->Cc" != "" ) $additional_headers .= "Cc: $this->Cc\r\n";
206 if ( "$this->Bcc" != "" ) $additional_headers .= "Bcc: $this->Bcc\r\n";
207 if ( "$this->ReplyTo" != "" ) $additional_headers .= "Reply-To: $this->ReplyTo\r\n";
208 if ( "$this->ErrorsTo" != "" ) $additional_headers .= "Errors-To: $this->ErrorsTo\r\n";
210 $additional_parameters = "";
211 if ( "$this->Sender" != "" ) $additional_parameters = "-f$this->Sender";
212 mail( $this->To, $this->Subject, $this->Body, $additional_headers, $additional_parameters );