X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=phpmailer%2Fphpmailer.php;h=30f10ff815f572f414a331cc848269a8fe68bf01;hb=2fc7356cf43a52ac91fa451f0e8a49f83bccf6df;hp=96643f94edbbf725dc37223c319db286da07a46f;hpb=3835705a4110680c72099398f1e11cf75f48bdc0;p=friendica-addons.git diff --git a/phpmailer/phpmailer.php b/phpmailer/phpmailer.php index 96643f94..30f10ff8 100644 --- a/phpmailer/phpmailer.php +++ b/phpmailer/phpmailer.php @@ -2,103 +2,109 @@ /** * 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\DI; +use Friendica\Object\EMail\IEmail; +use Friendica\Core\Config\Util\ConfigFileLoader; use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; +require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php'; + 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', 5); } -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'), \Friendica\Core\Config\ValueObject\Cache::SOURCE_STATIC); } /** * @param App $a - * @param array $b + * @param IEmail $email */ -function phpmailer_emailer_send_prepare(App $a, array &$b) +function phpmailer_emailer_send_prepare(App $a, IEmail &$email) { - 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); + $mailer = new PHPMailer(true); try { - if (Config::get('phpmailer', 'smtp')) { + // Setup encoding. + $mailer->CharSet = 'UTF-8'; + $mailer->Encoding = 'base64'; + + if (DI::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'; + $mailer->isSMTP(); + // Specify main and backup SMTP servers - $mail->Host = Config::get('phpmailer', 'smtp_server'); - $mail->Port = Config::get('phpmailer', 'smtp_port'); + $mailer->Host = DI::config()->get('phpmailer', 'smtp_server'); + $mailer->Port = DI::config()->get('phpmailer', 'smtp_port'); - 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'); + if (DI::config()->get('system', 'smtp_secure') && DI::config()->get('phpmailer', 'smtp_port_s')) { + $mailer->SMTPSecure = DI::config()->get('phpmailer', 'smtp_secure'); + $mailer->Port = DI::config()->get('phpmailer', 'smtp_port_s'); } - if (Config::get('phpmailer', 'smtp_username') && Config::get('phpmailer', 'smtp_password')) { - $mail->SMTPAuth = true; - $mail->Username = Config::get('phpmailer', 'smtp_username'); - $mail->Password = Config::get('phpmailer', 'smtp_password'); + if (DI::config()->get('phpmailer', 'smtp_username') && DI::config()->get('phpmailer', 'smtp_password')) { + $mailer->SMTPAuth = true; + $mailer->Username = DI::config()->get('phpmailer', 'smtp_username'); + $mailer->Password = DI::config()->get('phpmailer', 'smtp_password'); } - if (Config::get('phpmailer', 'smtp_from')) { - $mail->setFrom(Config::get('phpmailer', 'smtp_from'), Config::get('config', 'sitename')); + if (DI::config()->get('phpmailer', 'smtp_from')) { + $mailer->setFrom(DI::config()->get('phpmailer', 'smtp_from'), $email->getFromName()); } + } else { + $mailer->setFrom($email->getFromAddress(), $email->getFromName()); } // subject - $mail->Subject = $b['messageSubject']; - - // add text - $mail->AltBody = $b['textVersion']; + $mailer->Subject = $email->getSubject(); - if (!empty($b['toEmail'])) { - $mail->addAddress($b['toEmail']); + if (!empty($email->getToAddress())) { + $mailer->addAddress($email->getToAddress()); } // html version - if (!empty($b['htmlVersion'])) { - $mail->isHTML(true); - $mail->Body = $b['htmlVersion']; + if (!empty($email->getMessage())) { + $mailer->isHTML(true); + $mailer->Body = $email->getMessage(); + $mailer->AltBody = $email->getMessage(true); + } else { + // add text + $mailer->Body = $email->getMessage(true); + } + + if (!empty($email->getReplyTo())) { + $mailer->addReplyTo($email->getReplyTo(), $email->getFromName()); } - /* // additional headers - if (!empty($b['additionalMailHeader'])) { - $mail->addCustomHeader($b['additionalMailHeader']); + if (!empty($email->getAdditionalMailHeader())) { + foreach ($email->getAdditionalMailHeader() as $name => $values) { + // Set the "Message-ID" header for PHP-Mailer directly + if (strtolower($name) === 'message-id') { + // implode all values to one entry, because there's only one value possible + $mailer->MessageID = trim(implode("", $values)); + } else { + $mailer->addCustomHeader(trim($name), trim(implode("\n", $values))); + } + } } - */ - $mail->send(); + if ($mailer->send()) { + $email = null; + } } catch (Exception $e) { - echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo; - die(); + DI::logger()->error('PHPMailer error', ['email' => $email, 'ErrorInfo' => $mailer->ErrorInfo, 'exception' => $e]); } }