]> git.mxchange.org Git - friendica-addons.git/blob - phpmailer/phpmailer.php
Fix white spaces
[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\Core\Config\Util\ConfigFileManager;
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', 5);
24 }
25
26 function phpmailer_load_config(ConfigFileManager $loader)
27 {
28         DI::app()->getConfigCache()->load($loader->loadAddonConfig('phpmailer'), \Friendica\Core\Config\ValueObject\Cache::SOURCE_STATIC);
29 }
30
31 /**
32  * @param IEmail $email
33  */
34 function phpmailer_emailer_send_prepare(IEmail &$email)
35 {
36         // Passing `true` enables exceptions
37         $mailer = new PHPMailer(true);
38         try {
39                 // Setup encoding.
40                 $mailer->CharSet  = 'UTF-8';
41                 $mailer->Encoding = 'base64';
42
43                 if (DI::config()->get('phpmailer', 'smtp')) {
44                         // Set mailer to use SMTP
45                         $mailer->isSMTP();
46
47                         // Specify main and backup SMTP servers
48                         $mailer->Host = DI::config()->get('phpmailer', 'smtp_server');
49                         $mailer->Port = DI::config()->get('phpmailer', 'smtp_port');
50
51                         if (DI::config()->get('system', 'smtp_secure') && DI::config()->get('phpmailer', 'smtp_port_s')) {
52                                 $mailer->SMTPSecure = DI::config()->get('phpmailer', 'smtp_secure');
53                                 $mailer->Port       = DI::config()->get('phpmailer', 'smtp_port_s');
54                         }
55
56                         if (DI::config()->get('phpmailer', 'smtp_username') && DI::config()->get('phpmailer', 'smtp_password')) {
57                                 $mailer->SMTPAuth = true;
58                                 $mailer->Username = DI::config()->get('phpmailer', 'smtp_username');
59                                 $mailer->Password = DI::config()->get('phpmailer', 'smtp_password');
60                         }
61
62                         if (DI::config()->get('phpmailer', 'smtp_from')) {
63                                 $mailer->setFrom(DI::config()->get('phpmailer', 'smtp_from'), $email->getFromName());
64                         }
65                 } else {
66                         $mailer->setFrom($email->getFromAddress(), $email->getFromName());
67                 }
68
69                 // subject
70                 $mailer->Subject = $email->getSubject();
71
72                 if (!empty($email->getToAddress())) {
73                         $mailer->addAddress($email->getToAddress());
74                 }
75
76                 // html version
77                 if (!empty($email->getMessage())) {
78                         $mailer->isHTML(true);
79                         $mailer->Body    = $email->getMessage();
80                         $mailer->AltBody = $email->getMessage(true);
81                 } else {
82                         // add text
83                         $mailer->Body = $email->getMessage(true);
84                 }
85
86                 if (!empty($email->getReplyTo())) {
87                         $mailer->addReplyTo($email->getReplyTo(), $email->getFromName());
88                 }
89
90                 // additional headers
91                 if (!empty($email->getAdditionalMailHeader())) {
92                         foreach ($email->getAdditionalMailHeader() as $name => $values) {
93                                 // Set the "Message-ID" header for PHP-Mailer directly
94                                 if (strtolower($name) === 'message-id') {
95                                         // implode all values to one entry, because there's only one value possible
96                                         $mailer->MessageID = trim(implode("", $values));
97                                 } else {
98                                         $mailer->addCustomHeader(trim($name), trim(implode("\n", $values)));
99                                 }
100                         }
101                 }
102
103                 if ($mailer->send()) {
104                         $email = null;
105                 }
106         } catch (Exception $e) {
107                 DI::logger()->error('PHPMailer error', ['email' => $email, 'ErrorInfo' => $mailer->ErrorInfo, 'exception' => $e]);
108         }
109 }