]> git.mxchange.org Git - friendica-addons.git/blob - phpmailer/phpmailer.php
71392e1c2c10cda7a08cae4d59a996995b14c6bf
[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 <http://mat.exon.name>
7  */
8
9 use Friendica\App;
10 use Friendica\Core\Addon;
11 use PHPMailer\PHPMailer\PHPMailer;
12 use PHPMailer\PHPMailer\Exception;
13
14 function phpmailer_install()
15 {
16         Addon::registerHook(
17                 'emailer_send_prepare',
18                 'addon/phpmailer/phpmailer.php',
19                 'phpmailer_emailer_send_prepare'
20         );
21 }
22
23 function phpmailer_uninstall()
24 {
25         Addon::unregisterHook(
26                 'emailer_send_prepare',
27                 'addon/phpmailer/phpmailer.php',
28                 'phpmailer_emailer_send_prepare'
29         );
30 }
31
32 function phpmailer_module()
33 {
34 }
35
36 /**
37  * @param App $a
38  * @param array $b
39  */
40 function phpmailer_emailer_send_prepare(App $a, array &$b)
41 {
42         require_once __DIR__ . '/phpmailer/src/PHPMailer.php';
43         require_once __DIR__ . '/phpmailer/src/SMTP.php';
44         require_once __DIR__ . '/phpmailer/src/Exception.php';
45
46         // Passing `true` enables exceptions
47         $mail = new PHPMailer(true);
48         try {
49                 if (!empty($a->config['system']['smtp']) && (bool)$a->config['system']['smtp'] === true) {
50                         // Set mailer to use SMTP
51                         $mail->isSMTP();
52                         /*
53                         // Enable verbose debug output
54                         $mail->SMTPDebug = 2;
55                         */
56                         // Specify main and backup SMTP servers
57                         $mail->Host = $a->config['system']['smtp_server'];
58                         $mail->Port = $a->config['system']['smtp_port'];
59
60                         if (!empty($a->config['system']['smtp_secure']) && (bool)$a->config['system']['smtp_secure'] !== '') {
61                                 $mail->SMTPSecure = $a->config['system']['smtp_secure'];
62                                 $mail->Port = $a->config['system']['smtp_port_s'];
63                         }
64
65                         if (!empty($a->config['system']['smtp_username']) && !empty($a->config['system']['smtp_password'])) {
66                                 $mail->SMTPAuth = true;
67                                 $mail->Username = $a->config['system']['smtp_username'];
68                                 $mail->Password = $a->config['system']['smtp_password'];
69                         }
70
71                         if (!empty($a->config['system']['smtp_from']) && !empty($a->config['system']['smtp_domain'])) {
72                                 $mail->setFrom($a->config['system']['smtp_from'], $a->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 }