From: Hypolite Petovan Date: Sun, 26 May 2019 03:48:51 +0000 (-0400) Subject: [phpmailer] Rework addon X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=c8001ac4c47feaf86a4b67611a59f705e73c99ef;p=friendica-addons.git [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 --- diff --git a/phpmailer/config/phpmailer.config.php b/phpmailer/config/phpmailer.config.php new file mode 100644 index 00000000..b2916471 --- /dev/null +++ b/phpmailer/config/phpmailer.config.php @@ -0,0 +1,44 @@ + [ + // 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' => '', + ], +]; diff --git a/phpmailer/phpmailer.php b/phpmailer/phpmailer.php index 96643f94..cd86a836 100644 --- a/phpmailer/phpmailer.php +++ b/phpmailer/phpmailer.php @@ -2,32 +2,27 @@ /** * Name: PHP Mailer SMTP * Description: Connects to a SMTP server based on the config - * Version: 0.1 + * Version: 0.2 * Author: Marcus Mueller + * Maintainer: Hypolite Petovan */ use Friendica\App; -use Friendica\Core\Addon; use Friendica\Core\Config; +use Friendica\Core\Hook; +use Friendica\Util\Config\ConfigFileLoader; use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; function phpmailer_install() { - Addon::registerHook( - 'emailer_send_prepare', - __FILE__, - 'phpmailer_emailer_send_prepare' - ); + Hook::register('load_config' , __FILE__, 'phpmailer_load_config'); + Hook::register('emailer_send_prepare', __FILE__, 'phpmailer_emailer_send_prepare'); } -function phpmailer_uninstall() +function phpmailer_load_config(App $a, ConfigFileLoader $loader) { - Addon::unregisterHook( - 'emailer_send_prepare', - __FILE__, - 'phpmailer_emailer_send_prepare' - ); + $a->getConfigCache()->load($loader->loadAddonConfig('phpmailer')); } /** @@ -46,13 +41,11 @@ function phpmailer_emailer_send_prepare(App $a, array &$b) if (Config::get('phpmailer', 'smtp')) { // Set mailer to use SMTP $mail->isSMTP(); - /* - // Enable verbose debug output - $mail->SMTPDebug = 2; - */ + // Setup encoding. $mail->CharSet = 'UTF-8'; $mail->Encoding = 'base64'; + // Specify main and backup SMTP servers $mail->Host = Config::get('phpmailer', 'smtp_server'); $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')) { - $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 $mail->Subject = $b['messageSubject']; - // add text - $mail->AltBody = $b['textVersion']; - if (!empty($b['toEmail'])) { $mail->addAddress($b['toEmail']); } @@ -87,18 +79,26 @@ function phpmailer_emailer_send_prepare(App $a, array &$b) if (!empty($b['htmlVersion'])) { $mail->isHTML(true); $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 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(); } catch (Exception $e) { - echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo; - die(); + $a->getLogger()->error('PHPMailer error', ['ErrorInfo' => $mail->ErrorInfo, 'code' => $e->getCode(), 'message' => $e->getMessage()]); } }