modphpmailer.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. /*
  3. * This file is part of MODX Revolution.
  4. *
  5. * Copyright (c) MODX, LLC. All Rights Reserved.
  6. *
  7. * For complete copyright and license information, see the COPYRIGHT and LICENSE
  8. * files found in the top-level directory of this distribution.
  9. */
  10. require_once MODX_CORE_PATH . 'model/modx/mail/modmail.class.php';
  11. /**
  12. * PHPMailer implementation of the modMail service.
  13. *
  14. * @package modx
  15. * @subpackage mail
  16. */
  17. class modPHPMailer extends modMail {
  18. /**
  19. * Constructs a new instance of the modPHPMailer class.
  20. *
  21. * @param modX $modx A reference to the modX instance
  22. * @param array $attributes An array of attributes for the instance
  23. * @return modPHPMailer
  24. */
  25. function __construct(modX &$modx, array $attributes= array()) {
  26. parent :: __construct($modx, $attributes);
  27. require_once $modx->getOption('core_path') . 'model/modx/mail/phpmailer/class.phpmailer.php';
  28. $this->_getMailer();
  29. }
  30. /**
  31. * Sets a PHPMailer attribute corresponding to the modX::MAIL_* constants or
  32. * a custom key.
  33. *
  34. * @param string $key The attribute key to set
  35. * @param mixed $value The value to set
  36. */
  37. public function set($key, $value) {
  38. parent :: set($key, $value);
  39. switch ($key) {
  40. case modMail::MAIL_BODY :
  41. $this->mailer->Body= $this->attributes[$key];
  42. break;
  43. case modMail::MAIL_BODY_TEXT :
  44. $this->mailer->AltBody= $this->attributes[$key];
  45. break;
  46. case modMail::MAIL_CHARSET :
  47. $this->mailer->CharSet= $this->attributes[$key];
  48. break;
  49. case modMail::MAIL_CONTENT_TYPE :
  50. $this->mailer->ContentType= $this->attributes[$key];
  51. break;
  52. case modMail::MAIL_ENCODING :
  53. $this->mailer->Encoding= $this->attributes[$key];
  54. break;
  55. case modMail::MAIL_ENGINE :
  56. $this->mailer->Mailer= $this->attributes[$key];
  57. break;
  58. case modMail::MAIL_ENGINE_PATH :
  59. $this->mailer->Sendmail= $this->attributes[$key];
  60. break;
  61. case modMail::MAIL_FROM :
  62. $this->mailer->From= $this->attributes[$key];
  63. $this->mailer->Sender= $this->attributes[$key];
  64. break;
  65. case modMail::MAIL_FROM_NAME :
  66. $this->mailer->FromName= $this->attributes[$key];
  67. break;
  68. case modMail::MAIL_HOSTNAME :
  69. $this->mailer->Hostname= $this->attributes[$key];
  70. break;
  71. case modMail::MAIL_LANGUAGE :
  72. $this->mailer->setLanguage($this->attributes[$key]);
  73. break;
  74. case modMail::MAIL_PRIORITY :
  75. $this->mailer->Priority= $this->attributes[$key];
  76. break;
  77. case modMail::MAIL_READ_TO :
  78. $this->mailer->ConfirmReadingTo= $this->attributes[$key];
  79. break;
  80. case modMail::MAIL_SENDER :
  81. $this->mailer->Sender= $this->attributes[$key];
  82. break;
  83. case modMail::MAIL_SMTP_AUTH :
  84. $this->mailer->SMTPAuth= $this->attributes[$key];
  85. break;
  86. case modMail::MAIL_SMTP_HELO :
  87. $this->mailer->Helo= $this->attributes[$key];
  88. break;
  89. case modMail::MAIL_SMTP_HOSTS :
  90. $this->mailer->Host= $this->attributes[$key];
  91. break;
  92. case modMail::MAIL_SMTP_KEEPALIVE :
  93. $this->mailer->SMTPKeepAlive= $this->attributes[$key];
  94. break;
  95. case modMail::MAIL_SMTP_PASS :
  96. $this->mailer->Password= $this->attributes[$key];
  97. break;
  98. case modMail::MAIL_SMTP_PORT :
  99. $this->mailer->Port= $this->attributes[$key];
  100. break;
  101. case modMail::MAIL_SMTP_PREFIX :
  102. $this->mailer->SMTPSecure= $this->attributes[$key];
  103. break;
  104. case modMail::MAIL_SMTP_SINGLE_TO :
  105. $this->mailer->SingleTo= $this->attributes[$key];
  106. break;
  107. case modMail::MAIL_SMTP_TIMEOUT :
  108. $this->mailer->Timeout= $this->attributes[$key];
  109. break;
  110. case modMail::MAIL_SMTP_USER :
  111. $this->mailer->Username= $this->attributes[$key];
  112. break;
  113. case modMail::MAIL_SUBJECT :
  114. $this->mailer->Subject= $this->attributes[$key];
  115. break;
  116. case modMail::MAIL_DKIM_SELECTOR :
  117. $this->mailer->DKIM_selector= $this->attributes[$key];
  118. break;
  119. case modMail::MAIL_DKIM_IDENTITY :
  120. $this->mailer->DKIM_identity= $this->attributes[$key];
  121. break;
  122. case modMail::MAIL_DKIM_DOMAIN :
  123. $this->mailer->DKIM_domain= $this->attributes[$key];
  124. break;
  125. case modMail::MAIL_DKIM_PRIVATEKEYFILE :
  126. $this->mailer->DKIM_private= $this->attributes[$key];
  127. break;
  128. case modMail::MAIL_DKIM_PRIVATEKEYSTRING :
  129. $this->mailer->DKIM_private_string= $this->attributes[$key];
  130. break;
  131. case modMail::MAIL_DKIM_PASSPHRASE :
  132. $this->mailer->DKIM_passphrase= $this->attributes[$key];
  133. break;
  134. default :
  135. $this->modx->log(modX::LOG_LEVEL_WARN, $this->modx->lexicon('mail_err_attr_nv',array('attr' => $key)));
  136. break;
  137. }
  138. }
  139. /**
  140. * Adds an address to the mailer
  141. *
  142. * @param string $type The type of address (to, reply-to, bcc, cc)
  143. * @param string $email The email address to address to
  144. * @param string $name The name of the email address
  145. * @return boolean True if was addressed
  146. */
  147. public function address($type, $email, $name= '') {
  148. $set= false;
  149. if ($email) {
  150. $set= parent :: address($type, $email, $name);
  151. if ($set) {
  152. $type= strtolower($type);
  153. switch ($type) {
  154. case 'to' :
  155. $this->mailer->addAddress($email, $name);
  156. break;
  157. case 'cc' :
  158. $this->mailer->addCC($email, $name);
  159. break;
  160. case 'bcc' :
  161. $this->mailer->addBCC($email, $name);
  162. break;
  163. case 'reply-to' :
  164. $this->mailer->addReplyTo($email, $name);
  165. break;
  166. }
  167. }
  168. } elseif ($email === null) {
  169. $this->modx->log(modX::LOG_LEVEL_ERROR, $this->modx->lexicon('mail_err_unset_spec'));
  170. } else {
  171. $this->modx->log(modX::LOG_LEVEL_ERROR, $this->modx->lexicon('mail_err_address_ns'));
  172. }
  173. return $set;
  174. }
  175. /**
  176. * Adds a custom header to the mailer
  177. *
  178. * @param string $header The header to set
  179. * @return boolean True if the header was successfully set
  180. */
  181. public function header($header) {
  182. $set= parent :: header($header);
  183. if ($set) {
  184. $this->mailer->addCustomHeader($header);
  185. }
  186. return $set;
  187. }
  188. /**
  189. * Send the email, applying any attributes to the mailer before sending.
  190. *
  191. * @param array $attributes An array of attributes to pass when sending
  192. * @return boolean True if the email was successfully sent
  193. */
  194. public function send(array $attributes= array()) {
  195. parent :: send($attributes);
  196. $sent = $this->mailer->send();
  197. if ($sent !== true) {
  198. $this->error = $this->modx->getService('error.modError');
  199. $this->error->addError($this->mailer->ErrorInfo);
  200. }
  201. return $sent;
  202. }
  203. /**
  204. * Resets all PHPMailer attributes, including recipients and attachments.
  205. *
  206. * @param array $attributes An array of attributes to pass when resetting
  207. */
  208. public function reset(array $attributes= array()) {
  209. parent :: reset($attributes);
  210. $this->mailer->clearAllRecipients();
  211. $this->mailer->clearReplyTos();
  212. $this->mailer->clearAttachments();
  213. $this->mailer->clearCustomHeaders();
  214. $this->mailer->isHTML(false);
  215. }
  216. /**
  217. * Loads the PHPMailer object used to send the emails in this implementation.
  218. *
  219. * @return boolean True if the mailer class was successfully loaded
  220. */
  221. protected function _getMailer() {
  222. $success= false;
  223. if (!$this->mailer || !($this->mailer instanceof PHPMailer)) {
  224. if ($this->mailer= new PHPMailer()) {
  225. // Make sure PHPMailer autoloader is loaded
  226. if (version_compare(PHP_VERSION, '5.1.2', '>=')) {
  227. $autoload = spl_autoload_functions();
  228. if ($autoload === false or !in_array('PHPMailerAutoload', $autoload)) {
  229. require 'phpmailer/PHPMailerAutoload.php';
  230. }
  231. }
  232. if (!empty($this->attributes)) {
  233. foreach ($this->attributes as $attrKey => $attrVal) {
  234. $this->set($attrKey, $attrVal);
  235. }
  236. }
  237. if (!isset($this->attributes[modMail::MAIL_LANGUAGE])) {
  238. $this->set(modMail::MAIL_LANGUAGE, $this->modx->config['manager_language']);
  239. }
  240. $success= true;
  241. }
  242. }
  243. return $success;
  244. }
  245. /**
  246. * Attaches a file to the mailer.
  247. *
  248. * @param mixed $file The file to attach
  249. * @param string $name The name of the file to attach as
  250. * @param string $encoding The encoding of the attachment
  251. * @param string $type The header type of the attachment
  252. */
  253. public function attach($file,$name = '',$encoding = 'base64',$type = 'application/octet-stream') {
  254. parent :: attach($file);
  255. $this->mailer->addAttachment($file,$name,$encoding,$type);
  256. }
  257. /**
  258. * Embeds image inside message body.
  259. *
  260. * @param mixed $image Absolute path to image
  261. * @param string $cid Id of the image by wich it will be available in html.
  262. * Example: <img src="cid:<$cid>" />
  263. * @param string $name The name of the image to attach as
  264. * @param string $encoding The encoding of the attachment
  265. * @param string $type The header type of the attachment
  266. */
  267. public function embedImage($image, $cid, $name = '', $encoding = 'base64', $type = 'application/octet-stream') {
  268. parent :: embedImage($image,$cid);
  269. $this->mailer->addEmbeddedImage($image,$cid,$name,$encoding,$type);
  270. }
  271. /**
  272. * Clears all existing attachments.
  273. */
  274. public function clearAttachments() {
  275. parent :: clearAttachments();
  276. $this->mailer->clearAttachments();
  277. }
  278. /**
  279. * Sets email to HTML or text-only.
  280. *
  281. * @access public
  282. * @param boolean $toggle True to set to HTML.
  283. */
  284. public function setHTML($toggle) {
  285. $this->mailer->isHTML($toggle);
  286. }
  287. }