1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16:
17: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
18:
19:
20:
21: require_once 'swiftmailer/lib/swift_init.php';
22:
23: 24: 25: 26: 27: 28:
29: class cMailer extends Swift_Mailer {
30:
31: 32: 33: 34: 35:
36: private $_mailHost = 'localhost';
37:
38: 39: 40: 41: 42:
43: private $_mailUser = '';
44:
45: 46: 47: 48: 49:
50: private $_mailPass = '';
51:
52: 53: 54: 55: 56:
57: private $_mailEncryption = NULL;
58:
59: 60: 61: 62: 63:
64: private $_mailPort = 25;
65:
66: 67: 68: 69: 70:
71: private $_mailSender = 'noreply@contenido.org';
72:
73: 74: 75: 76: 77:
78: private $_mailSenderName = 'CONTENIDO Backend';
79:
80: 81: 82: 83: 84: 85:
86: public function __construct($transport = NULL) {
87:
88: $mailSender = getSystemProperty('system', 'mail_sender');
89: if (Swift_Validate::email($mailSender)) {
90: $this->_mailSender = $mailSender;
91: }
92:
93:
94: $mailSenderName = getSystemProperty('system', 'mail_sender_name');
95: if (!empty($mailSenderName)) {
96: $this->_mailSenderName = $mailSenderName;
97: }
98:
99:
100: if (!is_null($transport)) {
101: parent::__construct($transport);
102: return;
103: }
104:
105:
106:
107:
108: $mailHost = getSystemProperty('system', 'mail_host');
109: if (!empty($mailHost)) {
110: $this->_mailHost = $mailHost;
111: }
112:
113:
114: $this->_mailUser = (getSystemProperty('system', 'mail_user')) ? getSystemProperty('system', 'mail_user') : '';
115: $this->_mailPass = (getSystemProperty('system', 'mail_pass')) ? getSystemProperty('system', 'mail_pass') : '';
116:
117:
118: $encryptions = array(
119: 'tls',
120: 'ssl'
121: );
122: $mail_encryption = strtolower(getSystemProperty('system', 'mail_encryption'));
123: if (in_array($mail_encryption, $encryptions)) {
124: $this->_mailEncryption = $mail_encryption;
125: } elseif ('1' == $mail_encryption) {
126: $this->_mailEncryption = 'ssl';
127: } else {
128: $this->_mailEncryption = 'tls';
129: }
130:
131:
132: if (is_numeric(getSystemProperty('system', 'mail_port'))) {
133: $this->_mailPort = (int) getSystemProperty('system', 'mail_port');
134: }
135:
136:
137: $transport = self::constructTransport($this->_mailHost, $this->_mailPort, $this->_mailEncryption, $this->_mailUser, $this->_mailPass);
138: parent::__construct($transport);
139: }
140:
141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158:
159: public static function constructTransport($mailHost, $mailPort, $mailEncryption = NULL, $mailUser = NULL, $mailPass = NULL) {
160:
161: $transport = Swift_SmtpTransport::newInstance($mailHost, $mailPort, $mailEncryption);
162: if (!empty($mailUser)) {
163: $authHandler = new Swift_Transport_Esmtp_AuthHandler(array(
164: new Swift_Transport_Esmtp_Auth_PlainAuthenticator(),
165: new Swift_Transport_Esmtp_Auth_LoginAuthenticator(),
166: new Swift_Transport_Esmtp_Auth_CramMd5Authenticator()
167: ));
168: $authHandler->setUsername($mailUser);
169: if (!empty($mailPass)) {
170: $authHandler->setPassword($mailPass);
171: }
172: $transport->setExtensionHandlers(array(
173: $authHandler
174: ));
175: }
176:
177:
178: try {
179: $transport->start();
180: } catch (Swift_TransportException $e) {
181:
182: $transport = Swift_MailTransport::newInstance();
183: }
184:
185: return $transport;
186: }
187:
188: 189: 190: 191: 192: 193: 194:
195: public function setCharset($charset) {
196: Swift_Preferences::getInstance()->setCharset($charset);
197: }
198:
199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224:
225: public function sendMail($from, $to, $subject, $body = '', $cc = NULL, $bcc = NULL, $replyTo = NULL, $resend = false, $contentType = 'text/plain') {
226: $message = Swift_Message::newInstance($subject, $body, $contentType);
227: if (empty($from) || is_array($from) && count($from) > 1) {
228: $message->setFrom(array(
229: $this->_mailSender => $this->_mailSenderName
230: ));
231: } else {
232: $message->setFrom($from);
233: }
234: $message->setTo($to);
235: $message->setCc($cc);
236: $message->setBcc($bcc);
237: $message->setReplyTo($replyTo);
238: $failedRecipients = array();
239:
240: return $this->send($message, $failedRecipients, $resend);
241: }
242:
243: 244: 245: 246: 247: 248: 249: 250: 251:
252: public function send(Swift_Mime_Message $message, &$failedRecipients = NULL, $resend = false) {
253: if (!is_array($failedRecipients)) {
254: $failedRecipients = array();
255: }
256: $result = parent::send($message, $failedRecipients);
257:
258:
259: if (!$resend) {
260: $this->_logMail($message, $failedRecipients);
261: }
262:
263: return $result;
264: }
265:
266: 267: 268: 269: 270: 271: 272: 273:
274: public function resendMail($idmailsuccess) {
275: $mailLogSuccess = new cApiMailLogSuccess($idmailsuccess);
276: if (!$mailLogSuccess->isLoaded() || $mailLogSuccess->get('success') == 1) {
277: throw new cInvalidArgumentException('The mail which should be resent has already been sent successfully or does not exist.');
278: }
279:
280:
281: $idmail = $mailLogSuccess->get('idmail');
282: $mailLog = new cApiMailLog($idmail);
283: $from = json_decode($mailLog->get('from'), true);
284: $to = json_decode($mailLog->get('to'), true);
285: $replyTo = json_decode($mailLog->get('reply_to'), true);
286: $cc = json_decode($mailLog->get('cc'), true);
287: $bcc = json_decode($mailLog->get('bcc'), true);
288: $subject = $mailLog->get('subject');
289: $body = $mailLog->get('body');
290: $contentType = $mailLog->get('content_type');
291: $this->setCharset($mailLog->get('charset'));
292:
293:
294: $charset = $mailLog->get('charset');
295: $from = $this->decodeField($from, $charset);
296: $to = $this->decodeField($to, $charset);
297: $replyTo = $this->decodeField($replyTo, $charset);
298: $cc = $this->decodeField($cc, $charset);
299: $bcc = $this->decodeField($bcc, $charset);
300: $subject = $this->decodeField($subject, $charset);
301: $body = $this->decodeField($body, $charset);
302:
303: $success = $this->sendMail($from, $to, $subject, $body, $cc, $bcc, $replyTo, true, $contentType);
304:
305: if ($success) {
306: $mailLogSuccess->set('success', 1);
307: $mailLogSuccess->store();
308: }
309: }
310:
311: 312: 313: 314: 315: 316: 317: 318: 319: 320:
321: private function encodeField($value, $charset) {
322: if (is_array($value)) {
323: for ($i = 0; $i < count($value); $i++) {
324: if (!empty($value[$i])) {
325: $value[$i] = conHtmlEntities($value[$i], ENT_COMPAT, $charset, false);
326: }
327: }
328: return $value;
329: } else if (is_string($value)) {
330: return conHtmlentities($value, ENT_COMPAT, $charset, false);
331: }
332: return $value;
333: }
334:
335: 336: 337: 338: 339: 340: 341: 342: 343: 344:
345: private function decodeField($value, $charset) {
346: if (is_array($value)) {
347: for ($i = 0; $i < count($value); $i++) {
348: if (!empty($value[$i])) {
349: $value[$i] = conHtmlEntityDecode($value[$i], ENT_COMPAT | ENT_HTML401, $charset, false);
350: }
351: }
352: } else if (is_string($value)) {
353: return conHtmlEntityDecode($value, ENT_COMPAT | ENT_HTML401, $charset);
354: }
355: return $value;
356: }
357:
358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368:
369: private function _logMail(Swift_Mime_Message $message, array $failedRecipients = array()) {
370:
371:
372: $mail_log = getSystemProperty('system', 'mail_log');
373: if ($mail_log == 'false') {
374: return false;
375: }
376:
377: $mailLogCollection = new cApiMailLogCollection();
378:
379:
380: $charset = $message->getCharset();
381: $from = $this->encodeField($message->getFrom(), $charset);
382: $to = $this->encodeField($message->getTo(), $charset);
383: $replyTo = $this->encodeField($message->getReplyTo(), $charset);
384: $cc = $this->encodeField($message->getCc(), $charset);
385: $bcc = $this->encodeField($message->getBcc(), $charset);
386: $subject = $this->encodeField($message->getSubject(), $charset);
387: $body = $this->encodeField($message->getBody(), $charset);
388: $contentType = $message->getContentType();
389: $mailItem = $mailLogCollection->create($from, $to, $replyTo, $cc, $bcc, $subject, $body, time(), $charset, $contentType);
390:
391:
392: $idmail = $mailItem->get('idmail');
393:
394:
395:
396: $recipientArrays = array(
397: $message->getTo(),
398: $message->getCc(),
399: $message->getBcc()
400: );
401: $mailLogSuccessCollection = new cApiMailLogSuccessCollection();
402: foreach ($recipientArrays as $recipients) {
403: if (!is_array($recipients)) {
404: continue;
405: }
406: foreach ($recipients as $key => $value) {
407: $recipient = array(
408: $key => $value
409: );
410: $success = true;
411:
412:
413:
414: $exception = '';
415: if (in_array($key, $failedRecipients)) {
416: $success = false;
417: }
418: $mailLogSuccessCollection->create($idmail, $recipient, $success, $exception);
419: }
420: }
421:
422: return $idmail;
423: }
424:
425: }
426: