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
53 // Specify main and backup SMTP servers
54 $mail->Host = Config::get('phpmailer', 'smtp_server');
55 $mail->Port = Config::get('phpmailer', 'smtp_port');
57 if (Config::get('system', 'smtp_secure') && Config::get('phpmailer', 'smtp_port_s')) {
58 $mail->SMTPSecure = Config::get('phpmailer', 'smtp_secure');
59 $mail->Port = Config::get('phpmailer', 'smtp_port_s');
62 if (Config::get('phpmailer', 'smtp_username') && Config::get('phpmailer', 'smtp_password')) {
63 $mail->SMTPAuth = true;
64 $mail->Username = Config::get('phpmailer', 'smtp_username');
65 $mail->Password = Config::get('phpmailer', 'smtp_password');
68 if (Config::get('phpmailer', 'smtp_from')) {
69 $mail->setFrom(Config::get('phpmailer', 'smtp_from'), Config::get('config', 'sitename'));
74 $mail->Subject = $b['messageSubject'];
77 $mail->AltBody = $b['textVersion'];
79 if (!empty($b['toEmail'])) {
80 $mail->addAddress($b['toEmail']);
84 if (!empty($b['htmlVersion'])) {
86 $mail->Body = $b['htmlVersion'];
91 if (!empty($b['additionalMailHeader'])) {
92 $mail->addCustomHeader($b['additionalMailHeader']);
97 } catch (Exception $e) {
98 echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;