Some more bots and false positives
[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.1
6  * Author: Marcus Mueller
7  */
8
9 use Friendica\App;
10 use Friendica\Core\Addon;
11 use Friendica\Core\Config;
12 use PHPMailer\PHPMailer\PHPMailer;
13 use PHPMailer\PHPMailer\Exception;
14
15 function phpmailer_install()
16 {
17         Addon::registerHook(
18                 'emailer_send_prepare',
19                 __FILE__,
20                 'phpmailer_emailer_send_prepare'
21         );
22 }
23
24 function phpmailer_uninstall()
25 {
26         Addon::unregisterHook(
27                 'emailer_send_prepare',
28                 __FILE__,
29                 'phpmailer_emailer_send_prepare'
30         );
31 }
32
33 /**
34  * @param App $a
35  * @param array $b
36  */
37 function phpmailer_emailer_send_prepare(App $a, array &$b)
38 {
39         require_once __DIR__ . '/phpmailer/src/PHPMailer.php';
40         require_once __DIR__ . '/phpmailer/src/SMTP.php';
41         require_once __DIR__ . '/phpmailer/src/Exception.php';
42
43         // Passing `true` enables exceptions
44         $mail = new PHPMailer(true);
45         try {
46                 if (Config::get('phpmailer', 'smtp')) {
47                         // Set mailer to use SMTP
48                         $mail->isSMTP();
49                         /*
50                         // Enable verbose debug output
51                         $mail->SMTPDebug = 2;
52                         */
53                         // Setup encoding.
54                         $mail->CharSet = 'UTF-8';
55                         $mail->Encoding = 'base64';
56                         // Specify main and backup SMTP servers
57                         $mail->Host = Config::get('phpmailer', 'smtp_server');
58                         $mail->Port = Config::get('phpmailer', 'smtp_port');
59
60                         if (Config::get('system', 'smtp_secure') && Config::get('phpmailer', 'smtp_port_s')) {
61                                 $mail->SMTPSecure = Config::get('phpmailer', 'smtp_secure');
62                                 $mail->Port = Config::get('phpmailer', 'smtp_port_s');
63                         }
64
65                         if (Config::get('phpmailer', 'smtp_username') && Config::get('phpmailer', 'smtp_password')) {
66                                 $mail->SMTPAuth = true;
67                                 $mail->Username = Config::get('phpmailer', 'smtp_username');
68                                 $mail->Password = Config::get('phpmailer', 'smtp_password');
69                         }
70
71                         if (Config::get('phpmailer', 'smtp_from')) {
72                                 $mail->setFrom(Config::get('phpmailer', 'smtp_from'), Config::get('config', 'sitename'));
73                         }
74                 }
75
76                 // subject
77                 $mail->Subject = $b['messageSubject'];
78
79                 // add text
80                 $mail->AltBody = $b['textVersion'];
81
82                 if (!empty($b['toEmail'])) {
83                         $mail->addAddress($b['toEmail']);
84                 }
85
86                 // html version
87                 if (!empty($b['htmlVersion'])) {
88                         $mail->isHTML(true);
89                         $mail->Body = $b['htmlVersion'];
90                 }
91
92                 /*
93                 // additional headers
94                 if (!empty($b['additionalMailHeader'])) {
95                         $mail->addCustomHeader($b['additionalMailHeader']);
96                 }
97                 */
98
99                 $mail->send();
100         } catch (Exception $e) {
101                 echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
102                 die();
103         }
104 }