3 * Name: PHP Mailer SMTP
4 * Description: Connects to a SMTP server based on the config
6 * Author: Marcus Mueller <http://mat.exon.name>
10 use Friendica\Core\Addon;
11 use PHPMailer\PHPMailer\PHPMailer;
12 use PHPMailer\PHPMailer\Exception;
14 function phpmailer_install()
17 'emailer_send_prepare',
18 'addon/phpmailer/phpmailer.php',
19 'phpmailer_emailer_send_prepare'
23 function phpmailer_uninstall()
25 Addon::unregisterHook(
26 'emailer_send_prepare',
27 'addon/phpmailer/phpmailer.php',
28 'phpmailer_emailer_send_prepare'
32 function phpmailer_module()
40 function phpmailer_emailer_send_prepare(App $a, array &$b)
42 require_once __DIR__ . '/phpmailer/src/PHPMailer.php';
43 require_once __DIR__ . '/phpmailer/src/SMTP.php';
44 require_once __DIR__ . '/phpmailer/src/Exception.php';
46 // Passing `true` enables exceptions
47 $mail = new PHPMailer(true);
49 if (!empty($a->config['system']['smtp']) && (bool)$a->config['system']['smtp'] === true) {
50 // Set mailer to use SMTP
53 // Enable verbose debug output
56 // Specify main and backup SMTP servers
57 $mail->Host = $a->config['system']['smtp_server'];
58 $mail->Port = $a->config['system']['smtp_port'];
60 if (!empty($a->config['system']['smtp_secure']) && (bool)$a->config['system']['smtp_secure'] !== '') {
61 $mail->SMTPSecure = $a->config['system']['smtp_secure'];
62 $mail->Port = $a->config['system']['smtp_port_s'];
65 if (!empty($a->config['system']['smtp_username']) && !empty($a->config['system']['smtp_password'])) {
66 $mail->SMTPAuth = true;
67 $mail->Username = $a->config['system']['smtp_username'];
68 $mail->Password = $a->config['system']['smtp_password'];
71 if (!empty($a->config['system']['smtp_from']) && !empty($a->config['system']['smtp_domain'])) {
72 $mail->setFrom($a->config['system']['smtp_from'], $a->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;