Overview

Packages

  • CONTENIDO
  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentRssCreator
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
    • ScriptCookieDirective
  • mpAutoloaderClassMap
  • None
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SearchSolr
    • SmartyWrapper
    • UrlShortener
    • UserForum
    • Workflow
  • PluginManager
  • Setup
    • Form
    • GUI
    • Helper
      • Environment
      • Filesystem
      • MySQL
      • PHP
    • UpgradeJob
  • Smarty
    • Cacher
    • Compiler
    • Config
    • Debug
    • PluginsBlock
    • PluginsFilter
    • PluginsFunction
    • PluginsInternal
    • PluginsModifier
    • PluginsModifierCompiler
    • PluginsShared
    • Security
    • Template
    • TemplateResources
  • Swift
    • ByteStream
    • CharacterStream
    • Encoder
    • Events
    • KeyCache
    • Mailer
    • Mime
    • Plugins
    • Transport

Classes

  • Swift_FailoverTransport
  • Swift_LoadBalancedTransport
  • Swift_MailTransport
  • Swift_Plugins_Loggers_ArrayLogger
  • Swift_Plugins_Loggers_EchoLogger
  • Swift_SendmailTransport
  • Swift_SmtpTransport
  • Swift_Transport_AbstractSmtpTransport
  • Swift_Transport_Esmtp_Auth_CramMd5Authenticator
  • Swift_Transport_Esmtp_Auth_LoginAuthenticator
  • Swift_Transport_Esmtp_Auth_PlainAuthenticator
  • Swift_Transport_Esmtp_AuthHandler
  • Swift_Transport_EsmtpTransport
  • Swift_Transport_FailoverTransport
  • Swift_Transport_LoadBalancedTransport
  • Swift_Transport_MailTransport
  • Swift_Transport_SendmailTransport
  • Swift_Transport_SimpleMailInvoker
  • Swift_Transport_StreamBuffer

