Blockem SV translation update THX Bjoessi
[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\Util\ConfigFileLoader;
14 use PHPMailer\PHPMailer\PHPMailer;
15 use PHPMailer\PHPMailer\Exception;
16
17 function phpmailer_install()
18 {
19         Hook::register('load_config'         , __FILE__, 'phpmailer_load_config');
20         Hook::register('emailer_send_prepare', __FILE__, 'phpmailer_emailer_send_prepare');
21 }
22
23 function phpmailer_load_config(App $a, ConfigFileLoader $loader)
24 {
25         $a->getConfigCache()->load($loader->loadAddonConfig('phpmailer'));
26 }
27
28 /**
29  * @param App $a
30  * @param array $b
31  */
32 function phpmailer_emailer_send_prepare(App $a, array &$b)
33 {
34         require_once __DIR__ . '/phpmailer/src/PHPMailer.php';
35         require_once __DIR__ . '/phpmailer/src/SMTP.php';
36         require_once __DIR__ . '/phpmailer/src/Exception.php';
37
38         // Passing `true` enables exceptions
39         $mail = new PHPMailer(true);
40         try {
41                 if (Config::get('phpmailer', 'smtp')) {
42                         // Set mailer to use SMTP
43                         $mail->isSMTP();
44
45                         // Setup encoding.
46                         $mail->CharSet = 'UTF-8';
47                         $mail->Encoding = 'base64';
48
49                         // Specify main and backup SMTP servers
50                         $mail->Host = Config::get('phpmailer', 'smtp_server');
51                         $mail->Port = Config::get('phpmailer', 'smtp_port');
52
53                         if (Config::get('system', 'smtp_secure') && Config::get('phpmailer', 'smtp_port_s')) {
54                                 $mail->SMTPSecure = Config::get('phpmailer', 'smtp_secure');
55                                 $mail->Port = Config::get('phpmailer', 'smtp_port_s');
56                         }
57
58                         if (Config::get('phpmailer', 'smtp_username') && Config::get('phpmailer', 'smtp_password')) {
59                                 $mail->SMTPAuth = true;
60                                 $mail->Username = Config::get('phpmailer', 'smtp_username');
61                                 $mail->Password = Config::get('phpmailer', 'smtp_password');
62                         }
63
64                         if (Config::get('phpmailer', 'smtp_from')) {
65                                 $mail->setFrom(Config::get('phpmailer', 'smtp_from'), $b['fromName']);
66                         }
67                 } else {
68                         $mail->setFrom($b['fromEmail'], $b['fromName']);
69                 }
70
71                 // subject
72                 $mail->Subject = $b['messageSubject'];
73
74                 if (!empty($b['toEmail'])) {
75                         $mail->addAddress($b['toEmail']);
76                 }
77
78                 // html version
79                 if (!empty($b['htmlVersion'])) {
80                         $mail->isHTML(true);
81                         $mail->Body = $b['htmlVersion'];
82                         $mail->AltBody = $b['textVersion'];
83                 } else {
84                         // add text
85                         $mail->Body = $b['textVersion'];
86                 }
87
88                 if (!empty($b['replyTo'])) {
89                         $mail->addReplyTo($b['replyTo'], $b['fromName']);
90                 }
91
92                 // additional headers
93                 if (!empty($b['additionalMailHeader'])) {
94                         foreach (explode("\n", trim($b['additionalMailHeader'])) as $header_line) {
95                                 list($name, $value) = explode(':', $header_line, 2);
96                                 $mail->addCustomHeader(trim($name), trim($value));
97                         }
98                 }
99
100                 $b['sent'] = $mail->send();
101         } catch (Exception $e) {
102                 $a->getLogger()->error('PHPMailer error', ['ErrorInfo' => $mail->ErrorInfo, 'code' => $e->getCode(), 'message' => $e->getMessage()]);
103         }
104 }