Overview

Packages

  • awl
    • AuthPlugin
    • AwlDatabase
    • Browser
    • classEditor
    • DataEntry
    • DataUpdate
    • EMail
    • iCalendar
    • MenuSet
    • PgQuery
    • Session
    • Translation
    • User
    • Utilities
    • Validation
    • vCalendar
    • vComponent
    • XMLDocument
    • XMLElement
  • None
  • PHP

Classes

  • EMail
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  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: */
 10: 
 11: require_once("AWLUtilities.php");
 12: /**
 13: * Lightweight class for sending an e-mail.
 14: * @package awl
 15: */
 16: class EMail
 17: {
 18:   /**#@+
 19:   * @access private
 20:   */
 21: 
 22:   /**
 23:   * A comma-separated list of addresses to send To
 24:   * @var string
 25:   */
 26:   private $To;         // To:
 27: 
 28:   /**
 29:   * The visible sender of the e-mail.
 30:   * @var string
 31:   */
 32:   private $From;       // etc...
 33: 
 34:   /**
 35:   * A comma-separated list of addresses to carbon-copy to
 36:   * @var string
 37:   */
 38:   private $Cc;
 39: 
 40:   /**
 41:   * A comma-separated list of addresses to blind carbon-copy to
 42:   * @var string
 43:   */
 44:   private $Bcc;
 45: 
 46:   /**
 47:   * A comma-separated list of addresses to set as the Errors-to: header
 48:   * @var string
 49:   */
 50:   private $ErrorsTo;
 51: 
 52:   /**
 53:   * A comma-separated list of addresses to set as the Reply-to: header
 54:   * @var string
 55:   */
 56:   private $ReplyTo;
 57: 
 58:   /**
 59:   * The address to set as the sender of the e-mail.
 60:   * @var string
 61:   */
 62:   private $Sender;
 63: 
 64:   /**
 65:   * The subject line of the email.
 66:   * @var string
 67:   */
 68:   private $Subject;
 69: 
 70:   /**
 71:   * The body of the email.
 72:   * @var string
 73:   */
 74:   private $Body;
 75:   /**#@-*/
 76: 
 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).
 81:   */
 82:   function __construct( $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    = "";
 93:   }
 94: 
 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.
