2018-06-07 15:17:17 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Name: PHP Mailer SMTP
|
|
|
|
* Description: Connects to a SMTP server based on the config
|
|
|
|
* Version: 0.1
|
2019-04-01 13:04:51 -04:00
|
|
|
* Author: Marcus Mueller
|
2018-06-07 15:17:17 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
use Friendica\App;
|
|
|
|
use Friendica\Core\Addon;
|
2019-04-10 10:56:16 -04:00
|
|
|
use Friendica\Core\Config;
|
2018-06-07 15:17:17 -04:00
|
|
|
use PHPMailer\PHPMailer\PHPMailer;
|
|
|
|
use PHPMailer\PHPMailer\Exception;
|
|
|
|
|
|
|
|
function phpmailer_install()
|
|
|
|
{
|
|
|
|
Addon::registerHook(
|
|
|
|
'emailer_send_prepare',
|
2019-04-01 13:04:51 -04:00
|
|
|
__FILE__,
|
2018-06-07 15:17:17 -04:00
|
|
|
'phpmailer_emailer_send_prepare'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function phpmailer_uninstall()
|
|
|
|
{
|
|
|
|
Addon::unregisterHook(
|
|
|
|
'emailer_send_prepare',
|
2019-04-01 13:04:51 -04:00
|
|
|
__FILE__,
|
2018-06-07 15:17:17 -04:00
|
|
|
'phpmailer_emailer_send_prepare'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param App $a
|
|
|
|
* @param array $b
|
|
|
|
*/
|
|
|
|
function phpmailer_emailer_send_prepare(App $a, array &$b)
|
|
|
|
{
|
|
|
|
require_once __DIR__ . '/phpmailer/src/PHPMailer.php';
|
|
|
|
require_once __DIR__ . '/phpmailer/src/SMTP.php';
|
|
|
|
require_once __DIR__ . '/phpmailer/src/Exception.php';
|
|
|
|
|
|
|
|
// Passing `true` enables exceptions
|
|
|
|
$mail = new PHPMailer(true);
|
|
|
|
try {
|
2019-04-02 05:25:51 -04:00
|
|
|
if (Config::get('phpmailer', 'smtp')) {
|
2018-06-07 15:17:17 -04:00
|
|
|
// Set mailer to use SMTP
|
|
|
|
$mail->isSMTP();
|
|
|
|
/*
|
|
|
|
// Enable verbose debug output
|
|
|
|
$mail->SMTPDebug = 2;
|
|
|
|
*/
|
|
|
|
// Specify main and backup SMTP servers
|
2019-04-01 13:04:51 -04:00
|
|
|
$mail->Host = Config::get('phpmailer', 'smtp_server');
|
|
|
|
$mail->Port = Config::get('phpmailer', 'smtp_port');
|
2018-06-07 15:17:17 -04:00
|
|
|
|
2019-04-01 13:04:51 -04:00
|
|
|
if (Config::get('system', 'smtp_secure') && Config::get('phpmailer', 'smtp_port_s')) {
|
|
|
|
$mail->SMTPSecure = Config::get('phpmailer', 'smtp_secure');
|
|
|
|
$mail->Port = Config::get('phpmailer', 'smtp_port_s');
|
2018-06-07 15:17:17 -04:00
|
|
|
}
|
|
|
|
|
2019-04-01 13:04:51 -04:00
|
|
|
if (Config::get('phpmailer', 'smtp_username') && Config::get('phpmailer', 'smtp_password')) {
|
2018-06-07 15:17:17 -04:00
|
|
|
$mail->SMTPAuth = true;
|
2019-04-01 13:04:51 -04:00
|
|
|
$mail->Username = Config::get('phpmailer', 'smtp_username');
|
|
|
|
$mail->Password = Config::get('phpmailer', 'smtp_password');
|
2018-06-07 15:17:17 -04:00
|
|
|
}
|
|
|
|
|
2019-04-01 13:04:51 -04:00
|
|
|
if (Config::get('phpmailer', 'smtp_from')) {
|
|
|
|
$mail->setFrom(Config::get('phpmailer', 'smtp_from'), Config::get('sitename'));
|
2018-06-07 15:17:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// subject
|
|
|
|
$mail->Subject = $b['messageSubject'];
|
|
|
|
|
|
|
|
// add text
|
|
|
|
$mail->AltBody = $b['textVersion'];
|
|
|
|
|
2019-03-28 08:54:58 -04:00
|
|
|
if (!empty($b['toEmail'])) {
|
|
|
|
$mail->addAddress($b['toEmail']);
|
|
|
|
}
|
|
|
|
|
2018-06-07 15:17:17 -04:00
|
|
|
// html version
|
|
|
|
if (!empty($b['htmlVersion'])) {
|
|
|
|
$mail->isHTML(true);
|
|
|
|
$mail->Body = $b['htmlVersion'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
// additional headers
|
|
|
|
if (!empty($b['additionalMailHeader'])) {
|
|
|
|
$mail->addCustomHeader($b['additionalMailHeader']);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
$mail->send();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
}
|