]> git.mxchange.org Git - friendica-addons.git/blob - phpmailer/phpmailer.php
57871f3270b640840bf4003cfa8da2e71ff9b187
[friendica-addons.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                         // Specify main and backup SMTP servers
54                         $mail->Host = Config::get('phpmailer', 'smtp_server');
55                         $mail->Port = Config::get('phpmailer', 'smtp_port');
56
57                         if (Config::get('system', 'smtp_secure') && Config::get('phpmailer', 'smtp_port_s')) {
58                                 $mail->SMTPSecure = Config::get('phpmailer', 'smtp_secure');
59                                 $mail->Port = Config::get('phpmailer', 'smtp_port_s');
60                         }
61
62                         if (Config::get('phpmailer', 'smtp_username') && Config::get('phpmailer', 'smtp_password')) {
63                                 $mail->SMTPAuth = true;
64                                 $mail->Username = Config::get('phpmailer', 'smtp_username');
65                                 $mail->Password = Config::get('phpmailer', 'smtp_password');
66                         }
67
68                         if (Config::get('phpmailer', 'smtp_from')) {
69                                 $mail->setFrom(Config::get('phpmailer', 'smtp_from'), Config::get('config', 'sitename'));
70                         }
71                 }
72
73                 // subject
74                 $mail->Subject = $b['messageSubject'];
75
76                 // add text
77                 $mail->AltBody = $b['textVersion'];
78
79                 if (!empty($b['toEmail'])) {
80                         $mail->addAddress($b['toEmail']);
81                 }
82
83                 // html version
84                 if (!empty($b['htmlVersion'])) {
85                         $mail->isHTML(true);
86                         $mail->Body = $b['htmlVersion'];
87                 }
88
89                 /*
90                 // additional headers
91                 if (!empty($b['additionalMailHeader'])) {
92                         $mail->addCustomHeader($b['additionalMailHeader']);
93                 }
94                 */
95
96                 $mail->send();
97         } catch (Exception $e) {
98                 echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
99                 die();
100         }
101 }