PHPonTrax
[ class tree: PHPonTrax ] [ index: PHPonTrax ] [ all elements ]

Source for file action_mailer.php

Documentation is available at action_mailer.php

  1. <?php
  2. /**
  3. * File for ActionMailer class
  4. *
  5. * (PHP 5)
  6. *
  7. * @package PHPonTrax
  8. * @version $Id: action_mailer.php 162 2006-03-06 07:32:02Z john $
  9. * @copyright (c) 2005 John Peterson
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining
  12. * a copy of this software and associated documentation files (the
  13. * "Software"), to deal in the Software without restriction, including
  14. * without limitation the rights to use, copy, modify, merge, publish,
  15. * distribute, sublicense, and/or sell copies of the Software, and to
  16. * permit persons to whom the Software is furnished to do so, subject to
  17. * the following conditions:
  18. *
  19. * The above copyright notice and this permission notice shall be
  20. * included in all copies or substantial portions of the Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. */
  30.  
  31. /**
  32. *
  33. */
  34. include_once( "Mail.php" );
  35. include_once( "Mail/mime.php" );
  36.  
  37. /**
  38. *
  39. * @package PHPonTrax
  40. */
  41. class ActionMailer {
  42.  
  43. private
  44. $mail_mime, // Mail_mime object
  45. $to_addresses,
  46. $cc_addresses,
  47. $bcc_addresses,
  48. $replyto_addresses;
  49. public
  50. $crlf = "\r\n",
  51. $smtp_params = array("host"=>"localhost", "port"=>"25"),
  52. $send_type = "mail", // smtp or mail
  53. $subject = null, // email subject
  54. $from_address = "no-reply@nodomain.com",
  55. $from_name = null,
  56. $error = null;
  57.  
  58. function __construct($crlf = null) {
  59. if(!is_null($crlf)) {
  60. $this->crlf = $crlf;
  61. }
  62. $this->mail_mime = new Mail_mime();
  63. }
  64.  
  65. function add_to_address($address, $name = null) {
  66. $cur = count($this->to_addresses);
  67. $this->to_addresses[$cur][0] = trim($address);
  68. $this->to_addresses[$cur][1] = trim($name);
  69. }
  70.  
  71. function add_cc_address($address, $name = null) {
  72. $cur = count($this->cc);
  73. $this->cc_addresses[$cur][0] = trim($address);
  74. $this->cc_addresses[$cur][1] = trim($name);
  75. }
  76.  
  77. function add_bcc_address($address, $name = null) {
  78. $cur = count($this->bcc);
  79. $this->bcc_addresses[$cur][0] = trim($address);
  80. $this->bcc_addresses[$cur][1] = trim($name);
  81. }
  82.  
  83. function add_replyto_address($address, $name = null) {
  84. $cur = count($this->ReplyTo);
  85. $this->replyto_addresses[$cur][0] = trim($address);
  86. $this->replyto_addresses[$cur][1] = trim($name);
  87. }
  88.  
  89. function set_from_address($address, $name = null) {
  90. $this->from_address = trim($address);
  91. if(!is_null($name))
  92. $this->from_name = trim($name);
  93. }
  94.  
  95. function set_text_body($text) {
  96. if(strlen($text) > 0)
  97. $this->mail_mime->setTxtBody($text);
  98. }
  99.  
  100. function set_html_body($html) {
  101. if(strlen($html) > 0)
  102. $this->mail_mime->setHTMLBody($html);
  103. }
  104. function set_subject($subject) {
  105. $this->subject = $subject;
  106. }
  107.  
  108. function add_attachment($file, $content_type ='application/octet-stream', $file_name = '', $is_file = true, $encoding = 'base64') {
  109. $this->mail_mime->addAttachment($file, $content_type, $file_name, $is_file, $encoding);
  110. }
  111.  
  112. function format_address($address) {
  113. if(empty($address[1]))
  114. $formatted = $address[0];
  115. else
  116. $formatted = sprintf('"%s" <%s>', $address[1], $address[0]);
  117.  
  118. // now validate if the email address
  119. if(!$this->validate_email($address[0])) {
  120. $this->error .= "Invalid email address: ".$address[0]."<br>";
  121. }
  122.  
  123. return $formatted;
  124. }
  125.  
  126. function validate_email($email) {
  127. if(eregi("^[a-zA-Z0-9._-]+@([a-zA-Z0-9._-]+\.)+([a-zA-Z0-9_-]){2,4}$", $email)) {
  128. return true;
  129. }
  130. return false;
  131. }
  132.  
  133. function set_header_line($header_key, $header_value) {
  134. if($header_key && $header_value) {
  135. $this->headers[$header_key] = $header_value;
  136. }
  137. }
  138.  
  139. function set_headers($extra_headers = null) {
  140. if(is_array($this->to_addresses)) {
  141. foreach($this->to_addresses as $to_address) {
  142. $to_addresses[] = $this->format_address($to_address);
  143. }
  144. if(is_array($to_addresses)) $to_addresses = implode(",", $to_addresses);
  145. if(!empty($to_addresses))
  146. $this->set_header_line("To", $to_addresses);
  147. }
  148.  
  149. if(is_array($this->cc_addresses)) {
  150. foreach($this->cc_addresses as $cc_address) {
  151. $cc_addresses[] = $this->format_address($cc_address);
  152. }
  153. if(is_array($cc_addresses)) $cc_addresses = implode(",", $cc_addresses);
  154. if(!empty($cc_addresses))
  155. $this->set_header_line("Cc", $cc_addresses);
  156. }
  157.  
  158. if(is_array($this->bcc_addresses)) {
  159. foreach($this->bcc_addresses as $bcc_address) {
  160. $bcc_addresses[] = $this->format_address($bcc_address);
  161. }
  162. if(is_array($bcc_addresses)) $bcc_addresses = implode(",", $bcc_addresses);
  163. if(!empty($bcc_addresses))
  164. $this->set_header_line("Bcc", $bcc_addresses);
  165. }
  166.  
  167. if(is_array($this->replyto_addresses)) {
  168. foreach($this->replyto_addresses as $replyto_address) {
  169. $replyto_addresses[] = $this->format_address($replyto_address);
  170. }
  171. if(is_array($replyto_addresses)) $replyto_addresses = implode(",", $replyto_addresses);
  172. if(!empty($replyto_addresses))
  173. $this->set_header_line("Reply-To", $replyto_addresses);
  174. }
  175.  
  176. if(!is_null($this->subject)) {
  177. $this->set_header_line("Subject", $this->subject);
  178. } else {
  179. $this->set_header_line("Subject", "");
  180. }
  181.  
  182. if(!is_null($this->from_address)) {
  183. $from_address = $this->format_address(array("0"=>$this->from_address, "1"=>$this->from_name));
  184. $this->set_header_line("From", $from_address);
  185. }
  186.  
  187. if(is_array($extra_headers)) {
  188. foreach($extra_headers as $extra_key => $extra_value) {
  189. if(!empty($extra_key) && !empty($extra_value))
  190. $this->set_header_line($extra_key, $extra_value);
  191. }
  192. }
  193.  
  194. if(!array_key_exists("Date", $this->headers))
  195. $this->set_header_line("Date", date("r"));
  196. if(!array_key_exists("Return-Path", $this->headers) && !is_null($this->from_address))
  197. $this->set_header_line("Return-Path", $this->from_address);
  198. if(!array_key_exists("Reply-To", $this->headers) && !is_null($this->from_address))
  199. $this->set_header_line("Reply-To", $this->from_address);
  200. }
  201.  
  202. function send($to_address = null, $subject = null, $text_body = null, $html_body = null, $extra_headers = null) {
  203. if(!is_null($to_address)) $this->add_to_address($to_address);
  204. if(!is_null($subject)) $this->set_subject($subject);
  205. if(!is_null($html_body)) $this->set_html_body($html_body);
  206. if(!is_null($text_body)) $this->set_text_body($text_body);
  207.  
  208. $body = $this->mail_mime->get();
  209. $this->set_headers($extra_headers);
  210. $headers = $this->mail_mime->headers($this->headers);
  211.  
  212. if($this->send_type == "smtp") {
  213. $mail =& Mail::factory("smtp", $this->smtp_params);
  214. } else {
  215. $mail =& Mail::factory("mail");
  216. }
  217.  
  218. if(!$this->error) {
  219. $result = $mail->send(null, $headers, $body);
  220. if(is_object($result)) {
  221. $this->error = $result->getMessage();
  222. return false;
  223. } else {
  224. return true;
  225. }
  226. } else {
  227. return false;
  228. }
  229. }
  230.  
  231. }
  232.  
  233.  
  234. ?>

Documentation generated on Thu, 04 May 2006 19:47:03 -0600 by phpDocumentor 1.3.0RC4