/
class.mailer.php
209 lines (159 loc) · 5.05 KB
/
class.mailer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/**
* Mail class - mails with php mail()
*
* @author Marcel Folaron <marcel.folaron@gmail.com>
* @version 1.0
* @license GNU/GPL, see license.txt
*/
namespace leantime\core {
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
class mailer
{
/**
* @access public
* @var string
*/
public $cc;
/**
* @access public
* @var string
*/
public $bcc;
/**
* @access public
* @var string
*/
public $from = '';
/**
* @access public
* @var string
*/
public $text = '';
/**
* @access public
* @var string
*/
public $subject;
private $mailAgent;
private $emailDomain;
/**
* __construct - get configurations
*
* @access public
* @return void
*/
public function __construct()
{
$config = new config();
$this->from = $config->email;
//PHPMailer
$this->mailAgent = new PHPMailer();
//Use SMTP or php mail().
if($config->useSMTP === true) {
$this->mailAgent->SMTPDebug = 0; // Enable verbose debug output
$this->mailAgent->Timeout = 20;
$this->mailAgent->isSMTP(); // Set mailer to use SMTP
$this->mailAgent->Host = $config->smtpHosts; // Specify main and backup SMTP servers
$this->mailAgent->SMTPAuth = true; // Enable SMTP authentication
$this->mailAgent->Username = $config->smtpUsername; // SMTP username
$this->mailAgent->Password = $config->smtpPassword; // SMTP password
$this->mailAgent->SMTPSecure = $config->smtpSecure; // Enable TLS encryption, `ssl` also accepted
$this->mailAgent->Port = $config->smtpPort; // TCP port to connect to
}else{
$this->mailAgent->isMail();
}
$this->emailDomain = $config->email;
$this->logo = $config->logoPath;
$this->companyColor = $config->mainColor;
}
/**
*
* setText - sets the mailtext
*
* @access public
* @param $text
* @return void
*/
public function setText($text)
{
$this->text = $text;
}
/**
*
* setHTML - set Mail html (no function yet)
*
* @access public
* @param $html
* @return void
*/
public function setHtml($html)
{
$this->html = $html;
}
/**
* setSubject - set mail subject
*
* @access public
* @param $subject
* @return void
*/
public function setSubject($subject)
{
$this->subject = $subject;
}
/**
* sendMail - send the mail with mail()
*
* @access public
* @param array $to
* @param $from
* @return void
* @throws \phpmailerException
*/
public function sendMail(array $to, $from)
{
$this->mailAgent->isHTML(true); // Set email format to HTML
$this->mailAgent->setFrom($this->emailDomain, $from . " (Leantime)");
$this->mailAgent->Subject = $this->subject;
$bodyTemplate = '
<table width="100%" style="background:#eeeeee; padding:15px; ">
<tr>
<td align="center" valign="top">
<table width="600" style="width:600px; background-color:#ffffff; border:1px solid #ccc;">
<tr>
<td style="padding:3px 10px; background-color:#' . $this->companyColor . '">
<table>
<tr>
<td width="150"><img alt="Logo" src="' . $this->logo . '" width="150" style="width:150px;"></td>
<td></td>
</tr>
</table>
</td>
</tr>
<tr>
<td style="padding:10px; font-family:Arial; color:#666; font-size:14px; line-height:1.7;">
Hi,<br /><br />
' . nl2br($this->html) . '
<br /><br />
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="center">To <a href="https://'.$_SERVER['HTTP_HOST'].'/users/editOwn/">unsubscribe</a> from these types of notifications please login and edit your notifications settings</td>
</tr>
</table>';
$this->mailAgent->Body = $bodyTemplate;
$this->mailAgent->AltBody = $this->text;
$to = array_unique($to);
foreach ($to as $recip) {
$this->mailAgent->addAddress($recip);
$this->mailAgent->send();
$this->mailAgent->clearAllRecipients();
}
}
}
}