/core/model/modx/mail/modphpmailer.class.php

https://github.com/hatone/revolution · PHP · 251 lines · 168 code · 11 blank · 72 comment · 11 complexity · c61521b2dfc20595c74eef99da747181 MD5 · raw file

  1. <?php
  2. /**
  3. * This file contains the PHPMailer implementation of the modMail email service.
  4. * @package modx
  5. * @subpackage mail
  6. */
  7. require_once MODX_CORE_PATH . 'model/modx/mail/modmail.class.php';
  8. /**
  9. * PHPMailer implementation of the modMail service.
  10. *
  11. * @package modx
  12. * @subpackage mail
  13. */
  14. class modPHPMailer extends modMail {
  15. /**
  16. * Constructs a new instance of the modPHPMailer class.
  17. *
  18. * @param modX $modx A reference to the modX instance
  19. * @param array $attributes An array of attributes for the instance
  20. * @return modPHPMailer
  21. */
  22. function __construct(modX &$modx, array $attributes= array()) {
  23. parent :: __construct($modx, $attributes);
  24. require_once $modx->getOption('core_path') . 'model/modx/mail/phpmailer/class.phpmailer.php';
  25. $this->_getMailer();
  26. }
  27. /**
  28. * Sets a PHPMailer attribute corresponding to the modX::MAIL_* constants or
  29. * a custom key.
  30. *
  31. * @param string $key The attribute key to set
  32. * @param mixed $value The value to set
  33. */
  34. public function set($key, $value) {
  35. parent :: set($key, $value);
  36. switch ($key) {
  37. case modMail::MAIL_BODY :
  38. $this->mailer->Body= $this->attributes[$key];
  39. break;
  40. case modMail::MAIL_BODY_TEXT :
  41. $this->mailer->AltBody= $this->attributes[$key];
  42. break;
  43. case modMail::MAIL_CHARSET :
  44. $this->mailer->CharSet= $this->attributes[$key];
  45. break;
  46. case modMail::MAIL_CONTENT_TYPE :
  47. $this->mailer->ContentType= $this->attributes[$key];
  48. break;
  49. case modMail::MAIL_ENCODING :
  50. $this->mailer->Encoding= $this->attributes[$key];
  51. break;
  52. case modMail::MAIL_ENGINE :
  53. $this->mailer->Mailer= $this->attributes[$key];
  54. break;
  55. case modMail::MAIL_ENGINE_PATH :
  56. $this->mailer->Sendmail= $this->attributes[$key];
  57. break;
  58. case modMail::MAIL_FROM :
  59. $this->mailer->From= $this->attributes[$key];
  60. break;
  61. case modMail::MAIL_FROM_NAME :
  62. $this->mailer->FromName= $this->attributes[$key];
  63. break;
  64. case modMail::MAIL_HOSTNAME :
  65. $this->mailer->Hostname= $this->attributes[$key];
  66. break;
  67. case modMail::MAIL_LANGUAGE :
  68. $this->mailer->SetLanguage($this->attributes[$key]);
  69. break;
  70. case modMail::MAIL_PRIORITY :
  71. $this->mailer->Priority= $this->attributes[$key];
  72. break;
  73. case modMail::MAIL_READ_TO :
  74. $this->mailer->ConfirmReadingTo= $this->attributes[$key];
  75. break;
  76. case modMail::MAIL_SENDER :
  77. $this->mailer->Sender= $this->attributes[$key];
  78. break;
  79. case modMail::MAIL_SMTP_AUTH :
  80. $this->mailer->SMTPAuth= $this->attributes[$key];
  81. break;
  82. case modMail::MAIL_SMTP_HELO :
  83. $this->mailer->Helo= $this->attributes[$key];
  84. break;
  85. case modMail::MAIL_SMTP_HOSTS :
  86. $this->mailer->Host= $this->attributes[$key];
  87. break;
  88. case modMail::MAIL_SMTP_KEEPALIVE :
  89. $this->mailer->SMTPKeepAlive= $this->attributes[$key];
  90. break;
  91. case modMail::MAIL_SMTP_PASS :
  92. $this->mailer->Password= $this->attributes[$key];
  93. break;
  94. case modMail::MAIL_SMTP_PORT :
  95. $this->mailer->Port= $this->attributes[$key];
  96. break;
  97. case modMail::MAIL_SMTP_PREFIX :
  98. $this->mailer->SMTPSecure= $this->attributes[$key];
  99. break;
  100. case modMail::MAIL_SMTP_SINGLE_TO :
  101. $this->mailer->SingleTo= $this->attributes[$key];
  102. break;
  103. case modMail::MAIL_SMTP_TIMEOUT :
  104. $this->mailer->Timeout= $this->attributes[$key];
  105. break;
  106. case modMail::MAIL_SMTP_USER :
  107. $this->mailer->Username= $this->attributes[$key];
  108. break;
  109. case modMail::MAIL_SUBJECT :
  110. $this->mailer->Subject= $this->attributes[$key];
  111. break;
  112. default :
  113. $this->modx->log(modX::LOG_LEVEL_WARN, $this->modx->lexicon('mail_err_attr_nv',array('attr' => $key)));
  114. break;
  115. }
  116. }
  117. /**
  118. * Adds an address to the mailer
  119. *
  120. * @param string $type The type of address (to, reply-to, bcc, cc)
  121. * @param string $email The email address to address to
  122. * @param string $name The name of the email address
  123. * @return boolean True if was addressed
  124. */
  125. public function address($type, $email, $name= '') {
  126. $set= false;
  127. if ($email) {
  128. $set= parent :: address($type, $email, $name);
  129. if ($set) {
  130. $type= strtolower($type);
  131. switch ($type) {
  132. case 'to' :
  133. $this->mailer->AddAddress($email, $name);
  134. break;
  135. case 'cc' :
  136. $this->mailer->AddCC($email, $name);
  137. break;
  138. case 'bcc' :
  139. $this->mailer->AddBCC($email, $name);
  140. break;
  141. case 'reply-to' :
  142. $this->mailer->AddReplyTo($email, $name);
  143. break;
  144. }
  145. }
  146. } elseif ($email === null) {
  147. $this->modx->log(modX::LOG_LEVEL_ERROR, $this->modx->lexicon('mail_err_unset_spec'));
  148. } else {
  149. $this->modx->log(modX::LOG_LEVEL_ERROR, $this->modx->lexicon('mail_err_address_ns'));
  150. }
  151. return $set;
  152. }
  153. /**
  154. * Adds a custom header to the mailer
  155. *
  156. * @param string $header The header to set
  157. * @return boolean True if the header was successfully set
  158. */
  159. public function header($header) {
  160. $set= parent :: header($header);
  161. if ($set) {
  162. $this->mailer->AddCustomHeader($header);
  163. }
  164. return $set;
  165. }
  166. /**
  167. * Send the email, applying any attributes to the mailer before sending.
  168. *
  169. * @param array $attributes An array of attributes to pass when sending
  170. * @return boolean True if the email was successfully sent
  171. */
  172. public function send(array $attributes= array()) {
  173. $sent = parent :: send($attributes);
  174. $sent = $this->mailer->Send();
  175. return $sent;
  176. }
  177. /**
  178. * Resets all PHPMailer attributes, including recipients and attachments.
  179. *
  180. * @param array $attributes An array of attributes to pass when resetting
  181. */
  182. public function reset(array $attributes= array()) {
  183. parent :: reset($attributes);
  184. $this->mailer->ClearAllRecipients();
  185. $this->mailer->ClearReplyTos();
  186. $this->mailer->ClearAttachments();
  187. $this->mailer->ClearCustomHeaders();
  188. $this->mailer->IsHTML(false);
  189. }
  190. /**
  191. * Loads the PHPMailer object used to send the emails in this implementation.
  192. *
  193. * @return boolean True if the mailer class was successfully loaded
  194. */
  195. protected function _getMailer() {
  196. $success= false;
  197. if (!$this->mailer || !($this->mailer instanceof PHPMailer)) {
  198. if ($this->mailer= new PHPMailer()) {
  199. if (!empty($this->attributes)) {
  200. foreach ($this->attributes as $attrKey => $attrVal) {
  201. $this->set($attrKey, $attrVal);
  202. }
  203. }
  204. if (!isset($this->attributes[modMail::MAIL_LANGUAGE])) {
  205. $this->set(modMail::MAIL_LANGUAGE, $this->modx->config['manager_language']);
  206. }
  207. $success= true;
  208. }
  209. }
  210. return $success;
  211. }
  212. /**
  213. * Attaches a file to the mailer.
  214. *
  215. * @param mixed $file The file to attach
  216. * @param string $name The name of the file to attach as
  217. * @param string $encoding The encoding of the attachment
  218. * @param string $type The header type of the attachment
  219. */
  220. public function attach($file,$name = '',$encoding = 'base64',$type = 'application/octet-stream') {
  221. parent :: attach($file);
  222. $this->mailer->AddAttachment($file,$name,$encoding,$type);
  223. }
  224. /**
  225. * Clears all existing attachments.
  226. */
  227. public function clearAttachments() {
  228. parent :: clearAttachments();
  229. $this->mailer->ClearAttachments();
  230. }
  231. /**
  232. * Sets email to HTML or text-only.
  233. *
  234. * @access public
  235. * @param boolean $toggle True to set to HTML.
  236. */
  237. public function setHTML($toggle) {
  238. $this->mailer->IsHTML($toggle);
  239. }
  240. }