Refactor dynamic App::getLogger() to static DI::logger()
[friendica-addons.git/.git] / phpmailer / phpmailer.php
1 <?php
2 /**
3  * Name: PHP Mailer SMTP
4  * Description: Connects to a SMTP server based on the config
5  * Version: 0.2
6  * Author: Marcus Mueller
7  * Maintainer: Hypolite Petovan <hypolite@friendica.mrpetovan.com>
8  */
9
10 use Friendica\App;
11 use Friendica\Core\Config;
12 use Friendica\Core\Hook;
13 use Friendica\DI;
14 use Friendica\Util\ConfigFileLoader;
15 use PHPMailer\PHPMailer\PHPMailer;
16 use PHPMailer\PHPMailer\Exception;
17
18 function phpmailer_install()
19 {
20         Hook::register('load_config'         , __FILE__, 'phpmailer_load_config');
21         Hook::register('emailer_send_prepare', __FILE__, 'phpmailer_emailer_send_prepare');
22 }
23
24 function phpmailer_load_config(App $a, ConfigFileLoader $loader)
25 {
26         $a->getConfigCache()->load($loader->loadAddonConfig('phpmailer'));
27 }
28
29 /**
30  * @param App $a
31  * @param array $b
32  */
33 function phpmailer_emailer_send_prepare(App $a, array &$b)
34 {
35         require_once __DIR__ . '/phpmailer/src/PHPMailer.php';
36         require_once __DIR__ . '/phpmailer/src/SMTP.php';
37         require_once __DIR__ . '/phpmailer/src/Exception.php';
38
39         // Passing `true` enables exceptions
40         $mail = new PHPMailer(true);
41         try {
42                 if (Config::get('phpmailer', 'smtp')) {
43                         // Set mailer to use SMTP
44                         $mail->isSMTP();
45
46                         // Setup encoding.
47                         $mail->CharSet = 'UTF-8';
48                         $mail->Encoding = 'base64';
49
50                         // Specify main and backup SMTP servers
51                         $mail->Host = Config::get('phpmailer', 'smtp_server');
52                         $mail->Port = Config::get('phpmailer', 'smtp_port');
53
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');
57                         }
58
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');
63                         }
64
65                         if (Config::get('phpmailer', 'smtp_from')) {
66                                 $mail->setFrom(Config::get('phpmailer', 'smtp_from'), $b['fromName']);
67                         }
68                 } else {
69                         $mail->setFrom($b['fromEmail'], $b['fromName']);
70                 }
71
72                 // subject
73                 $mail->Subject = $b['messageSubject'];
74
75                 if (!empty($b['toEmail'])) {
76                         $mail->addAddress($b['toEmail']);
77                 }
78
79                 // html version
80                 if (!empty($b['htmlVersion'])) {
81                         $mail->isHTML(true);
82                         $mail->Body = $b['htmlVersion'];
83                         $mail->AltBody = $b['textVersion'];
84                 } else {
85                         // add text
86                         $mail->Body = $b['textVersion'];
87                 }
88
89                 if (!empty($b['replyTo'])) {
90                         $mail->addReplyTo($b['replyTo'], $b['fromName']);
91                 }
92
93                 // additional headers
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));
98                         }
99                 }
100
101                 $b['sent'] = $mail->send();
102         } catch (Exception $e) {
103                 DI::logger()->error('PHPMailer error', ['ErrorInfo' => $mail->ErrorInfo, 'code' => $e->getCode(), 'message' => $e->getMessage()]);
104         }
105 }