2018-06-07 19:17:17 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Name: PHP Mailer SMTP
|
|
|
|
* Description: Connects to a SMTP server based on the config
|
2019-05-26 03:48:51 +00:00
|
|
|
* Version: 0.2
|
2019-04-01 17:04:51 +00:00
|
|
|
* Author: Marcus Mueller
|
2019-05-26 03:48:51 +00:00
|
|
|
* Maintainer: Hypolite Petovan <hypolite@friendica.mrpetovan.com>
|
2018-06-07 19:17:17 +00:00
|
|
|
*/
|
|
|
|
|
2019-05-26 03:48:51 +00:00
|
|
|
use Friendica\Core\Hook;
|
2019-12-15 22:46:55 +00:00
|
|
|
use Friendica\DI;
|
2020-01-29 19:20:39 +00:00
|
|
|
use Friendica\Object\EMail\IEmail;
|
2023-01-01 19:21:56 +00:00
|
|
|
use Friendica\Core\Config\Util\ConfigFileManager;
|
2018-06-07 19:17:17 +00:00
|
|
|
use PHPMailer\PHPMailer\PHPMailer;
|
|
|
|
use PHPMailer\PHPMailer\Exception;
|
|
|
|
|
2020-01-29 19:20:39 +00:00
|
|
|
require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
|
|
|
|
|
2018-06-07 19:17:17 +00:00
|
|
|
function phpmailer_install()
|
|
|
|
{
|
2019-05-26 03:48:51 +00:00
|
|
|
Hook::register('load_config' , __FILE__, 'phpmailer_load_config');
|
2021-05-29 18:59:51 +00:00
|
|
|
Hook::register('emailer_send_prepare', __FILE__, 'phpmailer_emailer_send_prepare', 5);
|
2018-06-07 19:17:17 +00:00
|
|
|
}
|
|
|
|
|
2023-01-14 02:16:09 +00:00
|
|
|
function phpmailer_load_config(ConfigFileManager $loader)
|
2018-06-07 19:17:17 +00:00
|
|
|
{
|
2024-12-11 21:47:10 +00:00
|
|
|
DI::appHelper()->getConfigCache()->load($loader->loadAddonConfig('phpmailer'), \Friendica\Core\Config\ValueObject\Cache::SOURCE_STATIC);
|
2018-06-07 19:17:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-29 19:20:39 +00:00
|
|
|
* @param IEmail $email
|
2018-06-07 19:17:17 +00:00
|
|
|
*/
|
2023-01-14 02:16:09 +00:00
|
|
|
function phpmailer_emailer_send_prepare(IEmail &$email)
|
2018-06-07 19:17:17 +00:00
|
|
|
{
|
|
|
|
// Passing `true` enables exceptions
|
2020-01-29 19:20:39 +00:00
|
|
|
$mailer = new PHPMailer(true);
|
2018-06-07 19:17:17 +00:00
|
|
|
try {
|
2020-11-29 20:41:46 +00:00
|
|
|
// Setup encoding.
|
|
|
|
$mailer->CharSet = 'UTF-8';
|
|
|
|
$mailer->Encoding = 'base64';
|
|
|
|
|
2020-01-19 20:21:12 +00:00
|
|
|
if (DI::config()->get('phpmailer', 'smtp')) {
|
2018-06-07 19:17:17 +00:00
|
|
|
// Set mailer to use SMTP
|
2020-01-29 19:20:39 +00:00
|
|
|
$mailer->isSMTP();
|
2019-05-26 03:48:51 +00:00
|
|
|
|
2018-06-07 19:17:17 +00:00
|
|
|
// Specify main and backup SMTP servers
|
2020-01-29 19:20:39 +00:00
|
|
|
$mailer->Host = DI::config()->get('phpmailer', 'smtp_server');
|
|
|
|
$mailer->Port = DI::config()->get('phpmailer', 'smtp_port');
|
2018-06-07 19:17:17 +00:00
|
|
|
|
2020-01-19 20:21:12 +00:00
|
|
|
if (DI::config()->get('system', 'smtp_secure') && DI::config()->get('phpmailer', 'smtp_port_s')) {
|
2020-01-29 19:20:39 +00:00
|
|
|
$mailer->SMTPSecure = DI::config()->get('phpmailer', 'smtp_secure');
|
|
|
|
$mailer->Port = DI::config()->get('phpmailer', 'smtp_port_s');
|
2018-06-07 19:17:17 +00:00
|
|
|
}
|
|
|
|
|
2020-01-19 20:21:12 +00:00
|
|
|
if (DI::config()->get('phpmailer', 'smtp_username') && DI::config()->get('phpmailer', 'smtp_password')) {
|
2020-01-29 19:20:39 +00:00
|
|
|
$mailer->SMTPAuth = true;
|
|
|
|
$mailer->Username = DI::config()->get('phpmailer', 'smtp_username');
|
|
|
|
$mailer->Password = DI::config()->get('phpmailer', 'smtp_password');
|
2018-06-07 19:17:17 +00:00
|
|
|
}
|
|
|
|
|
2020-01-19 20:21:12 +00:00
|
|
|
if (DI::config()->get('phpmailer', 'smtp_from')) {
|
2020-01-29 19:20:39 +00:00
|
|
|
$mailer->setFrom(DI::config()->get('phpmailer', 'smtp_from'), $email->getFromName());
|
2018-06-07 19:17:17 +00:00
|
|
|
}
|
2019-05-26 03:48:51 +00:00
|
|
|
} else {
|
2020-01-29 19:20:39 +00:00
|
|
|
$mailer->setFrom($email->getFromAddress(), $email->getFromName());
|
2018-06-07 19:17:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// subject
|
2020-01-29 19:20:39 +00:00
|
|
|
$mailer->Subject = $email->getSubject();
|
2018-06-07 19:17:17 +00:00
|
|
|
|
2020-01-29 19:20:39 +00:00
|
|
|
if (!empty($email->getToAddress())) {
|
|
|
|
$mailer->addAddress($email->getToAddress());
|
2019-03-28 12:54:58 +00:00
|
|
|
}
|
|
|
|
|
2018-06-07 19:17:17 +00:00
|
|
|
// html version
|
2020-01-29 19:20:39 +00:00
|
|
|
if (!empty($email->getMessage())) {
|
|
|
|
$mailer->isHTML(true);
|
|
|
|
$mailer->Body = $email->getMessage();
|
|
|
|
$mailer->AltBody = $email->getMessage(true);
|
2019-05-26 03:48:51 +00:00
|
|
|
} else {
|
|
|
|
// add text
|
2020-01-29 19:20:39 +00:00
|
|
|
$mailer->Body = $email->getMessage(true);
|
2019-05-26 03:48:51 +00:00
|
|
|
}
|
|
|
|
|
2020-01-29 19:20:39 +00:00
|
|
|
if (!empty($email->getReplyTo())) {
|
|
|
|
$mailer->addReplyTo($email->getReplyTo(), $email->getFromName());
|
2018-06-07 19:17:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// additional headers
|
2020-01-29 19:20:39 +00:00
|
|
|
if (!empty($email->getAdditionalMailHeader())) {
|
2020-09-19 18:14:55 +00:00
|
|
|
foreach ($email->getAdditionalMailHeader() as $name => $values) {
|
2020-09-19 20:49:44 +00:00
|
|
|
// Set the "Message-ID" header for PHP-Mailer directly
|
2020-09-22 20:48:34 +00:00
|
|
|
if (strtolower($name) === 'message-id') {
|
2020-09-19 20:49:44 +00:00
|
|
|
// implode all values to one entry, because there's only one value possible
|
|
|
|
$mailer->MessageID = trim(implode("", $values));
|
|
|
|
} else {
|
|
|
|
$mailer->addCustomHeader(trim($name), trim(implode("\n", $values)));
|
2020-09-19 18:14:55 +00:00
|
|
|
}
|
2019-05-26 03:48:51 +00:00
|
|
|
}
|
2018-06-07 19:17:17 +00:00
|
|
|
}
|
|
|
|
|
2020-01-29 19:20:39 +00:00
|
|
|
if ($mailer->send()) {
|
|
|
|
$email = null;
|
|
|
|
}
|
2018-06-07 19:17:17 +00:00
|
|
|
} catch (Exception $e) {
|
2020-01-29 19:20:39 +00:00
|
|
|
DI::logger()->error('PHPMailer error', ['email' => $email, 'ErrorInfo' => $mailer->ErrorInfo, 'exception' => $e]);
|
2018-06-07 19:17:17 +00:00
|
|
|
}
|
|
|
|
}
|