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
|
|
|
*/
|
|
|
|
|
|
|
|
use Friendica\App;
|
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;
|
2019-06-23 17:56:21 +00:00
|
|
|
use Friendica\Util\ConfigFileLoader;
|
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');
|
|
|
|
Hook::register('emailer_send_prepare', __FILE__, 'phpmailer_emailer_send_prepare');
|
2018-06-07 19:17:17 +00:00
|
|
|
}
|
|
|
|
|
2019-05-26 03:48:51 +00:00
|
|
|
function phpmailer_load_config(App $a, ConfigFileLoader $loader)
|
2018-06-07 19:17:17 +00:00
|
|
|
{
|
2019-05-26 03:48:51 +00:00
|
|
|
$a->getConfigCache()->load($loader->loadAddonConfig('phpmailer'));
|
2018-06-07 19:17:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param App $a
|
2020-01-29 19:20:39 +00:00
|
|
|
* @param IEmail $email
|
2018-06-07 19:17:17 +00:00
|
|
|
*/
|
2020-01-29 19:20:39 +00:00
|
|
|
function phpmailer_emailer_send_prepare(App $a, 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-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
|
|
|
|
2019-04-12 10:13:13 +00:00
|
|
|
// Setup encoding.
|
2020-01-29 19:20:39 +00:00
|
|
|
$mailer->CharSet = 'UTF-8';
|
|
|
|
$mailer->Encoding = 'base64';
|
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())) {
|
|
|
|
foreach (explode("\n", trim($email->getAdditionalMailHeader())) as $header_line) {
|
2019-05-26 03:48:51 +00:00
|
|
|
list($name, $value) = explode(':', $header_line, 2);
|
2020-01-29 19:20:39 +00:00
|
|
|
$mailer->addCustomHeader(trim($name), trim($value));
|
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
|
|
|
}
|
|
|
|
}
|