Files
FreshRSS/lib/Minz/Mailer.php
Alexandre Alapetite f3760f138d Complete PHPStan Level 6 (#5305)
* Complete PHPStan Level 6
Fix https://github.com/FreshRSS/FreshRSS/issues/4112
And initiate PHPStan Level 7

* PHPStan Level 6 for tests
* Use phpstan/phpstan-phpunit
* Update to PHPStan version 1.10

* Fix mixed bug

* Fix mixed return bug

* Fix paginator bug

* Fix FreshRSS_UserConfiguration

* A couple more Minz_Configuration bug fixes

* A few trivial PHPStan Level 7 fixes

* A few more simple PHPStan Level 7

* More files passing PHPStan Level 7
Add interface to replace removed class from https://github.com/FreshRSS/FreshRSS/pull/5251

* A few more PHPStan Level 7 preparations

* A few last details
2023-04-17 08:30:21 +02:00

116 lines
2.9 KiB
PHP

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
/**
* 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.php')
* ```
*
* 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;
/** @var string */
private $mailer;
/** @var array<string|int|bool> */
private $smtp_config;
/** @var int */
private $debug_level;
/**
* Constructor.
*/
public function __construct () {
$this->view = new Minz_View();
$this->view->_layout(null);
$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(string $to, string $subject): bool {
ob_start();
$this->view->render();
$body = ob_get_contents();
ob_end_clean();
PHPMailer::$validator = 'html5';
$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;
}
}
}