2022-04-24 14:32:58 +02:00
|
|
|
<?php
|
|
|
|
namespace Grav\Plugin\Email;
|
|
|
|
|
|
|
|
use Grav\Common\Config\Config;
|
|
|
|
use Grav\Common\Grav;
|
2023-05-12 15:06:01 +02:00
|
|
|
use Grav\Common\Utils;
|
2022-04-24 14:32:58 +02:00
|
|
|
use Grav\Common\Language\Language;
|
|
|
|
use Grav\Common\Markdown\Parsedown;
|
|
|
|
use Grav\Common\Twig\Twig;
|
|
|
|
use Grav\Framework\Form\Interfaces\FormInterface;
|
|
|
|
use \Monolog\Logger;
|
|
|
|
use \Monolog\Handler\StreamHandler;
|
2023-05-12 15:06:01 +02:00
|
|
|
use RocketTheme\Toolbox\Event\Event;
|
|
|
|
use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
|
|
|
|
use Symfony\Component\Mailer\Envelope;
|
|
|
|
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
|
|
|
|
use Symfony\Component\Mailer\Header\MetadataHeader;
|
|
|
|
use Symfony\Component\Mailer\Header\TagHeader;
|
|
|
|
use Symfony\Component\Mailer\Mailer;
|
|
|
|
use Symfony\Component\Mailer\Transport;
|
|
|
|
use Symfony\Component\Mailer\Transport\TransportInterface;
|
|
|
|
use Symfony\Component\Mime\Address;
|
2022-04-24 14:32:58 +02:00
|
|
|
|
|
|
|
class Email
|
|
|
|
{
|
2023-05-12 15:06:01 +02:00
|
|
|
/** @var Mailer */
|
2022-04-24 14:32:58 +02:00
|
|
|
protected $mailer;
|
|
|
|
|
2023-05-12 15:06:01 +02:00
|
|
|
/** @var TransportInterface */
|
|
|
|
protected $transport;
|
2022-04-24 14:32:58 +02:00
|
|
|
|
2023-05-12 15:06:01 +02:00
|
|
|
protected $log;
|
|
|
|
|
2024-05-17 20:26:01 +02:00
|
|
|
protected $message;
|
|
|
|
protected $debug;
|
|
|
|
|
2023-05-12 15:06:01 +02:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->initMailer();
|
|
|
|
$this->initLog();
|
|
|
|
}
|
2022-04-24 14:32:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if emails have been enabled in the system.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2023-05-12 15:06:01 +02:00
|
|
|
public static function enabled(): bool
|
2022-04-24 14:32:58 +02:00
|
|
|
{
|
|
|
|
return Grav::instance()['config']->get('plugins.email.mailer.engine') !== 'none';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if debugging on emails has been enabled.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2023-05-12 15:06:01 +02:00
|
|
|
public static function debug(): bool
|
2022-04-24 14:32:58 +02:00
|
|
|
{
|
|
|
|
return Grav::instance()['config']->get('plugins.email.debug') == 'true';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an email message.
|
|
|
|
*
|
2023-05-12 15:06:01 +02:00
|
|
|
* @param string|null $subject
|
|
|
|
* @param string|null $body
|
|
|
|
* @param string|null $contentType
|
|
|
|
* @param string|null $charset @deprecated
|
|
|
|
* @return Message
|
2022-04-24 14:32:58 +02:00
|
|
|
*/
|
2023-05-12 15:06:01 +02:00
|
|
|
public function message(string $subject = null, string $body = null, string $contentType = null, string $charset = null): Message
|
2022-04-24 14:32:58 +02:00
|
|
|
{
|
2023-05-12 15:06:01 +02:00
|
|
|
$message = new Message();
|
|
|
|
$message->subject($subject);
|
|
|
|
if ($contentType === 'text/html') {
|
|
|
|
$message->html($body);
|
|
|
|
} else {
|
|
|
|
$message->text($body);
|
|
|
|
}
|
2022-04-24 14:32:58 +02:00
|
|
|
|
2023-05-12 15:06:01 +02:00
|
|
|
return $message;
|
2022-04-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send email.
|
|
|
|
*
|
2024-05-17 20:26:01 +02:00
|
|
|
* @param Message $message
|
|
|
|
* @param Envelope|null $envelope
|
2022-04-24 14:32:58 +02:00
|
|
|
* @return int
|
|
|
|
*/
|
2023-05-12 15:06:01 +02:00
|
|
|
public function send(Message $message, Envelope $envelope = null): int
|
2022-04-24 14:32:58 +02:00
|
|
|
{
|
2023-05-12 15:06:01 +02:00
|
|
|
try {
|
|
|
|
$sent_msg = $this->transport->send($message->getEmail(), $envelope);
|
2024-05-17 20:26:01 +02:00
|
|
|
$status = 1;
|
|
|
|
$this->message = '✅';
|
|
|
|
$this->debug = $sent_msg->getDebug();
|
2023-05-12 15:06:01 +02:00
|
|
|
} catch (TransportExceptionInterface $e) {
|
2024-05-17 20:26:01 +02:00
|
|
|
$status = 0;
|
|
|
|
$this->message = '🛑 ' . $e->getMessage();
|
|
|
|
$this->debug = $e->getDebug();
|
2023-05-12 15:06:01 +02:00
|
|
|
}
|
2022-04-24 14:32:58 +02:00
|
|
|
|
2023-05-12 15:06:01 +02:00
|
|
|
if ($this->debug()) {
|
|
|
|
$log_msg = "Email sent to %s at %s -> %s\n%s";
|
|
|
|
$to = $this->jsonifyRecipients($message->getEmail()->getTo());
|
2024-05-17 20:26:01 +02:00
|
|
|
$message = sprintf($log_msg, $to, date('Y-m-d H:i:s'), $this->message, $this->debug);
|
|
|
|
$this->log->addInfo($message);
|
2022-04-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
|
2024-05-17 20:26:01 +02:00
|
|
|
return $status;
|
2022-04-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build e-mail message.
|
|
|
|
*
|
|
|
|
* @param array $params
|
|
|
|
* @param array $vars
|
2023-05-12 15:06:01 +02:00
|
|
|
* @return Message
|
2022-04-24 14:32:58 +02:00
|
|
|
*/
|
2023-05-12 15:06:01 +02:00
|
|
|
public function buildMessage(array $params, array $vars = []): Message
|
2022-04-24 14:32:58 +02:00
|
|
|
{
|
|
|
|
/** @var Twig $twig */
|
|
|
|
$twig = Grav::instance()['twig'];
|
|
|
|
$twig->init();
|
|
|
|
|
|
|
|
/** @var Config $config */
|
|
|
|
$config = Grav::instance()['config'];
|
|
|
|
|
|
|
|
/** @var Language $language */
|
|
|
|
$language = Grav::instance()['language'];
|
|
|
|
|
|
|
|
// Create message object.
|
2023-05-12 15:06:01 +02:00
|
|
|
$message = new Message();
|
|
|
|
$headers = $message->getEmail()->getHeaders();
|
|
|
|
$email = $message->getEmail();
|
2022-04-24 14:32:58 +02:00
|
|
|
|
|
|
|
// Extend parameters with defaults.
|
2023-07-11 17:19:18 +02:00
|
|
|
$defaults = [
|
2022-04-24 14:32:58 +02:00
|
|
|
'bcc' => $config->get('plugins.email.bcc', []),
|
2023-07-11 17:19:18 +02:00
|
|
|
'bcc_name' => $config->get('plugins.email.bcc_name'),
|
2022-04-24 14:32:58 +02:00
|
|
|
'body' => $config->get('plugins.email.body', '{% include "forms/data.html.twig" %}'),
|
|
|
|
'cc' => $config->get('plugins.email.cc', []),
|
|
|
|
'cc_name' => $config->get('plugins.email.cc_name'),
|
|
|
|
'charset' => $config->get('plugins.email.charset', 'utf-8'),
|
|
|
|
'from' => $config->get('plugins.email.from'),
|
|
|
|
'from_name' => $config->get('plugins.email.from_name'),
|
|
|
|
'content_type' => $config->get('plugins.email.content_type', 'text/html'),
|
|
|
|
'reply_to' => $config->get('plugins.email.reply_to', []),
|
|
|
|
'reply_to_name' => $config->get('plugins.email.reply_to_name'),
|
|
|
|
'subject' => !empty($vars['form']) && $vars['form'] instanceof FormInterface ? $vars['form']->page()->title() : null,
|
|
|
|
'to' => $config->get('plugins.email.to'),
|
|
|
|
'to_name' => $config->get('plugins.email.to_name'),
|
|
|
|
'process_markdown' => false,
|
|
|
|
'template' => false,
|
|
|
|
'message' => $message
|
|
|
|
];
|
|
|
|
|
2023-07-11 17:19:18 +02:00
|
|
|
foreach ($defaults as $key => $value) {
|
|
|
|
if (!key_exists($key, $params)) {
|
|
|
|
$params[$key] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-24 14:32:58 +02:00
|
|
|
if (!$params['to']) {
|
|
|
|
throw new \RuntimeException($language->translate('PLUGIN_EMAIL.PLEASE_CONFIGURE_A_TO_ADDRESS'));
|
|
|
|
}
|
|
|
|
if (!$params['from']) {
|
|
|
|
throw new \RuntimeException($language->translate('PLUGIN_EMAIL.PLEASE_CONFIGURE_A_FROM_ADDRESS'));
|
|
|
|
}
|
|
|
|
|
2023-05-12 15:06:01 +02:00
|
|
|
|
2022-04-24 14:32:58 +02:00
|
|
|
// make email configuration available to templates
|
|
|
|
$vars += [
|
|
|
|
'email' => $params,
|
|
|
|
];
|
|
|
|
|
2023-05-12 15:06:01 +02:00
|
|
|
$params = $this->processParams($params, $vars);
|
|
|
|
|
2022-04-24 14:32:58 +02:00
|
|
|
// Process parameters.
|
|
|
|
foreach ($params as $key => $value) {
|
|
|
|
switch ($key) {
|
|
|
|
case 'body':
|
|
|
|
if (is_string($value)) {
|
2023-05-12 15:06:01 +02:00
|
|
|
$this->processBody($message, $params, $vars, $twig, $value);
|
2022-04-24 14:32:58 +02:00
|
|
|
} elseif (is_array($value)) {
|
|
|
|
foreach ($value as $body_part) {
|
2023-05-12 15:06:01 +02:00
|
|
|
$params_part = $params;
|
|
|
|
if (isset($body_part['content_type'])) {
|
|
|
|
$params_part['content_type'] = $body_part['content_type'];
|
2022-04-24 14:32:58 +02:00
|
|
|
}
|
2023-06-02 16:56:14 +02:00
|
|
|
if (isset($body_part['template'])) {
|
|
|
|
$params_part['template'] = $body_part['template'];
|
|
|
|
}
|
2023-05-12 15:06:01 +02:00
|
|
|
if (isset($body_part['body'])) {
|
|
|
|
$this->processBody($message, $params_part, $vars, $twig, $body_part['body']);
|
2022-04-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'subject':
|
|
|
|
if ($value) {
|
2023-05-12 15:06:01 +02:00
|
|
|
$message->subject($language->translate($value));
|
2022-04-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'to':
|
2023-05-12 15:06:01 +02:00
|
|
|
case 'from':
|
2022-04-24 14:32:58 +02:00
|
|
|
case 'cc':
|
|
|
|
case 'bcc':
|
2023-05-12 15:06:01 +02:00
|
|
|
case 'reply_to':
|
|
|
|
if ($recipients = $this->processRecipients($key, $params)) {
|
|
|
|
$key = $key === 'reply_to' ? 'replyTo' : $key;
|
|
|
|
$email->$key(...$recipients);
|
2022-04-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
break;
|
2023-05-12 15:06:01 +02:00
|
|
|
case 'tags':
|
|
|
|
foreach ((array) $value as $tag) {
|
|
|
|
if (is_string($tag)) {
|
|
|
|
$headers->add(new TagHeader($tag));
|
|
|
|
}
|
2022-04-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
break;
|
2023-05-12 15:06:01 +02:00
|
|
|
case 'metadata':
|
|
|
|
foreach ((array) $value as $k => $v) {
|
|
|
|
if (is_string($k) && is_string($v)) {
|
|
|
|
$headers->add(new MetadataHeader($k, $v));
|
|
|
|
}
|
2022-04-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $message;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-05-12 15:06:01 +02:00
|
|
|
* @param string $type
|
|
|
|
* @param array $params
|
2022-04-24 14:32:58 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
2023-05-12 15:06:01 +02:00
|
|
|
protected function processRecipients(string $type, array $params): array
|
2022-04-24 14:32:58 +02:00
|
|
|
{
|
2023-07-11 17:19:18 +02:00
|
|
|
if (array_key_exists($type, $params) && $params[$type] === null) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2023-05-12 15:06:01 +02:00
|
|
|
$recipients = $params[$type] ?? Grav::instance()['config']->get('plugins.email.'.$type) ?? [];
|
|
|
|
|
|
|
|
$list = [];
|
|
|
|
|
|
|
|
if (!empty($recipients)) {
|
2023-07-11 17:19:18 +02:00
|
|
|
if (is_array($recipients)) {
|
|
|
|
if (Utils::isAssoc($recipients) || (count($recipients) ===2 && $this->isValidEmail($recipients[0]) && !$this->isValidEmail($recipients[1]))) {
|
|
|
|
$list[] = $this->createAddress($recipients);
|
|
|
|
} else {
|
|
|
|
foreach ($recipients as $recipient) {
|
|
|
|
$list[] = $this->createAddress($recipient);
|
|
|
|
}
|
|
|
|
}
|
2023-05-12 15:06:01 +02:00
|
|
|
} else {
|
2023-07-11 17:19:18 +02:00
|
|
|
if (is_string($recipients) && Utils::contains($recipients, ',')) {
|
|
|
|
$recipients = array_map('trim', explode(',', $recipients));
|
|
|
|
foreach ($recipients as $recipient) {
|
|
|
|
$list[] = $this->createAddress($recipient);
|
2023-05-12 15:06:01 +02:00
|
|
|
}
|
|
|
|
} else {
|
2023-07-11 17:19:18 +02:00
|
|
|
if (!Utils::contains($recipients, ['<','>']) && (isset($params[$type."_name"]))) {
|
|
|
|
$recipients = [$recipients, $params[$type."_name"]];
|
2022-04-24 14:32:58 +02:00
|
|
|
}
|
2023-07-11 17:19:18 +02:00
|
|
|
$list[] = $this->createAddress($recipients);
|
2022-04-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-11 17:19:18 +02:00
|
|
|
|
2023-05-12 15:06:01 +02:00
|
|
|
return $list;
|
2022-04-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-05-12 15:06:01 +02:00
|
|
|
* @param $data
|
|
|
|
* @return Address
|
2022-04-24 14:32:58 +02:00
|
|
|
*/
|
2023-05-12 15:06:01 +02:00
|
|
|
protected function createAddress($data): Address
|
2022-04-24 14:32:58 +02:00
|
|
|
{
|
2023-05-12 15:06:01 +02:00
|
|
|
if (is_string($data)) {
|
|
|
|
preg_match('/^(.*)\<(.*)\>$/', $data, $matches);
|
|
|
|
if (isset($matches[2])) {
|
|
|
|
$email = trim($matches[2]);
|
|
|
|
$name = trim($matches[1]);
|
|
|
|
} else {
|
|
|
|
$email = $data;
|
|
|
|
$name = '';
|
|
|
|
}
|
|
|
|
} elseif (Utils::isAssoc($data)) {
|
|
|
|
$first_key = array_key_first($data);
|
|
|
|
if (filter_var($first_key, FILTER_VALIDATE_EMAIL)) {
|
|
|
|
$email = $first_key;
|
|
|
|
$name = $data[$first_key];
|
|
|
|
} else {
|
|
|
|
$email = $data['email'] ?? $data['mail'] ?? $data['address'] ?? '';
|
|
|
|
$name = $data['name'] ?? $data['fullname'] ?? '';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$email = $data[0] ?? '';
|
|
|
|
$name = $data[1] ?? '';
|
2022-04-24 14:32:58 +02:00
|
|
|
}
|
2023-05-12 15:06:01 +02:00
|
|
|
return new Address($email, $name);
|
2022-04-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-05-12 15:06:01 +02:00
|
|
|
* @return null|Mailer
|
2022-04-24 14:32:58 +02:00
|
|
|
* @internal
|
|
|
|
*/
|
2023-05-12 15:06:01 +02:00
|
|
|
protected function initMailer(): ?Mailer
|
2022-04-24 14:32:58 +02:00
|
|
|
{
|
|
|
|
if (!$this->enabled()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (!$this->mailer) {
|
2023-05-12 15:06:01 +02:00
|
|
|
$this->transport = $this->getTransport();
|
2022-04-24 14:32:58 +02:00
|
|
|
// Create the Mailer using your created Transport
|
2023-05-12 15:06:01 +02:00
|
|
|
$this->mailer = new Mailer($this->transport);
|
2022-04-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
return $this->mailer;
|
|
|
|
}
|
|
|
|
|
2023-05-12 15:06:01 +02:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
protected function initLog()
|
2022-04-24 14:32:58 +02:00
|
|
|
{
|
2023-05-12 15:06:01 +02:00
|
|
|
$log_file = Grav::instance()['locator']->findResource('log://email.log', true, true);
|
|
|
|
$this->log = new Logger('email');
|
|
|
|
/** @var UniformResourceLocator $locator */
|
|
|
|
$this->log->pushHandler(new StreamHandler($log_file, Logger::DEBUG));
|
2022-04-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
|
2023-05-12 15:06:01 +02:00
|
|
|
/**
|
|
|
|
* @param array $params
|
|
|
|
* @param array $vars
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function processParams(array $params, array $vars = []): array
|
2022-04-24 14:32:58 +02:00
|
|
|
{
|
2023-05-12 15:06:01 +02:00
|
|
|
$twig = Grav::instance()['twig'];
|
|
|
|
array_walk_recursive($params, function(&$value) use ($twig, $vars) {
|
|
|
|
if (is_string($value)) {
|
|
|
|
$value = $twig->processString($value, $vars);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return $params;
|
2022-04-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
|
2023-05-12 15:06:01 +02:00
|
|
|
/**
|
|
|
|
* @param $message
|
|
|
|
* @param $params
|
|
|
|
* @param $vars
|
|
|
|
* @param $twig
|
|
|
|
* @param $body
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function processBody($message, $params, $vars, $twig, $body)
|
2022-04-24 14:32:58 +02:00
|
|
|
{
|
2023-05-12 15:06:01 +02:00
|
|
|
if ($params['process_markdown'] && $params['content_type'] === 'text/html') {
|
|
|
|
$body = (new Parsedown())->text($body);
|
2022-04-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
|
2023-05-12 15:06:01 +02:00
|
|
|
if ($params['template']) {
|
|
|
|
$body = $twig->processTemplate($params['template'], ['content' => $body] + $vars);
|
2022-04-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
|
2023-05-12 15:06:01 +02:00
|
|
|
$content_type = !empty($params['content_type']) ? $twig->processString($params['content_type'], $vars) : null;
|
2022-04-24 14:32:58 +02:00
|
|
|
|
2023-05-12 15:06:01 +02:00
|
|
|
if ($content_type === 'text/html') {
|
|
|
|
$message->html($body);
|
|
|
|
} else {
|
|
|
|
$message->text($body);
|
2022-04-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-05-12 15:06:01 +02:00
|
|
|
* @return TransportInterface
|
2022-04-24 14:32:58 +02:00
|
|
|
*/
|
2023-05-12 15:06:01 +02:00
|
|
|
protected static function getTransport(): Transport\TransportInterface
|
2022-04-24 14:32:58 +02:00
|
|
|
{
|
|
|
|
/** @var Config $config */
|
|
|
|
$config = Grav::instance()['config'];
|
|
|
|
$engine = $config->get('plugins.email.mailer.engine');
|
2023-05-12 15:06:01 +02:00
|
|
|
$dsn = 'null://default';
|
|
|
|
|
2022-04-24 14:32:58 +02:00
|
|
|
|
|
|
|
// Create the Transport and initialize it.
|
|
|
|
switch ($engine) {
|
2023-05-12 15:06:01 +02:00
|
|
|
case 'smtps':
|
2022-04-24 14:32:58 +02:00
|
|
|
case 'smtp':
|
|
|
|
$options = $config->get('plugins.email.mailer.smtp');
|
2023-05-12 15:06:01 +02:00
|
|
|
$dsn = $engine . '://';
|
|
|
|
$auth = '';
|
|
|
|
|
|
|
|
if (isset($options['encryption']) && $options['encryption'] === 'none') {
|
|
|
|
$options['options']['verify_peer'] = 0;
|
2022-04-24 14:32:58 +02:00
|
|
|
}
|
2023-05-12 15:06:01 +02:00
|
|
|
if (isset($options['user'])) {
|
|
|
|
$auth .= urlencode($options['user']);
|
2022-04-24 14:32:58 +02:00
|
|
|
}
|
2023-05-12 15:06:01 +02:00
|
|
|
if (isset($options['password'])) {
|
|
|
|
$auth .= ':'. urlencode($options['password']);
|
2022-04-24 14:32:58 +02:00
|
|
|
}
|
2023-05-12 15:06:01 +02:00
|
|
|
if (!empty($auth)) {
|
|
|
|
$dsn .= "$auth@";
|
2022-04-24 14:32:58 +02:00
|
|
|
}
|
2023-05-12 15:06:01 +02:00
|
|
|
if (isset($options['server'])) {
|
|
|
|
$dsn .= urlencode($options['server']);
|
2022-04-24 14:32:58 +02:00
|
|
|
}
|
2023-05-12 15:06:01 +02:00
|
|
|
if (isset($options['port'])) {
|
|
|
|
$dsn .= ":{$options['port']}";
|
2022-04-24 14:32:58 +02:00
|
|
|
}
|
2023-05-12 15:06:01 +02:00
|
|
|
if (isset($options['options'])) {
|
|
|
|
$dsn .= '?' . http_build_query($options['options']);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'mail':
|
|
|
|
case 'native':
|
|
|
|
$dsn = 'native://default';
|
2022-04-24 14:32:58 +02:00
|
|
|
break;
|
|
|
|
case 'sendmail':
|
2023-05-12 15:06:01 +02:00
|
|
|
$dsn = 'sendmail://default';
|
|
|
|
$bin = $config->get('plugins.email.mailer.sendmail.bin');
|
|
|
|
if (isset($bin)) {
|
|
|
|
$dsn .= '?command=' . urlencode($bin);
|
|
|
|
}
|
|
|
|
break;
|
2022-04-24 14:32:58 +02:00
|
|
|
default:
|
2023-05-12 15:06:01 +02:00
|
|
|
$e = new Event(['engine' => $engine, ]);
|
|
|
|
Grav::instance()->fireEvent('onEmailTransportDsn', $e);
|
|
|
|
if (isset($e['dsn'])) {
|
|
|
|
$dsn = $e['dsn'];
|
|
|
|
}
|
2022-04-24 14:32:58 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-05-12 15:06:01 +02:00
|
|
|
if ($dsn instanceof TransportInterface) {
|
|
|
|
$transport = $dsn;
|
|
|
|
} else {
|
|
|
|
$transport = Transport::fromDsn($dsn) ;
|
|
|
|
}
|
|
|
|
|
2022-04-24 14:32:58 +02:00
|
|
|
return $transport;
|
|
|
|
}
|
2023-05-12 15:06:01 +02:00
|
|
|
|
2024-05-17 20:26:01 +02:00
|
|
|
/**
|
|
|
|
* Get any message from the last send attempt
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function getLastSendMessage(): ?string
|
|
|
|
{
|
|
|
|
return $this->message;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get any debug information from the last send attempt
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function getLastSendDebug(): ?string
|
|
|
|
{
|
|
|
|
return $this->debug;
|
|
|
|
}
|
|
|
|
|
2023-05-12 15:06:01 +02:00
|
|
|
/**
|
|
|
|
* @param array $recipients
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function jsonifyRecipients(array $recipients): string
|
|
|
|
{
|
|
|
|
$json = [];
|
|
|
|
foreach ($recipients as $recipient) {
|
|
|
|
$json[] = str_replace('"', "", $recipient->toString());
|
|
|
|
}
|
|
|
|
return json_encode($json);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function isValidEmail($email): bool
|
|
|
|
{
|
|
|
|
return is_string($email) && filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
* @deprecated 4.0 Switched from Swiftmailer to Symfony/Mailer - No longer supported
|
|
|
|
*/
|
|
|
|
public static function flushQueue() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
* @deprecated 4.0 Switched from Swiftmailer to Symfony/Mailer - No longer supported
|
|
|
|
*/
|
|
|
|
public static function clearQueueFailures() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an attachment.
|
|
|
|
*
|
|
|
|
* @param string $data
|
|
|
|
* @param string $filename
|
|
|
|
* @param string $contentType
|
|
|
|
* @deprecated 4.0 Switched from Swiftmailer to Symfony/Mailer - No longer supported
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function attachment($data = null, $filename = null, $contentType = null) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an embedded attachment.
|
|
|
|
*
|
|
|
|
* @param string $data
|
|
|
|
* @param string $filename
|
|
|
|
* @param string $contentType
|
|
|
|
* @deprecated 4.0 Switched from Swiftmailer to Symfony/Mailer - No longer supported
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function embedded($data = null, $filename = null, $contentType = null) {}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an image attachment.
|
|
|
|
*
|
|
|
|
* @param string $data
|
|
|
|
* @param string $filename
|
|
|
|
* @param string $contentType
|
|
|
|
* @deprecated 4.0 Switched from Swiftmailer to Symfony/Mailer - No longer supported
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function image($data = null, $filename = null, $contentType = null) {}
|
|
|
|
|
2022-04-24 14:32:58 +02:00
|
|
|
}
|