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