PageRenderTime 32ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/mailer.php

https://github.com/ess/pdnsadministrator
PHP | 210 lines | 91 code | 19 blank | 100 comment | 8 complexity | dee98015c77816c5ff15f09f44efc380 MD5 | raw file
  1. <?php
  2. /**
  3. * PDNS-Admin
  4. * Copyright (c) 2006-2011 Roger Libiez http://www.iguanadons.net
  5. *
  6. * Based on Quicksilver Forums
  7. * Copyright (c) 2005-2011 The Quicksilver Forums Development Team
  8. * http://code.google.com/p/quicksilverforums/
  9. *
  10. * Based on MercuryBoard
  11. * Copyright (c) 2001-2005 The Mercury Development Team
  12. * http://www.mercuryboard.com/
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version 2
  17. * of the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. **/
  25. if (!defined('PDNSADMIN')) {
  26. header('HTTP/1.0 403 Forbidden');
  27. die;
  28. }
  29. /**
  30. * A simple class that makes good use of the mail() function.
  31. *
  32. * @author Daniel Wilhelm II Murdoch <wilhelm@cyberxtreme.org>
  33. * @since RC1
  34. **/
  35. class mailer
  36. {
  37. var $sender; // Name to be displayed in From field. @access private @var string
  38. var $outgoing; // Outgoing email address @access private @var string
  39. var $incoming; // Incoming email address @access private @var string
  40. var $message; // Email body @access private @var string
  41. var $subject; // Email Subject @access private @var string
  42. var $server; // Mail Server @access private @var string
  43. var $list; // Formatted Bcc list @access private @var string
  44. var $recipient; // Recipient for single send mode @access private @var string
  45. var $bcc; // Blind Carbon Copy recipients @access private @var array
  46. var $headers; // Headers to be used for email @access private @var array
  47. var $html; // Determines whether message will be sent in HTML format. @access private @var boolean
  48. /**
  49. * This initializes instance variables / objects.
  50. *
  51. * @author Daniel Wilhelm II Murdoch <wilhelm@cyberxtreme.org>
  52. * @since RC1
  53. * @return void
  54. **/
  55. function mailer($in, $out, $sender, $html)
  56. {
  57. $this->outgoing = $out;
  58. $this->incoming = $in;
  59. $this->sender = $sender;
  60. $this->html = false;
  61. $this->server = 'localhost';
  62. $this->recipient = '';
  63. $this->message = '';
  64. $this->subject = '';
  65. $this->list = '';
  66. $this->bcc = array();
  67. $this->headers = array();
  68. }
  69. /**
  70. * Adds a new recipient to Bcc.
  71. *
  72. * @param string $email User's email
  73. * @author Daniel Wilhelm II Murdoch <wilhelm@cyberxtreme.org>
  74. * @since RC1
  75. * @return void
  76. **/
  77. function setBcc($email)
  78. {
  79. $this->bcc[] = $email;
  80. }
  81. /**
  82. * Builds the Bcc list and assigns the corresponding header.
  83. *
  84. * @author Daniel Wilhelm II Murdoch <wilhelm@cyberxtreme.org>
  85. * @since RC1
  86. * @return boolean true if bcc recipients exist
  87. **/
  88. function doBcc()
  89. {
  90. if (!$this->bcc) {
  91. return false;
  92. }
  93. $this->list .= implode(', ', $this->bcc);
  94. $this->setHeader('Bcc: ' . $this->list);
  95. return true;
  96. }
  97. /**
  98. * Sets a recipient for single send mode.
  99. *
  100. * @param string $recipient Email address
  101. * @author Daniel Wilhelm II Murdoch <wilhelm@cyberxtreme.org>
  102. * @since RC1
  103. * @return void
  104. **/
  105. function setRecipient($recipient)
  106. {
  107. $this->recipient = $recipient;
  108. }
  109. /**
  110. * Sets the email's subject
  111. *
  112. * @param string $subject Email Subject
  113. * @author Daniel Wilhelm II Murdoch <wilhelm@cyberxtreme.org>
  114. * @since RC1
  115. * @return void
  116. **/
  117. function setSubject($subject)
  118. {
  119. // Basic security check
  120. $subject = str_replace(array("\n", "\r"), array(' ', ' '), $subject);
  121. $this->subject = $subject;
  122. }
  123. /**
  124. * Sets the email's message body
  125. *
  126. * @param string $subject Email Subject
  127. * @author Daniel Wilhelm II Murdoch <wilhelm@cyberxtreme.org>
  128. * @since RC1
  129. * @return void
  130. **/
  131. function setMessage($message)
  132. {
  133. $this->message = $message;
  134. }
  135. /**
  136. * Adds a key / value to the current list of headers.
  137. *
  138. * @param string $header New header
  139. * @author Daniel Wilhelm II Murdoch <wilhelm@cyberxtreme.org>
  140. * @since RC1
  141. * @return void
  142. **/
  143. function setHeader($header)
  144. {
  145. // Basic security check
  146. $header = str_replace(array("\n", "\r"), array(' ', ' '), $header);
  147. $this->headers[] = $header;
  148. }
  149. /**
  150. * Changes the SMTP mail server
  151. *
  152. * @param string $server Mail Server
  153. * @author Jason Warner <jason@mercuryboard.com>
  154. * @since RC1
  155. * @return void
  156. **/
  157. function setServer($server)
  158. {
  159. $this->server = $server;
  160. }
  161. /**
  162. * Build the email message and send it off!
  163. *
  164. * @author Daniel Wilhelm II Murdoch <wilhelm@cyberxtreme.org>
  165. * @since RC1
  166. * @return boolean true on success
  167. **/
  168. function doSend()
  169. {
  170. if (!strlen($this->subject)) {
  171. return false;
  172. }
  173. $this->setHeader('From: ' . $this->sender . ' <' . $this->outgoing . '>');
  174. $this->setHeader('Reply-To: ' . $this->incoming);
  175. if (!strlen($this->recipient)) {
  176. $this->doBcc();
  177. $to = $this->sender . ' <' . $this->outgoing . '>';
  178. } else {
  179. $to = $this->recipient;
  180. }
  181. if ($this->server) {
  182. @ini_set('SMTP', $this->server);
  183. }
  184. if (mail($to, $this->subject, $this->message, implode("\n", $this->headers))) {
  185. return true;
  186. } else {
  187. return false;
  188. }
  189. }
  190. }
  191. ?>