1 <?php
 2 
 3 /**
 4  * PHPMailer Addon.
 5  *
 6  * @author markus[dot]staab[at]redaxo[dot]de Markus Staab
 7  *
 8  * @package redaxo\phpmailer
 9  */
10 
11 use PHPMailer\PHPMailer\PHPMailer;
12 
13 class rex_mailer extends PHPMailer
14 {
15     private $log;
16 
17     public function __construct($exceptions = false)
18     {
19         $addon = rex_addon::get('phpmailer');
20         $this->setLanguage(rex_i18n::getLanguage(), $addon->getPath('vendor/phpmailer/phpmailer/language/'));
21         $this->XMailer = 'REXMailer';
22         $this->From = $addon->getConfig('from');
23         $this->FromName = $addon->getConfig('fromname');
24         $this->ConfirmReadingTo = $addon->getConfig('confirmto');
25         $this->Mailer = $addon->getConfig('mailer');
26         $this->Host = $addon->getConfig('host');
27         $this->Port = $addon->getConfig('port');
28         $this->CharSet = $addon->getConfig('charset');
29         $this->WordWrap = $addon->getConfig('wordwrap');
30         $this->Encoding = $addon->getConfig('encoding');
31         if ($addon->getConfig('priority') == 0) {
32             $this->Priority = null;
33         } else {
34             $this->Priority = $addon->getConfig('priority');
35         }
36         $this->SMTPDebug = $addon->getConfig('smtp_debug');
37         $this->SMTPSecure = $addon->getConfig('smtpsecure');
38         $this->SMTPAuth = $addon->getConfig('smtpauth');
39         $this->SMTPAutoTLS = $addon->getConfig('security_mode');
40         $this->Username = $addon->getConfig('username');
41         $this->Password = $addon->getConfig('password');
42 
43         if ($bcc = $addon->getConfig('bcc')) {
44             $this->addBCC($bcc);
45         }
46 
47         $this->log = $addon->getConfig('log');
48 
49         parent::__construct($exceptions);
50     }
51 
52     public function send()
53     {
54         return rex_timer::measure(__METHOD__, function () {
55             if ($this->log) {
56                 $this->log();
57             }
58             return parent::send();
59         });
60     }
61 
62     /*
63      * @param boolean $status
64      */
65     public function setLog($status)
66     {
67         $this->log = $status;
68     }
69 
70     private function log()
71     {
72         $content = '<!-- '.PHP_EOL.date('d.m.Y H:i:s').PHP_EOL;
73         $content .= 'From : '.$this->From.PHP_EOL;
74         $content .= 'To : '.implode(', ', array_column($this->getToAddresses(), 0)).PHP_EOL;
75         $content .= 'Subject : '.$this->Subject.PHP_EOL;
76         $content .= ' -->'.PHP_EOL;
77         $content .= $this->Body;
78 
79         $dir = self::logFolder().'/'.date('Y').'/'.date('m');
80 
81         $count = 1;
82         $logFile = $dir.'/'.date('Y-m-d_H_i_s').'.html';
83         while (file_exists($logFile)) {
84             $logFile = $dir.'/'.date('Y-m-d_H_i_s').'_'.(++$count).'.html';
85         }
86 
87         rex_file::put($logFile, $content);
88     }
89 
90     public static function logFolder()
91     {
92         return rex_path::addonData('phpmailer', 'mail_log');
93     }
94 }
95