Skip to content

Commit

Permalink
tec: Provide a Minz_Mailer class (FreshRSS#2476)
Browse files Browse the repository at this point in the history
* Add Minz_View::_path method (replace change_view)

The `_path` method is more powerful since it allows to choose the file
extension. It is also Minz_Request-agnostic, which is useful to reuse
the Minz_View class in other places.

`change_view` is now deprecated and a warning is logged if we use it.

* Provide a Minz_Mailer to send emails

It uses PHPMailer under the hood and only supports PHP >= 5.5
  • Loading branch information
marienfressinaud authored and Alkarex committed Aug 15, 2019
1 parent a411af4 commit 034967e
Show file tree
Hide file tree
Showing 12 changed files with 6,023 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.fr.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ Tout client supportant une API de type Fever ; Sélection :
* [jQuery](https://jquery.com/)
* [lib_opml](https://github.com/marienfressinaud/lib_opml)
* [flotr2](http://www.humblesoftware.com/flotr2)
* [PHPMailer](https://github.com/PHPMailer/PHPMailer)

## Uniquement pour certaines options ou configurations
* [bcrypt.js](https://github.com/dcodeIO/bcrypt.js)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ Supported clients are:
* [jQuery](https://jquery.com/)
* [lib_opml](https://github.com/marienfressinaud/lib_opml)
* [flotr2](http://www.humblesoftware.com/flotr2)
* [PHPMailer](https://github.com/PHPMailer/PHPMailer)

## Only for some options or configurations
* [bcrypt.js](https://github.com/dcodeIO/bcrypt.js)
Expand Down
2 changes: 1 addition & 1 deletion app/Controllers/extensionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function configureAction() {
$this->view->_layout(false);
} else {
$this->indexAction();
$this->view->change_view('extension', 'index');
$this->view->_path('extension/index.phtml');
}

$ext_name = urldecode(Minz_Request::param('e'));
Expand Down
2 changes: 1 addition & 1 deletion app/Controllers/updateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function indexAction() {
}

public function checkAction() {
$this->view->change_view('update', 'index');
$this->view->_path('update/index.phtml');

if (file_exists(UPDATE_FILENAME)) {
// There is already an update file to apply: we don't need to check
Expand Down
18 changes: 18 additions & 0 deletions config.default.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,24 @@

),

# Configuration to send emails. Be aware that PHP < 5.5 are not supported.
# These options are basically a mapping of the PHPMailer class attributes
# from the PHPMailer library.
#
# See http://phpmailer.github.io/PHPMailer/classes/PHPMailer.PHPMailer.PHPMailer.html#properties
'mailer' => 'mail', // 'mail' or 'smtp'
'smtp' => array(
'hostname' => '', // the domain used in the Message-ID header
'host' => 'localhost', // the SMTP server address
'port' => 25,
'auth' => false,
'auth_type' => '', // 'CRAM-MD5', 'LOGIN', 'PLAIN', 'XOAUTH2' or ''
'username' => '',
'password' => '',
'secure' => '', // '', 'ssl' or 'tls'
'from' => 'noreply@localhost',
),

# List of enabled FreshRSS extensions.
'extensions_enabled' => array(
'Tumblr-GDPR' => true,
Expand Down
4 changes: 3 additions & 1 deletion lib/Minz/ActionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ class Minz_ActionController {
* Constructeur
*/
public function __construct () {
$this->view = new Minz_View ();
$this->view = new Minz_View();
$view_path = Minz_Request::controllerName() . '/' . Minz_Request::actionName() . '.phtml';
$this->view->_path($view_path);
$this->view->attributeParams ();
}

Expand Down
117 changes: 117 additions & 0 deletions lib/Minz/Mailer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require LIB_PATH . '/PHPMailer/PHPMailer.php';
require LIB_PATH . '/PHPMailer/Exception.php';
require LIB_PATH . '/PHPMailer/SMTP.php';

/**
* Allow to send emails.
*
* The Minz_Mailer class must be inherited by classes under app/Mailers.
* They work similarly to the ActionControllers in the way they have a view to
* which you can pass params (eg. $this->view->foo = 'bar').
*
* The view file is not determined automatically, so you have to select one
* with, for instance:
*
* ```
* $this->view->_path('user_mailer/email_need_validation.txt')
* ```
*
* Minz_Mailer uses the PHPMailer library under the hood. The latter requires
* PHP >= 5.5 to work. If you instantiate a Minz_Mailer with PHP < 5.5, a
* warning will be logged.
*
* The email is sent by calling the `mail` method.
*/
class Minz_Mailer {
/**
* The view attached to the mailer.
* You should set its file with `$this->view->_path($path)`
*
* @var Minz_View
*/
protected $view;

/**
* Constructor.
*
* If PHP version is < 5.5, a warning is logged.
*/
public function __construct () {
if (version_compare(PHP_VERSION, '5.5') < 0) {
Minz_Log::warning('Minz_Mailer cannot be used with a version of PHP < 5.5.');
}

$this->view = new Minz_View();
$this->view->_layout(false);
$this->view->attributeParams();

$conf = Minz_Configuration::get('system');
$this->mailer = $conf->mailer;
$this->smtp_config = $conf->smtp;

// According to https://github.com/PHPMailer/PHPMailer/wiki/SMTP-Debugging#debug-levels
// we should not use debug level above 2 unless if we have big trouble
// to connect.
if ($conf->environment === 'development') {
$this->debug_level = 2;
} else {
$this->debug_level = 0;
}
}

/**
* Send an email.
*
* @param string $to The recipient of the email
* @param string $subject The subject of the email
*
* @return bool true on success, false if a SMTP error happens
*/
public function mail($to, $subject) {
ob_start();
$this->view->render();
$body = ob_get_contents();
ob_end_clean();

$mail = new PHPMailer(true);
try {
// Server settings
$mail->SMTPDebug = $this->debug_level;
$mail->Debugoutput = 'error_log';

if ($this->mailer === 'smtp') {
$mail->isSMTP();
$mail->Hostname = $this->smtp_config['hostname'];
$mail->Host = $this->smtp_config['host'];
$mail->SMTPAuth = $this->smtp_config['auth'];
$mail->Username = $this->smtp_config['username'];
$mail->Password = $this->smtp_config['password'];
$mail->SMTPSecure = $this->smtp_config['secure'];
$mail->Port = $this->smtp_config['port'];
} else {
$mail->isMail();
}

// Recipients
$mail->setFrom($this->smtp_config['from']);
$mail->addAddress($to);

// Content
$mail->isHTML(false);
$mail->CharSet = 'utf-8';
$mail->Subject = $subject;
$mail->Body = $body;

$mail->send();
return true;
} catch (Exception $e) {
Minz_Log::error('Minz_Mailer cannot send a message: ' . $mail->ErrorInfo);
return false;
}
}
}
21 changes: 13 additions & 8 deletions lib/Minz/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,27 @@ class Minz_View {
* Constructeur
* Détermine si on utilise un layout ou non
*/
public function __construct () {
$this->change_view(Minz_Request::controllerName(),
Minz_Request::actionName());

public function __construct() {
$this->_layout(self::LAYOUT_DEFAULT);
$conf = Minz_Configuration::get('system');
self::$title = $conf->title;
}

/**
* Change le fichier de vue en fonction d'un controller / action
* [deprecated] Change the view file based on controller and action.
*/
public function change_view($controller_name, $action_name) {
$this->view_filename = self::VIEWS_PATH_NAME . '/'
. $controller_name . '/'
. $action_name . '.phtml';
Minz_Log::warning('Minz_View::change_view is deprecated, it will be removed in a future version. Please use Minz_View::_path instead.');
$this->_path($controller_name. '/' . $action_name . '.phtml');
}

/**
* Change the view file based on a pathname relative to VIEWS_PATH_NAME.
*
* @param string $path the new path
*/
public function _path($path) {
$this->view_filename = self::VIEWS_PATH_NAME . '/' . $path;
}

/**
Expand Down
39 changes: 39 additions & 0 deletions lib/PHPMailer/Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* PHPMailer Exception class.
* PHP Version 5.5.
*
* @see https://github.com/PHPMailer/PHPMailer/ The PHPMailer GitHub project
*
* @author Marcus Bointon (Synchro/coolbru) <[email protected]>
* @author Jim Jagielski (jimjag) <[email protected]>
* @author Andy Prevost (codeworxtech) <[email protected]>
* @author Brent R. Matzelle (original founder)
* @copyright 2012 - 2017 Marcus Bointon
* @copyright 2010 - 2012 Jim Jagielski
* @copyright 2004 - 2009 Andy Prevost
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
* @note This program is distributed in the hope that it will be useful - WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*/

namespace PHPMailer\PHPMailer;

/**
* PHPMailer exception handler.
*
* @author Marcus Bointon <[email protected]>
*/
class Exception extends \Exception
{
/**
* Prettify error message output.
*
* @return string
*/
public function errorMessage()
{
return '<strong>' . htmlspecialchars($this->getMessage()) . "</strong><br />\n";
}
}
Loading

0 comments on commit 034967e

Please sign in to comment.