PageRenderTime 29ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/web/concrete/core/helpers/mail.php

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