PageRenderTime 57ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/webroot/updates/concrete5.6.0.2/concrete/core/helpers/mail.php

https://bitbucket.org/microwebedu/registratie_carem
PHP | 404 lines | 248 code | 44 blank | 112 comment | 48 complexity | 486ada0fe227df19804ad5e2e9287331 MD5 | raw file
Possible License(s): MIT, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * @package Helpers
  4. * @category Concrete
  5. * @author Andrew Embler <andrew@concrete5.org>
  6. * @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
  7. * @license http://www.concrete5.org/license/ MIT License
  8. */
  9. /**
  10. * Functions used to send mail in Concrete.
  11. * @package Helpers
  12. * @category Concrete
  13. * @author Andrew Embler <andrew@concrete5.org>
  14. * @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
  15. * @license http://www.concrete5.org/license/ MIT License
  16. */
  17. defined('C5_EXECUTE') or die("Access Denied.");
  18. class Concrete5_Helper_Mail {
  19. protected $headers = array();
  20. protected $to = array();
  21. protected $cc = array();
  22. protected $bcc = array();
  23. protected $from = array();
  24. protected $data = array();
  25. protected $subject = '';
  26. public $body = '';
  27. protected $template;
  28. protected $bodyHTML = false;
  29. /**
  30. * this method is called by the Loader::helper to clean up the instance of this object
  31. * resets the class scope variables
  32. * @return void
  33. */
  34. public function reset() {
  35. $this->body = '';
  36. $this->headers = array();
  37. $this->to = array();
  38. $this->cc = array();
  39. $this->bcc = array();
  40. $this->from = array();
  41. $this->data = array();
  42. $this->subject = '';
  43. $this->body = '';
  44. $this->template;
  45. $this->bodyHTML = false;
  46. }
  47. /**
  48. * @todo documentation
  49. * @return array <Zend_Mail_Transport_Smtp, Zend_Mail>
  50. */
  51. public static function getMailerObject(){
  52. Loader::library('3rdparty/Zend/Mail');
  53. $response = array();
  54. $response['mail'] = new Zend_Mail(APP_CHARSET);
  55. if (MAIL_SEND_METHOD == "SMTP") {
  56. Loader::library('3rdparty/Zend/Mail/Transport/Smtp');
  57. $config = array();
  58. $username = Config::get('MAIL_SEND_METHOD_SMTP_USERNAME');
  59. $password = Config::get('MAIL_SEND_METHOD_SMTP_PASSWORD');
  60. if ($username != '') {
  61. $config['auth'] = 'login';
  62. $config['username'] = $username;
  63. $config['password'] = $password;
  64. }
  65. $port = Config::get('MAIL_SEND_METHOD_SMTP_PORT');
  66. if ($port != '') {
  67. $config['port'] = $port;
  68. }
  69. $encr = Config::get('MAIL_SEND_METHOD_SMTP_ENCRYPTION');
  70. if ($encr != '') {
  71. $config['ssl'] = $encr;
  72. }
  73. $transport = new Zend_Mail_Transport_Smtp(
  74. Config::get('MAIL_SEND_METHOD_SMTP_SERVER'), $config
  75. );
  76. $response['transport'] = $transport;
  77. }
  78. return $response;
  79. }
  80. /**
  81. * Adds a parameter to a mail template
  82. * @param string $key
  83. * @param string $val
  84. * @return void
  85. */
  86. public function addParameter($key, $val) {
  87. $this->data[$key] = $val;
  88. }
  89. /**
  90. * Loads an email template from the /mail/ directory
  91. * @param string $template
  92. * @param string $pkgHandle
  93. * @return void
  94. */
  95. public function load($template, $pkgHandle = null) {
  96. extract($this->data);
  97. // loads template from mail templates directory
  98. if (file_exists(DIR_FILES_EMAIL_TEMPLATES . "/{$template}.php")) {
  99. include(DIR_FILES_EMAIL_TEMPLATES . "/{$template}.php");
  100. } else if ($pkgHandle != null) {
  101. if (is_dir(DIR_PACKAGES . '/' . $pkgHandle)) {
  102. include(DIR_PACKAGES . '/' . $pkgHandle . '/' . DIRNAME_MAIL_TEMPLATES . "/{$template}.php");
  103. } else {
  104. include(DIR_PACKAGES_CORE . '/' . $pkgHandle . '/' . DIRNAME_MAIL_TEMPLATES . "/{$template}.php");
  105. }
  106. } else {
  107. include(DIR_FILES_EMAIL_TEMPLATES_CORE . "/{$template}.php");
  108. }
  109. if (isset($from)) {
  110. $this->from($from[0], $from[1]);
  111. }
  112. $this->template = $template;
  113. $this->subject = $subject;
  114. $this->body = $body;
  115. $this->bodyHTML = $bodyHTML;
  116. }
  117. /**
  118. * Manually set the text body of a mail message, typically the body is set in the template + load method
  119. * @param string $body
  120. * @return void
  121. */
  122. public function setBody($body){
  123. $this->body = $body;
  124. }
  125. /**
  126. * Manually set the message's subject
  127. * @param string $subject
  128. * @return void
  129. */
  130. public function setSubject($subject){
  131. $this->subject = $subject;
  132. }
  133. /**
  134. * Returns the message's subject
  135. * @return string
  136. */
  137. public function getSubject() {return $this->subject;}
  138. /**
  139. * Returns the message's text body
  140. * @return string
  141. */
  142. public function getBody() {return $this->body;}
  143. /**
  144. * manually set the HTML portion of a MIME encoded message, can also be done by setting $bodyHTML in a mail template
  145. * @param string $html
  146. * @return void
  147. */
  148. public function setBodyHTML($html) {
  149. $this->bodyHTML = $html;
  150. }
  151. /**
  152. * @param MailImporter $importer
  153. * @param array $data
  154. * @return void
  155. */
  156. public function enableMailResponseProcessing($importer, $data) {
  157. foreach($this->to as $em) {
  158. $importer->setupValidation($em[0], $data);
  159. }
  160. $this->from($importer->getMailImporterEmail());
  161. $this->body = $importer->setupBody($this->body);
  162. }
  163. /**
  164. * @param array $arr
  165. * @return string
  166. * @todo documentation
  167. */
  168. protected function generateEmailStrings($arr) {
  169. $str = '';
  170. for ($i = 0; $i < count($arr); $i++) {
  171. $v = $arr[$i];
  172. if (isset($v[1])) {
  173. $str .= '"' . $v[1] . '" <' . $v[0] . '>';
  174. } else {
  175. $str .= $v[0];
  176. }
  177. if (($i + 1) < count($arr)) {
  178. $str .= ', ';
  179. }
  180. }
  181. return $str;
  182. }
  183. /**
  184. * Sets the from address on the email about to be sent out
  185. * @param string $email
  186. * @param string $name
  187. * @return void
  188. */
  189. public function from($email, $name = null) {
  190. $this->from = array($email, $name);
  191. }
  192. /**
  193. * Sets to the to email address on the email about to be sent out.
  194. * @param string $email
  195. * @param string $name
  196. * @return void
  197. */
  198. public function to($email, $name = null) {
  199. if (strpos($email, ',') > 0) {
  200. $email = explode(',', $email);
  201. foreach($email as $em) {
  202. $this->to[] = array($em, $name);
  203. }
  204. } else {
  205. $this->to[] = array($email, $name);
  206. }
  207. }
  208. /**
  209. * Adds an email address to the cc field on the email about to be sent out.
  210. * @param string $email
  211. * @param string $name
  212. * @return void
  213. * @since 5.5.1
  214. */
  215. public function cc($email, $name = null) {
  216. if (strpos($email, ',') > 0) {
  217. $email = explode(',', $email);
  218. foreach($email as $em) {
  219. $this->cc[] = array($em, $name);
  220. }
  221. } else {
  222. $this->cc[] = array($email, $name);
  223. }
  224. }
  225. /**
  226. * Adds an email address to the bcc field on the email about to be sent out.
  227. * @param string $email
  228. * @param string $name
  229. * @return void
  230. * @since 5.5.1
  231. */
  232. public function bcc($email, $name = null) {
  233. if (strpos($email, ',') > 0) {
  234. $email = explode(',', $email);
  235. foreach($email as $em) {
  236. $this->bcc[] = array($em, $name);
  237. }
  238. } else {
  239. $this->bcc[] = array($email, $name);
  240. }
  241. }
  242. /*
  243. * Sets the reply-to address on the email about to be sent out
  244. * @param string $email
  245. * @param string $name
  246. * @return void
  247. */
  248. public function replyto($email, $name = null) {
  249. if (strpos($email, ',') > 0) {
  250. $email = explode(',', $email);
  251. foreach($email as $em) {
  252. $this->replyto[] = array($em, $name);
  253. }
  254. } else {
  255. $this->replyto[] = array($email, $name);
  256. }
  257. }
  258. /**
  259. * Sends the email
  260. * @return void
  261. */
  262. public function sendMail($resetData = true) {
  263. $_from[] = $this->from;
  264. $fromStr = $this->generateEmailStrings($_from);
  265. $toStr = $this->generateEmailStrings($this->to);
  266. $replyStr = $this->generateEmailStrings($this->replyto);
  267. if (ENABLE_EMAILS) {
  268. $zendMailData = self::getMailerObject();
  269. $mail=$zendMailData['mail'];
  270. $transport=(isset($zendMailData['transport']))?$zendMailData['transport']:NULL;
  271. if (is_array($this->from) && count($this->from)) {
  272. if ($this->from[0] != '') {
  273. $from = $this->from;
  274. }
  275. }
  276. if (!isset($from)) {
  277. $from = array(EMAIL_DEFAULT_FROM_ADDRESS, EMAIL_DEFAULT_FROM_NAME);
  278. $fromStr = EMAIL_DEFAULT_FROM_ADDRESS;
  279. }
  280. // The currently included Zend library has a bug in setReplyTo that
  281. // adds the Reply-To address as a recipient of the email. We must
  282. // set the Reply-To before any header with addresses and then clear
  283. // all recipients so that a copy is not sent to the Reply-To address.
  284. if(is_array($this->replyto)) {
  285. foreach ($this->replyto as $reply) {
  286. $mail->setReplyTo($reply[0], $reply[1]);
  287. }
  288. }
  289. $mail->clearRecipients();
  290. $mail->setFrom($from[0], $from[1]);
  291. $mail->setSubject($this->subject);
  292. foreach($this->to as $to) {
  293. $mail->addTo($to[0], $to[1]);
  294. }
  295. if(is_array($this->cc) && count($this->cc)) {
  296. foreach($this->cc as $cc) {
  297. $mail->addCc($cc[0], $cc[1]);
  298. }
  299. }
  300. if(is_array($this->bcc) && count($this->bcc)) {
  301. foreach($this->bcc as $bcc) {
  302. $mail->addBcc($bcc[0], $bcc[1]);
  303. }
  304. }
  305. $mail->setBodyText($this->body);
  306. if ($this->bodyHTML != false) {
  307. $mail->setBodyHTML($this->bodyHTML);
  308. }
  309. try {
  310. $mail->send($transport);
  311. } catch(Exception $e) {
  312. $l = new Log(LOG_TYPE_EXCEPTIONS, true, true);
  313. $l->write(t('Mail Exception Occurred. Unable to send mail: ') . $e->getMessage());
  314. $l->write($e->getTraceAsString());
  315. if (ENABLE_LOG_EMAILS) {
  316. $l->write(t('Template Used') . ': ' . $this->template);
  317. $l->write(t('To') . ': ' . $toStr);
  318. $l->write(t('From') . ': ' . $fromStr);
  319. if (isset($this->replyto)) {
  320. $l->write(t('Reply-To') . ': ' . $replyStr);
  321. }
  322. $l->write(t('Subject') . ': ' . $this->subject);
  323. $l->write(t('Body') . ': ' . $this->body);
  324. }
  325. $l->close();
  326. }
  327. }
  328. // add email to log
  329. if (ENABLE_LOG_EMAILS) {
  330. $l = new Log(LOG_TYPE_EMAILS, true, true);
  331. if (ENABLE_EMAILS) {
  332. $l->write('**' . t('EMAILS ARE ENABLED. THIS EMAIL WAS SENT TO mail()') . '**');
  333. } else {
  334. $l->write('**' . t('EMAILS ARE DISABLED. THIS EMAIL WAS LOGGED BUT NOT SENT') . '**');
  335. }
  336. $l->write(t('Template Used') . ': ' . $this->template);
  337. $l->write(t('To') . ': ' . $toStr);
  338. $l->write(t('From') . ': ' . $fromStr);
  339. if (isset($this->replyto)) {
  340. $l->write(t('Reply-To') . ': ' . $replyStr);
  341. }
  342. $l->write(t('Subject') . ': ' . $this->subject);
  343. $l->write(t('Body') . ': ' . $this->body);
  344. $l->close();
  345. }
  346. // clear data if applicable
  347. if ($resetData) {
  348. $this->to = array();
  349. $this->cc = array();
  350. $this->bcc = array();
  351. $this->replyto = array();
  352. $this->from = array();
  353. $this->template = '';
  354. $this->subject = '';
  355. $this->body = '';
  356. $this->bodyHTML = '';
  357. }
  358. }
  359. }
  360. ?>