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;
14 use Friendica\Util\ConfigFileLoader;
15 use PHPMailer\PHPMailer\PHPMailer;
16 use PHPMailer\PHPMailer\Exception;
18 function phpmailer_install()
20 Hook::register('load_config' , __FILE__, 'phpmailer_load_config');
21 Hook::register('emailer_send_prepare', __FILE__, 'phpmailer_emailer_send_prepare');
24 function phpmailer_load_config(App $a, ConfigFileLoader $loader)
26 $a->getConfigCache()->load($loader->loadAddonConfig('phpmailer'));
33 function phpmailer_emailer_send_prepare(App $a, array &$b)
35 require_once __DIR__ . '/phpmailer/src/PHPMailer.php';
36 require_once __DIR__ . '/phpmailer/src/SMTP.php';
37 require_once __DIR__ . '/phpmailer/src/Exception.php';
39 // Passing `true` enables exceptions
40 $mail = new PHPMailer(true);
42 if (Config::get('phpmailer', 'smtp')) {
43 // Set mailer to use SMTP
47 $mail->CharSet = 'UTF-8';
48 $mail->Encoding = 'base64';
50 // Specify main and backup SMTP servers
51 $mail->Host = Config::get('phpmailer', 'smtp_server');
52 $mail->Port = Config::get('phpmailer', 'smtp_port');
54 if (Config::get('system', 'smtp_secure') && Config::get('phpmailer', 'smtp_port_s')) {
55 $mail->SMTPSecure = Config::get('phpmailer', 'smtp_secure');
56 $mail->Port = Config::get('phpmailer', 'smtp_port_s');
59 if (Config::get('phpmailer', 'smtp_username') && Config::get('phpmailer', 'smtp_password')) {
60 $mail->SMTPAuth = true;
61 $mail->Username = Config::get('phpmailer', 'smtp_username');
62 $mail->Password = Config::get('phpmailer', 'smtp_password');
65 if (Config::get('phpmailer', 'smtp_from')) {
66 $mail->setFrom(Config::get('phpmailer', 'smtp_from'), $b['fromName']);
69 $mail->setFrom($b['fromEmail'], $b['fromName']);
73 $mail->Subject = $b['messageSubject'];
75 if (!empty($b['toEmail'])) {
76 $mail->addAddress($b['toEmail']);
80 if (!empty($b['htmlVersion'])) {
82 $mail->Body = $b['htmlVersion'];
83 $mail->AltBody = $b['textVersion'];
86 $mail->Body = $b['textVersion'];
89 if (!empty($b['replyTo'])) {
90 $mail->addReplyTo($b['replyTo'], $b['fromName']);
94 if (!empty($b['additionalMailHeader'])) {
95 foreach (explode("\n", trim($b['additionalMailHeader'])) as $header_line) {
96 list($name, $value) = explode(':', $header_line, 2);
97 $mail->addCustomHeader(trim($name), trim($value));
101 $b['sent'] = $mail->send();
102 } catch (Exception $e) {
103 DI::logger()->error('PHPMailer error', ['ErrorInfo' => $mail->ErrorInfo, 'code' => $e->getCode(), 'message' => $e->getMessage()]);