]> git.mxchange.org Git - friendica-addons.git/commitdiff
[phpmailer] Rework addon
authorHypolite Petovan <hypolite@mrpetovan.com>
Sun, 26 May 2019 03:48:51 +0000 (23:48 -0400)
committerHypolite Petovan <hypolite@mrpetovan.com>
Mon, 27 May 2019 02:07:19 +0000 (22:07 -0400)
- Remove redundant uninstall function
- Add default config file
- Add replyTo support
- Add custom headers support
- Add plain text-only support
- Improve error handling

phpmailer/config/phpmailer.config.php [new file with mode: 0644]
phpmailer/phpmailer.php

diff --git a/phpmailer/config/phpmailer.config.php b/phpmailer/config/phpmailer.config.php
new file mode 100644 (file)
index 0000000..b291647
--- /dev/null
@@ -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' => '',
+       ],
+];
index 96643f94edbbf725dc37223c319db286da07a46f..cd86a836ae93f7df3e0545b9b763ba362a3cf56f 100644 (file)
@@ -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 <hypolite@friendica.mrpetovan.com>
  */
 
 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()]);
        }
 }