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\Hook;
13 use Friendica\Object\EMail\IEmail;
14 use Friendica\Core\Config\Util\ConfigFileManager;
15 use PHPMailer\PHPMailer\PHPMailer;
16 use PHPMailer\PHPMailer\Exception;
18 require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
20 function phpmailer_install()
22 Hook::register('load_config' , __FILE__, 'phpmailer_load_config');
23 Hook::register('emailer_send_prepare', __FILE__, 'phpmailer_emailer_send_prepare', 5);
26 function phpmailer_load_config(ConfigFileManager $loader)
28 DI::app()->getConfigCache()->load($loader->loadAddonConfig('phpmailer'), \Friendica\Core\Config\ValueObject\Cache::SOURCE_STATIC);
32 * @param IEmail $email
34 function phpmailer_emailer_send_prepare(IEmail &$email)
36 // Passing `true` enables exceptions
37 $mailer = new PHPMailer(true);
40 $mailer->CharSet = 'UTF-8';
41 $mailer->Encoding = 'base64';
43 if (DI::config()->get('phpmailer', 'smtp')) {
44 // Set mailer to use SMTP
47 // Specify main and backup SMTP servers
48 $mailer->Host = DI::config()->get('phpmailer', 'smtp_server');
49 $mailer->Port = DI::config()->get('phpmailer', 'smtp_port');
51 if (DI::config()->get('system', 'smtp_secure') && DI::config()->get('phpmailer', 'smtp_port_s')) {
52 $mailer->SMTPSecure = DI::config()->get('phpmailer', 'smtp_secure');
53 $mailer->Port = DI::config()->get('phpmailer', 'smtp_port_s');
56 if (DI::config()->get('phpmailer', 'smtp_username') && DI::config()->get('phpmailer', 'smtp_password')) {
57 $mailer->SMTPAuth = true;
58 $mailer->Username = DI::config()->get('phpmailer', 'smtp_username');
59 $mailer->Password = DI::config()->get('phpmailer', 'smtp_password');
62 if (DI::config()->get('phpmailer', 'smtp_from')) {
63 $mailer->setFrom(DI::config()->get('phpmailer', 'smtp_from'), $email->getFromName());
66 $mailer->setFrom($email->getFromAddress(), $email->getFromName());
70 $mailer->Subject = $email->getSubject();
72 if (!empty($email->getToAddress())) {
73 $mailer->addAddress($email->getToAddress());
77 if (!empty($email->getMessage())) {
78 $mailer->isHTML(true);
79 $mailer->Body = $email->getMessage();
80 $mailer->AltBody = $email->getMessage(true);
83 $mailer->Body = $email->getMessage(true);
86 if (!empty($email->getReplyTo())) {
87 $mailer->addReplyTo($email->getReplyTo(), $email->getFromName());
91 if (!empty($email->getAdditionalMailHeader())) {
92 foreach ($email->getAdditionalMailHeader() as $name => $values) {
93 // Set the "Message-ID" header for PHP-Mailer directly
94 if (strtolower($name) === 'message-id') {
95 // implode all values to one entry, because there's only one value possible
96 $mailer->MessageID = trim(implode("", $values));
98 $mailer->addCustomHeader(trim($name), trim(implode("\n", $values)));
103 if ($mailer->send()) {
106 } catch (Exception $e) {
107 DI::logger()->error('PHPMailer error', ['email' => $email, 'ErrorInfo' => $mailer->ErrorInfo, 'exception' => $e]);