3 * Lightweight class for sending an e-mail.
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
11 require_once("AWLUtilities.php");
13 * Lightweight class for sending an e-mail.
23 * A comma-separated list of addresses to send To
29 * The visible sender of the e-mail.
35 * A comma-separated list of addresses to carbon-copy to
41 * A comma-separated list of addresses to blind carbon-copy to
47 * A comma-separated list of addresses to set as the Errors-to: header
53 * A comma-separated list of addresses to set as the Reply-to: header
59 * The address to set as the sender of the e-mail.
65 * The subject line of the email.
71 * The body of the email.
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
85 $this->Subject
= $subject;
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 .= ", ";
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;
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;
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;
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 ) {
199 * Actually send the email
200 * @param string $recipient The email address to append.
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 );