raspisms/controllers/internals/Mailer.php

163 lines
4.6 KiB
PHP
Raw Normal View History

2020-04-02 18:38:41 +02:00
<?php
2020-06-23 21:06:13 +02:00
/*
* This file is part of RaspiSMS.
*
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
*
* This source file is subject to the GPL-3.0 license that is bundled
* with this source code in the file LICENSE.
*/
2020-04-02 18:38:41 +02:00
namespace controllers\internals;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
/**
2020-06-23 21:06:13 +02:00
* Mailing class.
2020-04-02 18:38:41 +02:00
*/
class Mailer extends \descartes\Controller
{
private $log;
private $mail;
2020-06-23 21:06:13 +02:00
public function __construct()
2020-04-02 18:38:41 +02:00
{
$this->log = new Logger('Mailer');
$this->log->pushHandler(new StreamHandler(PWD_LOGS . '/mail.log', Logger::DEBUG));
$this->mail = new PHPMailer(true);
2020-06-23 21:06:13 +02:00
$this->mail->CharSet = 'utf-8';
$this->mail->SMTPDebug = SMTP::DEBUG_OFF;
2020-04-16 07:50:30 +02:00
$this->mail->isSMTP();
2020-06-23 21:06:13 +02:00
$this->mail->Host = MAIL['SMTP']['HOST'];
$this->mail->SMTPAuth = true;
$this->mail->Username = MAIL['SMTP']['USER'];
$this->mail->Password = MAIL['SMTP']['PASS'];
$this->mail->Port = MAIL['SMTP']['PORT'];
2020-04-16 07:50:30 +02:00
$this->mail->setFrom(MAIL['FROM']);
2020-04-02 18:38:41 +02:00
if (MAIL['SMTP']['TLS'])
{
2020-04-16 07:50:30 +02:00
$this->mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
2020-04-02 18:38:41 +02:00
}
}
/**
2020-06-23 21:06:13 +02:00
* Send email.
*
* @param array $destinations : Destinations address
* @param string $subject : Message subject
* @param string $message : Message
* @param ?string $alt_message : Alt Message if no html support. Null if message is not html.
* @param array $attachments : List of path to attachment files
*
2020-04-02 18:38:41 +02:00
* @return bool : false on error, true else
*/
2020-06-23 21:06:13 +02:00
public function send(array $destinations, string $subject, string $message, ?string $alt_message = null, array $attachments = [])
2020-04-02 18:38:41 +02:00
{
try
{
$mail = clone $this->mail;
foreach ($destinations as $destination)
{
//Only use bcc to avoid leak
$mail->addBCC($destination);
}
foreach ($attachments as $attachment)
{
$mail->addAttachment($attachment);
}
$mail->Subject = $subject;
$mail->Body = $message;
2020-06-23 21:06:13 +02:00
2020-04-02 18:38:41 +02:00
if ($alt_message)
{
$mail->isHTML($html);
$mail->AltBody = $alt_message;
}
$mail->send();
2020-06-23 21:06:13 +02:00
2020-04-02 18:38:41 +02:00
return true;
}
catch (\Throwable $t)
{
$this->log->error('Error sending mail : ' . $t);
2020-06-23 21:06:13 +02:00
2020-04-02 18:38:41 +02:00
return false;
}
}
/**
2020-06-23 21:06:13 +02:00
* Enqueue an email for later sending.
2020-04-02 18:38:41 +02:00
*
2020-06-23 21:06:13 +02:00
* @param string $destination : email address to send email to
* @param array $settings : Email settings
* @param array $datas : Datas to inject into email template
*
* @return bool : true on success, false on error
*/
public function enqueue(string $destination, array $settings, array $datas): bool
{
$response = $this->generate_body($settings, $datas);
$message = [
'destinations' => [$destination],
'subject' => $settings['subject'],
'body' => $response['body'],
'alt_body' => $response['alt_body'],
];
$error_code = null;
$queue = msg_get_queue(QUEUE_ID_EMAIL);
$success = msg_send($queue, QUEUE_TYPE_EMAIL, $message, true, true, $error_code);
return (bool) $success;
}
/**
* Generate an email body.
*
* @param array $settings : [
* string 'type' => Internal RaspiSMS email type,
* string 'subject' => Email subject,
* string 'template' => Email template to use
* ?string 'alt_template' => Template to use for alt message, if null ignore
* ]
2020-04-02 18:38:41 +02:00
* @param array : Datas to inject into email template
*
* @return array [
2020-06-23 21:06:13 +02:00
* string 'body' => email body
* ?string 'alt_body' => email alternative body if needed
* ]
2020-04-02 18:38:41 +02:00
*/
2020-06-23 21:06:13 +02:00
private function generate_body(array $settings, array $datas): array
2020-04-02 18:38:41 +02:00
{
//Generate body of email
ob_start();
$this->render($settings['template'], $datas);
$body = ob_get_clean();
//Generate alt body if needed
$alt_body = null;
if ($settings['alt_template'] ?? false)
{
ob_start();
$this->render($settings['alt_template'], $datas);
$alt_body = ob_get_clean();
}
2020-06-23 21:06:13 +02:00
2020-04-02 18:38:41 +02:00
return [
2020-06-23 21:06:13 +02:00
'body' => $body,
2020-04-02 18:38:41 +02:00
'alt_body' => $alt_body,
];
}
}