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