3 * Name: PHP Mailer SMTP
4 * Description: Connects to a SMTP server based on the config
6 * Author: Marcus Mueller
7 * Maintainer: Hypolite Petovan <hypolite@friendica.mrpetovan.com>
11 use Friendica\Core\Config;
12 use Friendica\Core\Hook;
13 use Friendica\Util\ConfigFileLoader;
14 use PHPMailer\PHPMailer\PHPMailer;
15 use PHPMailer\PHPMailer\Exception;
17 function phpmailer_install()
19 Hook::register('load_config' , __FILE__, 'phpmailer_load_config');
20 Hook::register('emailer_send_prepare', __FILE__, 'phpmailer_emailer_send_prepare');
23 function phpmailer_load_config(App $a, ConfigFileLoader $loader)
25 $a->getConfigCache()->load($loader->loadAddonConfig('phpmailer'));
32 function phpmailer_emailer_send_prepare(App $a, array &$b)
34 require_once __DIR__ . '/phpmailer/src/PHPMailer.php';
35 require_once __DIR__ . '/phpmailer/src/SMTP.php';
36 require_once __DIR__ . '/phpmailer/src/Exception.php';
38 // Passing `true` enables exceptions
39 $mail = new PHPMailer(true);
41 if (Config::get('phpmailer', 'smtp')) {
42 // Set mailer to use SMTP
46 $mail->CharSet = 'UTF-8';
47 $mail->Encoding = 'base64';
49 // Specify main and backup SMTP servers
50 $mail->Host = Config::get('phpmailer', 'smtp_server');
51 $mail->Port = Config::get('phpmailer', 'smtp_port');
53 if (Config::get('system', 'smtp_secure') && Config::get('phpmailer', 'smtp_port_s')) {
54 $mail->SMTPSecure = Config::get('phpmailer', 'smtp_secure');
55 $mail->Port = Config::get('phpmailer', 'smtp_port_s');
58 if (Config::get('phpmailer', 'smtp_username') && Config::get('phpmailer', 'smtp_password')) {
59 $mail->SMTPAuth = true;
60 $mail->Username = Config::get('phpmailer', 'smtp_username');
61 $mail->Password = Config::get('phpmailer', 'smtp_password');
64 if (Config::get('phpmailer', 'smtp_from')) {
65 $mail->setFrom(Config::get('phpmailer', 'smtp_from'), $b['fromName']);
68 $mail->setFrom($b['fromEmail'], $b['fromName']);
72 $mail->Subject = $b['messageSubject'];
74 if (!empty($b['toEmail'])) {
75 $mail->addAddress($b['toEmail']);
79 if (!empty($b['htmlVersion'])) {
81 $mail->Body = $b['htmlVersion'];
82 $mail->AltBody = $b['textVersion'];
85 $mail->Body = $b['textVersion'];
88 if (!empty($b['replyTo'])) {
89 $mail->addReplyTo($b['replyTo'], $b['fromName']);
93 if (!empty($b['additionalMailHeader'])) {
94 foreach (explode("\n", trim($b['additionalMailHeader'])) as $header_line) {
95 list($name, $value) = explode(':', $header_line, 2);
96 $mail->addCustomHeader(trim($name), trim($value));
100 $b['sent'] = $mail->send();
101 } catch (Exception $e) {
102 $a->getLogger()->error('PHPMailer error', ['ErrorInfo' => $mail->ErrorInfo, 'code' => $e->getCode(), 'message' => $e->getMessage()]);