PageRenderTime 52ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/library/core/class.email.php

https://github.com/mischka/Garden
PHP | 219 lines | 93 code | 26 blank | 100 comment | 17 complexity | eb930affb8caa6a81a438bafd1e11ecc MD5 | raw file
  1. <?php if (!defined('APPLICATION')) exit();
  2. /*
  3. Copyright 2008, 2009 Mark O'Sullivan
  4. This file is part of Garden.
  5. Garden is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  6. Garden is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  7. You should have received a copy of the GNU General Public License along with Garden. If not, see <http://www.gnu.org/licenses/>.
  8. Contact Mark O'Sullivan at mark [at] lussumo [dot] com
  9. */
  10. /**
  11. * Object Representation of an email. All public methods return $this for
  12. * chaining purposes. ie. $Email->Subject('Hi')->Message('Just saying hi!')-
  13. * To('joe@lussumo.com')->Send();
  14. *
  15. * @author Mark O'Sullivan
  16. * @copyright 2003 Mark O'Sullivan
  17. * @license http://www.opensource.org/licenses/gpl-2.0.php GPL
  18. * @package Garden
  19. * @version @@GARDEN-VERSION@@
  20. * @todo This class needs to be tested on a function mail server and with SMTP
  21. * @namespace Lussumo.Garden.Core
  22. */
  23. class Gdn_Email extends Gdn_Pluggable {
  24. /**
  25. * @var PHPMailer
  26. */
  27. private $_PhpMailer;
  28. /**
  29. * @var boolean
  30. */
  31. private $_IsToSet;
  32. /**
  33. * Constructor
  34. */
  35. function __construct() {
  36. $this->_PhpMailer = new PHPMailer();
  37. $this->Clear();
  38. parent::__construct();
  39. }
  40. /**
  41. * Adds to the "Bcc" recipient collection.
  42. *
  43. * @param mixed $RecipientEmail An email (or array of emails) to add to the "Bcc" recipient collection.
  44. * @param string $RecipientName The recipient name associated with $RecipientEmail. If $RecipientEmail is
  45. * an array of email addresses, this value will be ignored.
  46. * @return Email
  47. */
  48. public function Bcc($RecipientEmail, $RecipientName = '') {
  49. $this->_PhpMailer->AddBCC($RecipientEmail, $RecipientName);
  50. return $this;
  51. }
  52. /**
  53. * Adds to the "Cc" recipient collection.
  54. *
  55. * @param mixed $RecipientEmail An email (or array of emails) to add to the "Cc" recipient collection.
  56. * @param string $RecipientName The recipient name associated with $RecipientEmail. If $RecipientEmail is
  57. * an array of email addresses, this value will be ignored.
  58. * @return Email
  59. */
  60. public function Cc($RecipientEmail, $RecipientName = '') {
  61. $this->_PhpMailer->AddCC($RecipientEmail, $RecipientName);
  62. return $this;
  63. }
  64. /**
  65. * Clears out all previously specified values for this object and restores
  66. * it to the state it was in when it was instantiated.
  67. *
  68. * @return Email
  69. */
  70. public function Clear() {
  71. $this->_PhpMailer->ClearAllRecipients();
  72. $this->_PhpMailer->Body = '';
  73. $this->From();
  74. $this->_IsToSet = False;
  75. $this->MimeType(Gdn::Config('Garden.Email.MimeType', 'text/plain'));
  76. $this->_MasterView = 'email.master';
  77. return $this;
  78. }
  79. /**
  80. * Allows the explicit definition of the email's sender address & name.
  81. * Defaults to the applications Configuration 'SupportEmail' & 'SupportName'
  82. * settings respectively.
  83. *
  84. * @param string $SenderEmail
  85. * @param string $SenderName
  86. * @return Email
  87. */
  88. public function From($SenderEmail = '', $SenderName = '') {
  89. if ($SenderEmail == '')
  90. $SenderEmail = Gdn::Config('Garden.Email.SupportAddress', '');
  91. if ($SenderName == '')
  92. $SenderName = Gdn::Config('Garden.Email.SupportName', '');
  93. $this->_PhpMailer->From = $SenderEmail;
  94. $this->_PhpMailer->FromName = $SenderName;
  95. return $this;
  96. }
  97. /**
  98. * Allows the definition of a masterview other than the default:
  99. * "email.master".
  100. *
  101. * @param string $MasterView
  102. * @todo To implement
  103. * @return Email
  104. */
  105. public function MasterView($MasterView) {
  106. return $this;
  107. }
  108. /**
  109. * The message to be sent.
  110. *
  111. * @param string $Message The message to be sent.
  112. * @tod: implement
  113. * @return Email
  114. */
  115. public function Message($Message) {
  116. if ($this->_PhpMailer->ContentType == 'text/html') {
  117. $this->_PhpMailer->MsgHTML($Message);
  118. } else {
  119. $this->_PhpMailer->Body = $Message;
  120. }
  121. return $this;
  122. }
  123. /**
  124. * Sets the mime-type of the email.
  125. *
  126. * Only accept text/plain or text/html.
  127. *
  128. * @param string $MimeType The mime-type of the email.
  129. * @return Email
  130. */
  131. public function MimeType($MimeType) {
  132. $this->_PhpMailer->IsHTML($MimeType === 'text/html');
  133. return $this;
  134. }
  135. /**
  136. * @todo add port settings
  137. */
  138. public function Send() {
  139. if (Gdn::Config('Garden.Email.UseSmtp')) {
  140. $this->_PhpMailer->IsSMTP();
  141. $SmtpHost = Gdn::Config('Garden.Email.SmtpHost', '');
  142. $SmtpPort = Gdn::Config('Garden.Email.SmtpPort', 25);
  143. if (strpos($SmtpHost, ':') !== FALSE) {
  144. list($SmtpHost, $SmtpPort) = explode(':', $SmtpHost);
  145. }
  146. $this->_PhpMailer->Host = $SmtpHost;
  147. $this->_PhpMailer->Port = $SmtpPort;
  148. $this->_PhpMailer->Username = $Username = Gdn::Config('Garden.Email.SmtpUser', '');
  149. $this->_PhpMailer->Password = $Password = Gdn::Config('Garden.Email.SmtpPassword', '');
  150. if(!empty($Username))
  151. $this->_PhpMailer->SMTPAuth = TRUE;
  152. } else {
  153. $this->_PhpMailer->IsMail();
  154. }
  155. if (!$this->_PhpMailer->Send()) {
  156. throw new Exception($this->_PhpMailer->ErrorInfo);
  157. }
  158. return true;
  159. }
  160. /**
  161. * Adds subject of the message to the email.
  162. *
  163. * @param string $Subject The subject of the message.
  164. */
  165. public function Subject($Subject) {
  166. $this->_PhpMailer->Subject = $Subject;
  167. }
  168. /**
  169. * Adds to the "To" recipient collection.
  170. *
  171. * @param mixed $RecipientEmail An email (or array of emails) to add to the "To" recipient collection.
  172. * @param string $RecipientName The recipient name associated with $RecipientEmail. If $RecipientEmail is
  173. * an array of email addresses, this value will be ignored.
  174. */
  175. public function To($RecipientEmail, $RecipientName = '') {
  176. // Only allow one address in the "to" field. Append all extras to the "cc" field.
  177. if (!$this->_IsToSet) {
  178. $this->_PhpMailer->AddAddress($RecipientEmail, $RecipientName);
  179. $this->_IsToSet = True;
  180. }
  181. else {
  182. $this->Cc($RecipientEmail, $RecipientName);
  183. }
  184. return $this;
  185. }
  186. public function Charset($Use = ''){
  187. if ($Use != '') {
  188. $this->_PhpMailer->Charset = $Use;
  189. return $this;
  190. }
  191. return $this->_PhpMailer->Charset;
  192. }
  193. }