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