100:   */
101:   private function _AppendDelimited( &$onto, $extra ) {
102:     if ( !isset($extra) || $extra == "" ) return false;
103:     if ( $onto != "" ) $onto .= ", ";
104:     $onto .= $extra;
105:     return $onto;
106:   }
107: 
108:   /**
109:   * Add another recipient to the email
110:   * @param string $recipient The email address to append.
111:   * @return string The new recipient list.
112:   */
113:   function AddTo( $recipient ) {
114:     return $this->_AppendDelimited($this->To, $recipient);
115:   }
116: 
117:   /**
118:   * Get the current recipient list.
119:   * @return string The current recipient list.
120:   */
121:   function To() {
122:     return $this->To;
123:   }
124: 
125:   /**
126:   * Add another Cc recipient to the email
127:   * @param string $recipient The email address to append.
128:   * @return string The new Cc recipient list.
129:   */
130:   function AddCc( $recipient ) {
131:     return $this->_AppendDelimited($this->Cc, $recipient);
132:   }
133: 
134:   /**
135:   * Add another Bcc recipient to the email
136:   * @param string $recipient The email address to append.
137:   * @return string The new Bcc recipient list.
138:   */
139:   function AddBcc( $recipient ) {
140:     return $this->_AppendDelimited($this->Bcc, $recipient);
141:   }
142: 
143:   /**
144:   * Add another Reply-to address to the email
145:   * @param string $recipient The email address to append.
146:   * @return string The new Reply-to list.
147:   */
148:   function AddReplyTo( $recipient ) {
149:     return $this->_AppendDelimited($this->ReplyTo, $recipient);
150:   }
151: 
152:   /**
153:   * Add another Error recipient to the email
154:   * @param string $recipient The email address to append.
155:   * @return string The new Error recipient list.
156:   */
157:   function AddErrorsTo( $recipient ) {
158:     return $this->_AppendDelimited($this->ErrorsTo, $recipient);
159:   }
160: 
161: 
162:   /**
163:   * Set the visible From address for the e-mail.
164:   * @param string $recipient The visible From address
165:   * @return string The new From address
166:   */
167:   function SetFrom( $sender ) {
168:     $this->From = $sender;
169:     return $sender;
170:   }
171: 
172: 
173:   /**
174:   * Set the envelope sender address for the e-mail.
175:   * @param string $recipient The e-mail address for the sender
176:   * @return string The new envelope sender address.
177:   */
178:   function SetSender( $sender ) {
179:     $this->Sender = $sender;
180:     return $sender;
181:   }
182: 
183: 
184:   /**
185:   * Set the subject line for the email
186:   * @param string $recipient The new subject line.
187:   * @return string The new subject line.
188:   */
189:   function SetSubject( $subject ) {
190:     $this->Subject = $subject;
191:     return $subject;
192:   }
193: 
194: 
195:   /**
196:   * Set the body of the e-mail.
197:   * @param string $recipient The email address to append.
198:   * @return string The new body of the e-mail.
199:   */
200:   function SetBody( $body ) {
201:     $this->Body = $body;
202:     return $body;
203:   }
204: 
205: 
206:   /**
207:   * Actually send the email
208:   * @param string $additional_headers Any additional headers that are needed.
209:   */
210:   function Send( $additional_headers = "" ) {
211:     if ( !empty($this->From) )     $additional_headers .= "From: $this->From\r\n";
212:     if ( !empty($this->Cc) )       $additional_headers .= "Cc: $this->Cc\r\n";
213:     if ( !empty($this->Bcc) )      $additional_headers .= "Bcc: $this->Bcc\r\n";
214:     if ( !empty($this->ReplyTo) )  $additional_headers .= "Reply-To: $this->ReplyTo\r\n";
215:     if ( !empty($this->ErrorsTo) ) $additional_headers .= "Errors-To: $this->ErrorsTo\r\n";
216: 
217:     $additional_parameters = "";
218:     if ( !empty($this->Sender) ) $additional_parameters = "-f$this->Sender";
219:     mail( $this->To, $this->Subject, $this->Body, $additional_headers, $additional_parameters );
220:   }
221: 
222: 
223:   /**
224:   * Don't actually send the email, just log it.
225:   * @param string $additional_headers Any additional headers that are needed.
226:   */
227:   function PretendLog( $additional_headers = "" ) {
228:     if ( !empty($this->From) )     dbg_error_log('LOG', "From: $this->From");
229:     if ( !empty($this->Cc) )       dbg_error_log('LOG', "Cc: $this->Cc");
230:     if ( !empty($this->Bcc) )      dbg_error_log('LOG', "Bcc: $this->Bcc");
231:     if ( !empty($this->ReplyTo) )  dbg_error_log('LOG', "Reply-To: $this->ReplyTo");
232:     if ( !empty($this->ErrorsTo) ) dbg_error_log('LOG', "Errors-To: $this->ErrorsTo");
233: 
234:     $additional_parameters = "";
235:     if ( !empty($this->Sender) ) dbg_error_log('LOG', "Envelope Sender set to: $this->Sender");
236:     dbg_error_log('LOG', "To: $this->To");
237:     dbg_error_log('LOG', "Subject: $this->Subject");
238:     dbg_error_log('LOG', "Body: $this->Body");
239:   }
240: 
241:   /**
242:   * Don't actually send the email, just output it directly in the stream(!).  We use this method
243:   * when we're doing regression testing.
244:   * @param string $additional_headers Any additional headers that are needed.
245:   */
246:   function Pretend( $additional_headers = "" ) {
247:     if ( !empty($this->From) )     print("From: $this->From\r\n");
248:     if ( !empty($this->Cc) )       print("Cc: $this->Cc\r\n");
249:     if ( !empty($this->Bcc) )      print("Bcc: $this->Bcc\r\n");
250:     if ( !empty($this->ReplyTo) )  print("Reply-To: $this->ReplyTo\r\n");
251:     if ( !empty($this->ErrorsTo) ) print("Errors-To: $this->ErrorsTo\r\n");
252: 
253:     $additional_parameters = "";
254:     if ( !empty($this->Sender) ) print("Envelope Sender set to: $this->Sender\r\n");
255:     print("To: $this->To\r\n");
256:     print("Subject: $this->Subject\r\n");
257:     print("Body: $this->Body\r\n");
258:   }
259: }
260: 
AWL API documentation generated by ApiGen 2.8.0