]> git.mxchange.org Git - friendica-addons.git/blob - phpmailer/phpmailer.php
Bump phpmailer/phpmailer from 6.1.4 to 6.1.6 in /phpmailer
[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.2
6  * Author: Marcus Mueller
7  * Maintainer: Hypolite Petovan <hypolite@friendica.mrpetovan.com>
8  */
9
10 use Friendica\App;
11 use Friendica\Core\Hook;
12 use Friendica\DI;
13 use Friendica\Object\EMail\IEmail;
14 use Friendica\Util\ConfigFileLoader;
15 use PHPMailer\PHPMailer\PHPMailer;
16 use PHPMailer\PHPMailer\Exception;
17
18 require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
19
20 function phpmailer_install()
21 {
22         Hook::register('load_config'         , __FILE__, 'phpmailer_load_config');
23         Hook::register('emailer_send_prepare', __FILE__, 'phpmailer_emailer_send_prepare');
24 }
25
26 function phpmailer_load_config(App $a, ConfigFileLoader $loader)
27 {
28         $a->getConfigCache()->load($loader->loadAddonConfig('phpmailer'));
29 }
30
31 /**
32  * @param App $a
33  * @param IEmail $email
34  */
35 function phpmailer_emailer_send_prepare(App $a, IEmail &$email)
36 {
37         // Passing `true` enables exceptions
38         $mailer = new PHPMailer(true);
39         try {
40                 if (DI::config()->get('phpmailer', 'smtp')) {
41                         // Set mailer to use SMTP
42                         $mailer->isSMTP();
43
44                         // Setup encoding.
45                         $mailer->CharSet  = 'UTF-8';
46                         $mailer->Encoding = 'base64';
47
48                         // Specify main and backup SMTP servers
49                         $mailer->Host = DI::config()->get('phpmailer', 'smtp_server');
50                         $mailer->Port = DI::config()->get('phpmailer', 'smtp_port');
51
52                         if (DI::config()->get('system', 'smtp_secure') && DI::config()->get('phpmailer', 'smtp_port_s')) {
53                                 $mailer->SMTPSecure = DI::config()->get('phpmailer', 'smtp_secure');
54                                 $mailer->Port       = DI::config()->get('phpmailer', 'smtp_port_s');
55                         }
56
57                         if (DI::config()->get('phpmailer', 'smtp_username') && DI::config()->get('phpmailer', 'smtp_password')) {
58                                 $mailer->SMTPAuth = true;
59                                 $mailer->Username = DI::config()->get('phpmailer', 'smtp_username');
60                                 $mailer->Password = DI::config()->get('phpmailer', 'smtp_password');
61                         }
62
63                         if (DI::config()->get('phpmailer', 'smtp_from')) {
64                                 $mailer->setFrom(DI::config()->get('phpmailer', 'smtp_from'), $email->getFromName());
65                         }
66                 } else {
67                         $mailer->setFrom($email->getFromAddress(), $email->getFromName());
68                 }
69
70                 // subject
71                 $mailer->Subject = $email->getSubject();
72
73                 if (!empty($email->getToAddress())) {
74                         $mailer->addAddress($email->getToAddress());
75                 }
76
77                 // html version
78                 if (!empty($email->getMessage())) {
79                         $mailer->isHTML(true);
80                         $mailer->Body    = $email->getMessage();
81                         $mailer->AltBody = $email->getMessage(true);
82                 } else {
83                         // add text
84                         $mailer->Body = $email->getMessage(true);
85                 }
86
87                 if (!empty($email->getReplyTo())) {
88                         $mailer->addReplyTo($email->getReplyTo(), $email->getFromName());
89                 }
90
91                 // additional headers
92                 if (!empty($email->getAdditionalMailHeader())) {
93                         foreach (explode("\n", trim($email->getAdditionalMailHeader())) as $header_line) {
94                                 list($name, $value) = explode(':', $header_line, 2);
95                                 $mailer->addCustomHeader(trim($name), trim($value));
96                         }
97                 }
98
99                 if ($mailer->send()) {
100                         $email = null;
101                 }
102         } catch (Exception $e) {
103                 DI::logger()->error('PHPMailer error', ['email' => $email, 'ErrorInfo' => $mailer->ErrorInfo, 'exception' => $e]);
104         }
105 }