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: public function __construct($transport = NULL) {
86:
87: $mailSender = getSystemProperty('system', 'mail_sender');
88: if (Swift_Validate::email($mailSender)) {
89: $this->_mailSender = $mailSender;
90: }
91:
92:
93: $mailSenderName = getSystemProperty('system', 'mail_sender_name');
94: if (!empty($mailSenderName)) {
95: $this->_mailSenderName = $mailSenderName;
96: }
97:
98:
99: if (!is_null($transport)) {
100: parent::__construct($transport);
101: return;
102: }
103:
104:
105:
106:
107: $mailHost = getSystemProperty('system', 'mail_host');
108: if (!empty($mailHost)) {
109: $this->_mailHost = $mailHost;
110: }
111:
112:
113: $this->_mailUser = (getSystemProperty('system', 'mail_user')) ? getSystemProperty('system', 'mail_user') : '';
114: $this->_mailPass = (getSystemProperty('system', 'mail_pass')) ? getSystemProperty('system', 'mail_pass') : '';
115:
116:
117: $encryptions = array(
118: 'tls',
119: 'ssl'
120: );
121: $mail_encryption = strtolower(getSystemProperty('system', 'mail_encryption'));
122: if (in_array($mail_encryption, $encryptions)) {
123: $this->_mailEncryption = $mail_encryption;
124: } elseif ('1' == $mail_encryption) {
125: $this->_mailEncryption = 'ssl';
126: } else {
127: $this->_mailEncryption = 'tls';
128: }
129:
130:
131: if (is_numeric(getSystemProperty('system', 'mail_port'))) {
132: $this->_mailPort = (int) getSystemProperty('system', 'mail_port');
133: }
134:
135:
136: $transport = self::constructTransport($this->_mailHost, $this->_mailPort, $this->_mailEncryption, $this->_mailUser, $this->_mailPass);
137: parent::__construct($transport);
138: }
139:
140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151:
152: public static function constructTransport($mailHost, $mailPort, $mailEncryption = NULL, $mailUser = NULL, $mailPass = NULL) {
153:
154: $transport = Swift_SmtpTransport::newInstance($mailHost, $mailPort, $mailEncryption);
155: if (!empty($mailUser)) {
156: $authHandler = new Swift_Transport_Esmtp_AuthHandler(array(
157: new Swift_Transport_Esmtp_Auth_PlainAuthenticator(),
158: new Swift_Transport_Esmtp_Auth_LoginAuthenticator(),
159: new Swift_Transport_Esmtp_Auth_CramMd5Authenticator()
160: ));
161: $authHandler->setUsername($mailUser);
162: if (!empty($mailPass)) {
163: $authHandler->setPassword($mailPass);
164: }
165: $transport->setExtensionHandlers(array(
166: $authHandler
167: ));
168: }
169:
170:
171: try {
172: $transport->start();
173: } catch (Swift_TransportException $e) {
174:
175: $transport = Swift_MailTransport::newInstance();
176: }
177:
178: return $transport;
179: }
180:
181: 182: 183: 184: 185: 186:
187: public function setCharset($charset) {
188: Swift_Preferences::getInstance()->setCharset($charset);
189: }
190:
191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210:
211: public function sendMail($from, $to, $subject, $body = '', $cc = NULL, $bcc = NULL, $replyTo = NULL, $resend = false, $contentType = 'text/plain') {
212: $message = Swift_Message::newInstance($subject, $body, $contentType);
213: if (empty($from) || is_array($from) && count($from) > 1) {
214: $message->setFrom(array(
215: $this->_mailSender => $this->_mailSenderName
216: ));
217: } else {
218: $message->setFrom($from);
219: }
220: $message->setTo($to);
221: $message->setCc($cc);
222: $message->setBcc($bcc);
223: $message->setReplyTo($replyTo);
224: $failedRecipients = array();
225:
226: return $this->send($message, $failedRecipients, $resend);
227: }
228:
229: 230: 231: 232: 233: 234: 235: 236: 237:
238: public function send(Swift_Mime_Message $message, &$failedRecipients = NULL, $resend = false) {
239: if (!is_array($failedRecipients)) {
240: $failedRecipients = array();
241: }
242: $result = parent::send($message, $failedRecipients);
243:
244:
245: if (!$resend) {
246: $this->_logMail($message, $failedRecipients);
247: }
248:
249: return $result;
250: }
251:
252: 253: 254: 255: 256: 257: 258:
259: public function resendMail($idmailsuccess) {
260: $mailLogSuccess = new cApiMailLogSuccess($idmailsuccess);
261: if (!$mailLogSuccess->isLoaded() || $mailLogSuccess->get('success') == 1) {
262: throw new cInvalidArgumentException('The mail which should be resent has already been sent successfully or does not exist.');
263: }
264:
265:
266: $idmail = $mailLogSuccess->get('idmail');
267: $mailLog = new cApiMailLog($idmail);
268: $from = json_decode($mailLog->get('from'), true);
269: $to = json_decode($mailLog->get('to'), true);
270: $replyTo = json_decode($mailLog->get('reply_to'), true);
271: $cc = json_decode($mailLog->get('cc'), true);
272: $bcc = json_decode($mailLog->get('bcc'), true);
273: $subject = $mailLog->get('subject');
274: $body = $mailLog->get('body');
275: $contentType = $mailLog->get('content_type');
276: $this->setCharset($mailLog->get('charset'));
277:
278:
279: $charset = $mailLog->get('charset');
280: $from = $this->decodeField($from, $charset);
281: $to = $this->decodeField($to, $charset);
282: $replyTo = $this->decodeField($replyTo, $charset);
283: $cc = $this->decodeField($cc, $charset);
284: $bcc = $this->decodeField($bcc, $charset);
285: $subject = $this->decodeField($subject, $charset);
286: $body = $this->decodeField($body, $charset);
287:
288: $success = $this->sendMail($from, $to, $subject, $body, $cc, $bcc, $replyTo, true, $contentType);
289:
290: if ($success) {
291: $mailLogSuccess->set('success', 1);
292: $mailLogSuccess->store();
293: }
294: }
295:
296: 297: 298: 299: 300: 301: 302:
303: private function encodeField($value, $charset) {
304: if (is_array($value)) {
305: for ($i = 0; $i < count($value); $i++) {
306: if (!empty($value[$i])) {
307: $value[$i] = conHtmlEntities($value[$i], ENT_COMPAT, $charset, false);
308: }
309: }
310: return $value;
311: } else if (is_string($value)) {
312: return conHtmlentities($value, ENT_COMPAT, $charset, false);
313: }
314: return $value;
315: }
316:
317: 318: 319: 320: 321: 322: 323:
324: private function decodeField($value, $charset) {
325: if (is_array($value)) {
326: for ($i = 0; $i < count($value); $i++) {
327: if (!empty($value[$i])) {
328: $value[$i] = conHtmlEntityDecode($value[$i], ENT_COMPAT | ENT_HTML401, $charset, false);
329: }
330: }
331: } else if (is_string($value)) {
332: return conHtmlEntityDecode($value, ENT_COMPAT | ENT_HTML401, $charset);
333: }
334: return $value;
335: }
336:
337: 338: 339: 340: 341: 342: 343: 344:
345: private function _logMail(Swift_Mime_Message $message, array $failedRecipients = array()) {
346: $mailLogCollection = new cApiMailLogCollection();
347:
348:
349: $charset = $message->getCharset();
350: $from = $this->encodeField($message->getFrom(), $charset);
351: $to = $this->encodeField($message->getTo(), $charset);
352: $replyTo = $this->encodeField($message->getReplyTo(), $charset);
353: $cc = $this->encodeField($message->getCc(), $charset);
354: $bcc = $this->encodeField($message->getBcc(), $charset);
355: $subject = $this->encodeField($message->getSubject(), $charset);
356: $body = $this->encodeField($message->getBody(), $charset);
357: $contentType = $message->getContentType();
358: $mailItem = $mailLogCollection->create($from, $to, $replyTo, $cc, $bcc, $subject, $body, time(), $charset, $contentType);
359:
360:
361: $idmail = $mailItem->get('idmail');
362:
363:
364:
365: $recipientArrays = array(
366: $message->getTo(),
367: $message->getCc(),
368: $message->getBcc()
369: );
370: $mailLogSuccessCollection = new cApiMailLogSuccessCollection();
371: foreach ($recipientArrays as $recipients) {
372: if (!is_array($recipients)) {
373: continue;
374: }
375: foreach ($recipients as $key => $value) {
376: $recipient = array(
377: $key => $value
378: );
379: $success = true;
380:
381:
382:
383: $exception = '';
384: if (in_array($key, $failedRecipients)) {
385: $success = false;
386: }
387: $mailLogSuccessCollection->create($idmail, $recipient, $success, $exception);
388: }
389: }
390:
391: return $idmail;
392: }
393:
394: }
395: