3 * Name: PHP Mailer SMTP
4 * Description: Connects to a SMTP server based on the config
6 * Author: Marcus Mueller
10 use Friendica\Core\Addon;
11 use Friendica\Core\Config;
12 use PHPMailer\PHPMailer\PHPMailer;
13 use PHPMailer\PHPMailer\Exception;
15 function phpmailer_install()
18 'emailer_send_prepare',
20 'phpmailer_emailer_send_prepare'
24 function phpmailer_uninstall()
26 Addon::unregisterHook(
27 'emailer_send_prepare',
29 'phpmailer_emailer_send_prepare'
37 function phpmailer_emailer_send_prepare(App $a, array &$b)
39 require_once __DIR__ . '/phpmailer/src/PHPMailer.php';
40 require_once __DIR__ . '/phpmailer/src/SMTP.php';
41 require_once __DIR__ . '/phpmailer/src/Exception.php';
43 // Passing `true` enables exceptions
44 $mail = new PHPMailer(true);
46 if (Config::get('phpmailer', 'smtp')) {
47 // Set mailer to use SMTP
50 // Enable verbose debug output
54 $mail->CharSet = 'UTF-8';
55 $mail->Encoding = 'base64';
56 // Specify main and backup SMTP servers
57 $mail->Host = Config::get('phpmailer', 'smtp_server');
58 $mail->Port = Config::get('phpmailer', 'smtp_port');
60 if (Config::get('system', 'smtp_secure') && Config::get('phpmailer', 'smtp_port_s')) {
61 $mail->SMTPSecure = Config::get('phpmailer', 'smtp_secure');
62 $mail->Port = Config::get('phpmailer', 'smtp_port_s');
65 if (Config::get('phpmailer', 'smtp_username') && Config::get('phpmailer', 'smtp_password')) {
66 $mail->SMTPAuth = true;
67 $mail->Username = Config::get('phpmailer', 'smtp_username');
68 $mail->Password = Config::get('phpmailer', 'smtp_password');
71 if (Config::get('phpmailer', 'smtp_from')) {
72 $mail->setFrom(Config::get('phpmailer', 'smtp_from'), Config::get('config', 'sitename'));
77 $mail->Subject = $b['messageSubject'];
80 $mail->AltBody = $b['textVersion'];
82 if (!empty($b['toEmail'])) {
83 $mail->addAddress($b['toEmail']);
87 if (!empty($b['htmlVersion'])) {
89 $mail->Body = $b['htmlVersion'];
94 if (!empty($b['additionalMailHeader'])) {
95 $mail->addCustomHeader($b['additionalMailHeader']);
100 } catch (Exception $e) {
101 echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;