PageRenderTime 56ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/EMail.php

https://gitlab.com/karora/awl
PHP | 259 lines | 99 code | 34 blank | 126 comment | 23 complexity | 281fd2e9ceaf7e9c20e29d75fd9b03d9 MD5 | raw file
  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. require_once("AWLUtilities.php");
  11. /**
  12. * Lightweight class for sending an e-mail.
  13. * @package awl
  14. */
  15. class EMail
  16. {
  17. /**#@+
  18. * @access private
  19. */
  20. /**
  21. * A comma-separated list of addresses to send To
  22. * @var string
  23. */
  24. private $To; // To:
  25. /**
  26. * The visible sender of the e-mail.
  27. * @var string
  28. */
  29. private $From; // etc...
  30. /**
  31. * A comma-separated list of addresses to carbon-copy to
  32. * @var string
  33. */
  34. private $Cc;
  35. /**
  36. * A comma-separated list of addresses to blind carbon-copy to
  37. * @var string
  38. */
  39. private $Bcc;
  40. /**
  41. * A comma-separated list of addresses to set as the Errors-to: header
  42. * @var string
  43. */
  44. private $ErrorsTo;
  45. /**
  46. * A comma-separated list of addresses to set as the Reply-to: header
  47. * @var string
  48. */
  49. private $ReplyTo;
  50. /**
  51. * The address to set as the sender of the e-mail.
  52. * @var string
  53. */
  54. private $Sender;
  55. /**
  56. * The subject line of the email.
  57. * @var string
  58. */
  59. private $Subject;
  60. /**
  61. * The body of the email.
  62. * @var string
  63. */
  64. private $Body;
  65. /**#@-*/
  66. /**
  67. * Create the e-mail, optionally assigning the subject and primary recipient.
  68. * @param string $subject The subject line of the email.
  69. * @param string $to A comma-separated list of addresses for the primary recipient(s).
  70. */
  71. function __construct( $subject = "", $to = "" ) {
  72. // Initialise with some defaults
  73. $this->From = "";
  74. $this->Subject = $subject;
  75. $this->To = $to;
  76. $this->Cc = "";
  77. $this->Bcc = "";
  78. $this->ErrorsTo = "";
  79. $this->ReplyTo = "";
  80. $this->Sender = "";
  81. $this->Body = "";
  82. }
  83. /**
  84. * Append something with a comma delimter onto the existing referenced string
  85. * @param stringref &$onto The string we will be appending to.
  86. * @param string $extra What we will be appending
  87. * @return string The new string.
  88. */
  89. private function _AppendDelimited( &$onto, $extra ) {
  90. if ( !isset($extra) || $extra == "" ) return false;
  91. if ( $onto != "" ) $onto .= ", ";
  92. $onto .= $extra;
  93. return $onto;
  94. }
  95. /**
  96. * Add another recipient to the email
  97. * @param string $recipient The email address to append.
  98. * @return string The new recipient list.
  99. */
  100. function AddTo( $recipient ) {
  101. return $this->_AppendDelimited($this->To, $recipient);
  102. }
  103. /**
  104. * Get the current recipient list.
  105. * @return string The current recipient list.
  106. */
  107. function To() {
  108. return $this->To;
  109. }
  110. /**
  111. * Add another Cc recipient to the email
  112. * @param string $recipient The email address to append.
  113. * @return string The new Cc recipient list.
  114. */
  115. function AddCc( $recipient ) {
  116. return $this->_AppendDelimited($this->Cc, $recipient);
  117. }
  118. /**
  119. * Add another Bcc recipient to the email
  120. * @param string $recipient The email address to append.
  121. * @return string The new Bcc recipient list.
  122. */
  123. function AddBcc( $recipient ) {
  124. return $this->_AppendDelimited($this->Bcc, $recipient);
  125. }
  126. /**
  127. * Add another Reply-to address to the email
  128. * @param string $recipient The email address to append.
  129. * @return string The new Reply-to list.
  130. */
  131. function AddReplyTo( $recipient ) {
  132. return $this->_AppendDelimited($this->ReplyTo, $recipient);
  133. }
  134. /**
  135. * Add another Error recipient to the email
  136. * @param string $recipient The email address to append.
  137. * @return string The new Error recipient list.
  138. */
  139. function AddErrorsTo( $recipient ) {
  140. return $this->_AppendDelimited($this->ErrorsTo, $recipient);
  141. }
  142. /**
  143. * Set the visible From address for the e-mail.
  144. * @param string $recipient The visible From address
  145. * @return string The new From address
  146. */
  147. function SetFrom( $sender ) {
  148. $this->From = $sender;
  149. return $sender;
  150. }
  151. /**
  152. * Set the envelope sender address for the e-mail.
  153. * @param string $recipient The e-mail address for the sender
  154. * @return string The new envelope sender address.
  155. */
  156. function SetSender( $sender ) {
  157. $this->Sender = $sender;
  158. return $sender;
  159. }
  160. /**
  161. * Set the subject line for the email
  162. * @param string $recipient The new subject line.
  163. * @return string The new subject line.
  164. */
  165. function SetSubject( $subject ) {
  166. $this->Subject = $subject;
  167. return $subject;
  168. }
  169. /**
  170. * Set the body of the e-mail.
  171. * @param string $recipient The email address to append.
  172. * @return string The new body of the e-mail.
  173. */
  174. function SetBody( $body ) {
  175. $this->Body = $body;
  176. return $body;
  177. }
  178. /**
  179. * Actually send the email
  180. * @param string $additional_headers Any additional headers that are needed.
  181. */
  182. function Send( $additional_headers = "" ) {
  183. if ( !empty($this->From) ) $additional_headers .= "From: $this->From\r\n";
  184. if ( !empty($this->Cc) ) $additional_headers .= "Cc: $this->Cc\r\n";
  185. if ( !empty($this->Bcc) ) $additional_headers .= "Bcc: $this->Bcc\r\n";
  186. if ( !empty($this->ReplyTo) ) $additional_headers .= "Reply-To: $this->ReplyTo\r\n";
  187. if ( !empty($this->ErrorsTo) ) $additional_headers .= "Errors-To: $this->ErrorsTo\r\n";
  188. $additional_parameters = "";
  189. if ( !empty($this->Sender) ) $additional_parameters = "-f$this->Sender";
  190. mail( $this->To, $this->Subject, $this->Body, $additional_headers, $additional_parameters );
  191. }
  192. /**
  193. * Don't actually send the email, just log it.
  194. * @param string $additional_headers Any additional headers that are needed.
  195. */
  196. function PretendLog( $additional_headers = "" ) {
  197. if ( !empty($this->From) ) dbg_error_log('LOG', "From: $this->From");
  198. if ( !empty($this->Cc) ) dbg_error_log('LOG', "Cc: $this->Cc");
  199. if ( !empty($this->Bcc) ) dbg_error_log('LOG', "Bcc: $this->Bcc");
  200. if ( !empty($this->ReplyTo) ) dbg_error_log('LOG', "Reply-To: $this->ReplyTo");
  201. if ( !empty($this->ErrorsTo) ) dbg_error_log('LOG', "Errors-To: $this->ErrorsTo");
  202. $additional_parameters = "";
  203. if ( !empty($this->Sender) ) dbg_error_log('LOG', "Envelope Sender set to: $this->Sender");
  204. dbg_error_log('LOG', "To: $this->To");
  205. dbg_error_log('LOG', "Subject: $this->Subject");
  206. dbg_error_log('LOG', "Body: $this->Body");
  207. }
  208. /**
  209. * Don't actually send the email, just output it directly in the stream(!). We use this method
  210. * when we're doing regression testing.
  211. * @param string $additional_headers Any additional headers that are needed.
  212. */
  213. function Pretend( $additional_headers = "" ) {
  214. if ( !empty($this->From) ) print("From: $this->From\r\n");
  215. if ( !empty($this->Cc) ) print("Cc: $this->Cc\r\n");
  216. if ( !empty($this->Bcc) ) print("Bcc: $this->Bcc\r\n");
  217. if ( !empty($this->ReplyTo) ) print("Reply-To: $this->ReplyTo\r\n");
  218. if ( !empty($this->ErrorsTo) ) print("Errors-To: $this->ErrorsTo\r\n");
  219. $additional_parameters = "";
  220. if ( !empty($this->Sender) ) print("Envelope Sender set to: $this->Sender\r\n");
  221. print("To: $this->To\r\n");
  222. print("Subject: $this->Subject\r\n");
  223. print("Body: $this->Body\r\n");
  224. }
  225. }