Interfaces

  • Swift_Plugins_Logger
  • Swift_Plugins_Pop_Pop3Exception
  • Swift_Transport
  • Swift_Transport_Esmtp_Authenticator
  • Swift_Transport_EsmtpHandler
  • Swift_Transport_IoBuffer
  • Swift_Transport_MailInvoker
  • Swift_Transport_SmtpAgent
  • Swift_TransportException
  • Overview
  • Package
  • Function
  • Todo
  • Download
  1: <?php
  2: /**
  3:  * This file contains the cMail class which should be used for all mail sending
  4:  * purposes.
  5:  *
  6:  * @package Core
  7:  * @subpackage Backend
  8:  * @version SVN Revision $Rev:$
  9:  *
 10:  * @author Rusmir Jusufovic, Simon Sprankel
 11:  * @copyright four for business AG <www.4fb.de>
 12:  * @license http://www.contenido.org/license/LIZENZ.txt
 13:  * @link http://www.4fb.de
 14:  * @link http://www.contenido.org
 15:  */
 16: 
 17: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 18: 
 19: // since CONTENIDO has it's own autoloader, swift_init.php is enough
 20: // we do not need and should not use swift_required.php!
 21: require_once 'swiftmailer/lib/swift_init.php';
 22: 
 23: /**
 24:  * Mailer class which should be used for all mail sending purposes.
 25:  *
 26:  * @package Core
 27:  * @subpackage Backend
 28:  */
 29: class cMailer extends Swift_Mailer {
 30: 
 31:     /**
 32:      * The mail host name
 33:      *
 34:      * @var string
 35:      */
 36:     private $_mailHost = 'localhost';
 37: 
 38:     /**
 39:      * The username for authentication at the host
 40:      *
 41:      * @var string
 42:      */
 43:     private $_mailUser = '';
 44: 
 45:     /**
 46:      * The password for authentication at the host
 47:      *
 48:      * @var string
 49:      */
 50:     private $_mailPass = '';
 51: 
 52:     /**
 53:      * The encryption method (ssl/tls).
 54:      *
 55:      * @var string
 56:      */
 57:     private $_mailEncryption = NULL;
 58: 
 59:     /**
 60:      * The port to use at the host
 61:      *
 62:      * @var int
 63:      */
 64:     private $_mailPort = 25;
 65: 
 66:     /**
 67:      * The mail address of the sender
 68:      *
 69:      * @var string
 70:      */
 71:     private $_mailSender = 'noreply@contenido.org';
 72: 
 73:     /**
 74:      * The name of the sender
 75:      *
 76:      * @var string
 77:      */
 78:     private $_mailSenderName = 'CONTENIDO Backend';
 79: 
 80:     /**
 81:      * Constructor
 82:      *
 83:      * @param Swift_Transport $transport [optional] the transport type
 84:      */
 85:     public function __construct($transport = NULL) {
 86:         // get sender mail from system properties
 87:         $mailSender = getSystemProperty('system', 'mail_sender');
 88:         if (Swift_Validate::email($mailSender)) {
 89:             $this->_mailSender = $mailSender;
 90:         }
 91: 
 92:         // get sender name from system properties
 93:         $mailSenderName = getSystemProperty('system', 'mail_sender_name');
 94:         if (!empty($mailSenderName)) {
 95:             $this->_mailSenderName = $mailSenderName;
 96:         }
 97: 
 98:         // if a transport object has been given, use it
 99:         if (!is_null($transport)) {
100:             parent::__construct($transport);
101:             return;
102:         }
103: 
104:         // if no transport object has been given, read system setting and create
105:         // one
106:         // get mailserver host from system properties
107:         $mailHost = getSystemProperty('system', 'mail_host');
108:         if (!empty($mailHost)) {
109:             $this->_mailHost = $mailHost;
110:         }
111: 
112:         // get mailserver user and pass from system properties
113:         $this->_mailUser = (getSystemProperty('system', 'mail_user')) ? getSystemProperty('system', 'mail_user') : '';
114:         $this->_mailPass = (getSystemProperty('system', 'mail_pass')) ? getSystemProperty('system', 'mail_pass') : '';
115: 
116:         // get mailserver encryption from system properties
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:         // get mailserver port from system properties
131:         if (is_numeric(getSystemProperty('system', 'mail_port'))) {
132:             $this->_mailPort = (int) getSystemProperty('system', 'mail_port');
133:         }
134: 
135:         // try to use SMTP
136:         $transport = self::constructTransport($this->_mailHost, $this->_mailPort, $this->_mailEncryption, $this->_mailUser, $this->_mailPass);
137:         parent::__construct($transport);
138:     }
139: 
140:     /**
141:      * Tries to establish an SMTP connection with the given settings.
142:      * If this is possible, a Swift_SmtpTransport object is returned. Otherwise
143:      * a simple Swift_MailTransport object is returned.
144:      *
145:      * @param string $mailHost the mail host
146:      * @param string $mailPort the mail port
147:      * @param string $mailEncryption [optional] the mail encryption
148:      * @param string $mailUser [optional] the mail user
149:      * @param string $mailPass [optional] the mail password
150:      * @return Swift_SmtpTransport Swift_MailTransport the transport object
151:      */
152:     public static function constructTransport($mailHost, $mailPort, $mailEncryption = NULL, $mailUser = NULL, $mailPass = NULL) {
153:         // try to use SMTP
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:         // check if SMTP usage is possible
171:         try {
172:             $transport->start();
173:         } catch (Swift_TransportException $e) {
174:             // if SMTP is not possible, simply use PHP's mail() functionality
175:             $transport = Swift_MailTransport::newInstance();
176:         }
177: 
178:         return $transport;
179:     }
180: 
181:     /**
182:      * Sets the charset of the messages which are sent with this mailer object.
183:      * If you want to use UTF-8, you do not need to call this method.
184:      *
185:      * @param string $charset the character encoding
186:      */
187:     public function setCharset($charset) {
188:         Swift_Preferences::getInstance()->setCharset($charset);
189:     }
190: 
191:     /**
192:      * Wrapper function for sending a mail.
193:      * All parameters which accept mail addresses also accept an array where
194:      * the key is the email address and the value is the name.
195:      *
196:      * @param string|array $from the sender of the mail, if something "empty" is
197:      *        given, default address from CONTENIDO system settings is used
198:      * @param string|array $to one or more recipient addresses
199:      * @param string $subject the subject of the mail
200:      * @param string $body [optional] the body of the mail
201:      * @param string|array $cc [optional] one or more recipient addresses which
202:      *        should get a normal copy
203:      * @param string|array $bcc [optional] one or more recipient addresses which
204:      *        should get a blind copy
205:      * @param string|array $replyTo [optional] address to which replies should
206:      *        be sent
207:      * @param bool $resend [optional] whether the mail is resent
208:      * @param string $contentType
209:      * @return int number of recipients to which the mail has been sent
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:      * Sends the given Swift_Mime_Message and logs it if $resend is false.
231:      *
232:      * @see Swift_Mailer::send()
233:      * @param Swift_Mime_Message $message
234:      * @param array &$failedRecipients, optional
235:      * @param bool $resend, optional
236:      * @return int
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:         // log the mail only if it is a new one
245:         if (!$resend) {
246:             $this->_logMail($message, $failedRecipients);
247:         }
248: 
249:         return $result;
250:     }
251: 
252:     /**
253:      * Resends the mail with the given idmailsuccess.
254:      *
255:      * @param int $idmailsuccess the ID of the mail which should be resend
256:      * @throws cInvalidArgumentException if the mail has already been sent
257:      *         successfully or does not exist
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:         // get all fields, json-decode address fields
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:         // decode all fields
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:      * Encodes the given value / the given array values with htmlentities.
298:      *
299:      * @param string|array $value the value to encode
300:      * @param string $charset the charset to use in htmlentities
301:      * @return string array encoded value
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:      * Decodes the given value / the given array values with html_entity_decode.
319:      *
320:      * @param string|array $value the value to decode
321:      * @param string $charset the charset to use in htmlentities
322:      * @return string array decoded value
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:      * Log the information about sending the email.
339:      *
340:      * @param Swift_Message $message the message which has been send
341:      * @param array $failedRecipients [optional] the recipient addresses that
342:      *        did not get the mail
343:      * @return string the idmail of the inserted table row in con_mail_log
344:      */
345:     private function _logMail(Swift_Mime_Message $message, array $failedRecipients = array()) {
346:         $mailLogCollection = new cApiMailLogCollection();
347: 
348:         // encode all fields
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:         // get idmail variable
361:         $idmail = $mailItem->get('idmail');
362: 
363:         // do not use array_merge here since the mail addresses are array keys
364:         // array_merge will make problems if one recipient is e.g. in cc and bcc
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:                 // TODO how do we get the information why message sending
381:                 // has
382:                 // failed?
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: 
CMS CONTENIDO 4.9.7 API documentation generated by ApiGen