SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/Email.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Helpers;
  15.  
  16. use Kohana;
  17. use PHPMailer\PHPMailer\PHPMailer;
  18.  
  19. /**
  20.  * Sends emails using HTML content generated by a View, wrapped in a standard template
  21.  *
  22.  * @example
  23.  * $view = new View('modules/MyModule/welcome_email');
  24.  * $view->set(...);
  25.  *
  26.  * $mail = new Email();
  27.  * $mail->Subject = 'Talking about the things and stuff';
  28.  * $mail->addAddress('example@example.org', 'Elaine Exemplary');
  29.  * $mail->SkinnedHTML($view);
  30.  * $mail->send();
  31.  */
  32. class Email extends PHPMailer
  33. {
  34.  
  35. public function __construct()
  36. {
  37. parent::__construct();
  38.  
  39. $this->From = Kohana::config('sprout.site_email');
  40. $this->FromName = Kohana::config('sprout.site_title');
  41.  
  42. $this->IsSMTP();
  43. $this->Host = Kohana::config('sprout.smtp_server');
  44. $this->Port = Kohana::config('sprout.smtp_port');
  45. $this->SMTPSecure = (Kohana::config('sprout.smtp_secure') ? 'tls' : '');
  46. $this->SMTPAutoTLS = Kohana::config('sprout.smtp_autotls');
  47. $this->SMTPAuth = Kohana::config('sprout.smtp_auth');
  48. $this->Username = Kohana::config('sprout.smtp_username');
  49. $this->Password = Kohana::config('sprout.smtp_password');
  50.  
  51. $this->AllowedDomains = Kohana::config('sprout.email_allowed_domains');
  52.  
  53. // Bypass failed cert verification to use TLS
  54. if (!IN_PRODUCTION) {
  55. $this->SMTPOptions['ssl']['verify_peer'] = false;
  56. $this->SMTPOptions['ssl']['verify_peer_name'] = false;
  57. }
  58.  
  59. // Other SMTP options:
  60. // $this->SMTPSecure = '', 'ssl', 'tls'
  61. // $this->Port = ?
  62. // $this->SMTPDebug = true | false
  63. // $this->Timeout = ?
  64.  
  65. // Other transports
  66. // $this->IsMail()
  67. // $this->IsSendmail()
  68. // $this->IsQmail()
  69. }
  70.  
  71.  
  72. /**
  73.   * Sets the message content to be the specified content, with the email skin around it
  74.   * Content can either be a HTML string or a View.
  75.   *
  76.   * @param $content string|View The content for the email.
  77.   * In the outer view becomes the parameter $content.
  78.   * @param $extraparams array Optional extra parameters to set for the outer view.
  79.   * @param $template string The template to use for the outer view. The default is "skin/email".
  80.   **/
  81. public function SkinnedHTML($content, $extraparams = null, $template = 'skin/email')
  82. {
  83. $view = new View($template);
  84. $view->content = $content;
  85.  
  86. if (!empty($this->Subject)) {
  87. $view->html_title = $this->Subject;
  88. } else {
  89. $view->html_title = Kohana::config('sprout.site_title');
  90. }
  91.  
  92. if (is_array($extraparams)) {
  93. foreach ($extraparams as $k => $v) {
  94. $view->set($k, $v);
  95. }
  96. }
  97.  
  98. $html = $view->render();
  99.  
  100. $html = mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8');
  101.  
  102. $this->MsgHTML($html, DOCROOT);
  103. }
  104.  
  105. }
  106.  
  107.  
  108.