[phpmailer] Rework addon
- Remove redundant uninstall function - Add default config file - Add replyTo support - Add custom headers support - Add plain text-only support - Improve error handling
This commit is contained in:
parent
c228c4523c
commit
c8001ac4c4
44
phpmailer/config/phpmailer.config.php
Normal file
44
phpmailer/config/phpmailer.config.php
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
// Warning: Don't change this file! It only holds the default config values for this addon.
|
||||||
|
// Instead overwrite these config values in config/addon.config.php in your Friendica directory
|
||||||
|
|
||||||
|
return [
|
||||||
|
'phpmailer' => [
|
||||||
|
// smtp (Boolean)
|
||||||
|
// Enables SMTP relaying for outbound emails
|
||||||
|
'smtp' => false,
|
||||||
|
|
||||||
|
// smtp_server (String)
|
||||||
|
// SMTP server host name
|
||||||
|
'smtp_server' => 'smtp.example.com',
|
||||||
|
|
||||||
|
// smtp_port (Integer)
|
||||||
|
// SMTP server port number
|
||||||
|
'smtp_port' => 25,
|
||||||
|
|
||||||
|
// smtp_secure (String)
|
||||||
|
// What kind of encryption to use on the SMTP connection.
|
||||||
|
// Options: '', 'ssl' or 'tls'.
|
||||||
|
'smtp_secure' => '',
|
||||||
|
|
||||||
|
// smtp_port_s (Integer)
|
||||||
|
// Secure SMTP server port number
|
||||||
|
'smtp_port_s' => 465,
|
||||||
|
|
||||||
|
// smtp_username (String)
|
||||||
|
// SMTP server authentication user name
|
||||||
|
// Empty string disables authentication
|
||||||
|
'smtp_username' => '',
|
||||||
|
|
||||||
|
// smtp_password (String)
|
||||||
|
// SMTP server authentication password
|
||||||
|
// Empty string disables authentication
|
||||||
|
'smtp_password' => '',
|
||||||
|
|
||||||
|
// smtp_from (String)
|
||||||
|
// From address used when using the SMTP server
|
||||||
|
// Example: no-reply@example.com
|
||||||
|
'smtp_from' => '',
|
||||||
|
],
|
||||||
|
];
|
|
@ -2,32 +2,27 @@
|
||||||
/**
|
/**
|
||||||
* Name: PHP Mailer SMTP
|
* Name: PHP Mailer SMTP
|
||||||
* Description: Connects to a SMTP server based on the config
|
* Description: Connects to a SMTP server based on the config
|
||||||
* Version: 0.1
|
* Version: 0.2
|
||||||
* Author: Marcus Mueller
|
* Author: Marcus Mueller
|
||||||
|
* Maintainer: Hypolite Petovan <hypolite@friendica.mrpetovan.com>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use Friendica\App;
|
use Friendica\App;
|
||||||
use Friendica\Core\Addon;
|
|
||||||
use Friendica\Core\Config;
|
use Friendica\Core\Config;
|
||||||
|
use Friendica\Core\Hook;
|
||||||
|
use Friendica\Util\Config\ConfigFileLoader;
|
||||||
use PHPMailer\PHPMailer\PHPMailer;
|
use PHPMailer\PHPMailer\PHPMailer;
|
||||||
use PHPMailer\PHPMailer\Exception;
|
use PHPMailer\PHPMailer\Exception;
|
||||||
|
|
||||||
function phpmailer_install()
|
function phpmailer_install()
|
||||||
{
|
{
|
||||||
Addon::registerHook(
|
Hook::register('load_config' , __FILE__, 'phpmailer_load_config');
|
||||||
'emailer_send_prepare',
|
Hook::register('emailer_send_prepare', __FILE__, 'phpmailer_emailer_send_prepare');
|
||||||
__FILE__,
|
|
||||||
'phpmailer_emailer_send_prepare'
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function phpmailer_uninstall()
|
function phpmailer_load_config(App $a, ConfigFileLoader $loader)
|
||||||
{
|
{
|
||||||
Addon::unregisterHook(
|
$a->getConfigCache()->load($loader->loadAddonConfig('phpmailer'));
|
||||||
'emailer_send_prepare',
|
|
||||||
__FILE__,
|
|
||||||
'phpmailer_emailer_send_prepare'
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -46,13 +41,11 @@ function phpmailer_emailer_send_prepare(App $a, array &$b)
|
||||||
if (Config::get('phpmailer', 'smtp')) {
|
if (Config::get('phpmailer', 'smtp')) {
|
||||||
// Set mailer to use SMTP
|
// Set mailer to use SMTP
|
||||||
$mail->isSMTP();
|
$mail->isSMTP();
|
||||||
/*
|
|
||||||
// Enable verbose debug output
|
|
||||||
$mail->SMTPDebug = 2;
|
|
||||||
*/
|
|
||||||
// Setup encoding.
|
// Setup encoding.
|
||||||
$mail->CharSet = 'UTF-8';
|
$mail->CharSet = 'UTF-8';
|
||||||
$mail->Encoding = 'base64';
|
$mail->Encoding = 'base64';
|
||||||
|
|
||||||
// Specify main and backup SMTP servers
|
// Specify main and backup SMTP servers
|
||||||
$mail->Host = Config::get('phpmailer', 'smtp_server');
|
$mail->Host = Config::get('phpmailer', 'smtp_server');
|
||||||
$mail->Port = Config::get('phpmailer', 'smtp_port');
|
$mail->Port = Config::get('phpmailer', 'smtp_port');
|
||||||
|
@ -69,16 +62,15 @@ function phpmailer_emailer_send_prepare(App $a, array &$b)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Config::get('phpmailer', 'smtp_from')) {
|
if (Config::get('phpmailer', 'smtp_from')) {
|
||||||
$mail->setFrom(Config::get('phpmailer', 'smtp_from'), Config::get('config', 'sitename'));
|
$mail->setFrom(Config::get('phpmailer', 'smtp_from'), $b['fromName']);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
$mail->setFrom($b['fromEmail'], $b['fromName']);
|
||||||
}
|
}
|
||||||
|
|
||||||
// subject
|
// subject
|
||||||
$mail->Subject = $b['messageSubject'];
|
$mail->Subject = $b['messageSubject'];
|
||||||
|
|
||||||
// add text
|
|
||||||
$mail->AltBody = $b['textVersion'];
|
|
||||||
|
|
||||||
if (!empty($b['toEmail'])) {
|
if (!empty($b['toEmail'])) {
|
||||||
$mail->addAddress($b['toEmail']);
|
$mail->addAddress($b['toEmail']);
|
||||||
}
|
}
|
||||||
|
@ -87,18 +79,26 @@ function phpmailer_emailer_send_prepare(App $a, array &$b)
|
||||||
if (!empty($b['htmlVersion'])) {
|
if (!empty($b['htmlVersion'])) {
|
||||||
$mail->isHTML(true);
|
$mail->isHTML(true);
|
||||||
$mail->Body = $b['htmlVersion'];
|
$mail->Body = $b['htmlVersion'];
|
||||||
|
$mail->AltBody = $b['textVersion'];
|
||||||
|
} else {
|
||||||
|
// add text
|
||||||
|
$mail->Body = $b['textVersion'];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($b['replyTo'])) {
|
||||||
|
$mail->addReplyTo($b['replyTo'], $b['fromName']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
// additional headers
|
// additional headers
|
||||||
if (!empty($b['additionalMailHeader'])) {
|
if (!empty($b['additionalMailHeader'])) {
|
||||||
$mail->addCustomHeader($b['additionalMailHeader']);
|
foreach (explode("\n", trim($b['additionalMailHeader'])) as $header_line) {
|
||||||
|
list($name, $value) = explode(':', $header_line, 2);
|
||||||
|
$mail->addCustomHeader(trim($name), trim($value));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
$mail->send();
|
$mail->send();
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
|
$a->getLogger()->error('PHPMailer error', ['ErrorInfo' => $mail->ErrorInfo, 'code' => $e->getCode(), 'message' => $e->getMessage()]);
|
||||||
die();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue