/**
  * Test that addresses can be taken back out of the list once added.
  */
 public function testAddressesCanBeRemoved()
 {
     $list = new Swift_RecipientList();
     $list->addBcc(new Swift_Address("*****@*****.**"));
     $list->addBcc("*****@*****.**", "Joe");
     $list->addBcc("*****@*****.**");
     $list->removeBcc("*****@*****.**");
     $this->assertEqual(array("*****@*****.**", "*****@*****.**"), array_keys($list->getBcc()));
 }
 /**
  * Send a contact mail (text only) with the data provided by the given form.
  *
  * @uses Swift
  *
  * @throws InvalidArgumentException
  * @throws RuntimeException
  *
  * @param sfContactForm $form A valid contact form which contains the content.
  * @param sfContactFormDecorator $decorator Defaults to null. If given, the decorated subject and message will be used.
  *
  * @return bool True if all recipients got the mail.
  */
 public static function send(sfContactForm $form, sfContactFormDecorator $decorator = null)
 {
     if (!$form->isValid()) {
         throw new InvalidArgumentException('The given form is not valid.', 1);
     }
     if (!class_exists('Swift')) {
         throw new RuntimeException('Swift could not be found.');
     }
     // set up sender
     $from = sfConfig::get('sf_contactformplugin_from', false);
     if ($from === false) {
         throw new InvalidArgumentException('Configuration value of sf_contactformplugin_from is missing.', 2);
     }
     // where to send the contents of the contact form
     $mail = sfConfig::get('sf_contactformplugin_mail', false);
     if ($mail === false) {
         throw new InvalidArgumentException('Configuration value of sf_contactformplugin_mail is missing.', 3);
     }
     // set up mail content
     if (!is_null($decorator)) {
         $subject = $decorator->getSubject();
         $body = $decorator->getMessage();
     } else {
         $subject = $form->getValue('subject');
         $body = $form->getValue('message');
     }
     // set up recipients
     $recipients = new Swift_RecipientList();
     // check amount for given recipients
     $recipientCheck = 0;
     // use the sender as recipient to apply other recipients only as blind carbon copy
     $recipients->addTo($from);
     $recipientCheck++;
     // add a mail where to send the message
     $recipients->addBcc($mail);
     $recipientCheck++;
     // add sender to recipients, if chosen
     if ($form->getValue('sendcopy')) {
         $recipients->addBcc($form->getValue('email'));
         $recipientCheck++;
     }
     if (count($recipients->getIterator('bcc')) === 0) {
         throw new InvalidArgumentException('There are no recipients given.', 4);
     }
     // send the mail using swift
     try {
         $mailer = new Swift(new Swift_Connection_NativeMail());
         $message = new Swift_Message($subject, $body, 'text/plain');
         $countRecipients = $mailer->send($message, $recipients, $from);
         $mailer->disconnect();
         return $countRecipients == $recipientCheck;
     } catch (Exception $e) {
         $mailer->disconnect();
         throw $e;
     }
 }
Beispiel #3
0
 public function executeRegister($request)
 {
     $userParams = $request->getParameter('api_user');
     $this->user_form = new ApiUserForm();
     $this->created = false;
     if ($request->isMethod('post')) {
         //bind request params to form
         $captcha = array('recaptcha_challenge_field' => $request->getParameter('recaptcha_challenge_field'), 'recaptcha_response_field' => $request->getParameter('recaptcha_response_field'));
         $userParams = array_merge($userParams, array('captcha' => $captcha));
         $this->user_form->bind($userParams);
         //look for user with duplicate email
         $q = LsDoctrineQuery::create()->from('ApiUser u')->where('u.email = ?', $userParams['email']);
         if ($q->count()) {
             $validator = new sfValidatorString(array(), array('invalid' => 'There is already an API user with that email address.'));
             $this->user_form->getErrorSchema()->addError(new sfValidatorError($validator, 'invalid'), 'email');
             $request->setError('email', 'There is already a user with that email');
         }
         if ($this->user_form->isValid() && !$request->hasErrors()) {
             //create inactive api user
             $user = new ApiUser();
             $user->name_first = $userParams['name_first'];
             $user->name_last = $userParams['name_last'];
             $user->email = $userParams['email'];
             $user->reason = $userParams['reason'];
             $user->api_key = $user->generateKey();
             $user->is_active = 1;
             $user->save();
             //add admin notification email to queue
             $email = new ScheduledEmail();
             $email->from_name = sfConfig::get('app_mail_sender_name');
             $email->from_email = sfConfig::get('app_mail_sender_address');
             $email->to_name = sfConfig::get('app_mail_sender_name');
             $email->to_email = sfConfig::get('app_mail_sender_address');
             $email->subject = sprintf("%s (%s) has requested an API key", $user->getFullName(), $user->email);
             $email->body_text = $this->getPartial('keyrequestnotify', array('user' => $user));
             $email->save();
             $this->created = true;
             //send approval email
             $mailBody = $this->getPartial('keycreatenotify', array('user' => $user));
             $mailer = new Swift(new Swift_Connection_NativeMail());
             $message = new Swift_Message('Your LittleSis API key', $mailBody, 'text/plain');
             $from = new Swift_Address(sfConfig::get('app_mail_sender_address'), sfConfig::get('app_mail_sender_name'));
             $recipients = new Swift_RecipientList();
             $recipients->addTo($user->email, $user->name_first . ' ' . $user->name_last);
             $recipients->addBcc(sfConfig::get('app_mail_sender_address'));
             $mailer->send($message, $recipients, $from);
             $mailer->disconnect();
         }
     }
 }
Beispiel #4
0
 public function executeApproveUser($request)
 {
     if ($request->isMethod('post')) {
         if (!($apiUser = Doctrine::getTable('ApiUser')->find($request->getParameter('id')))) {
             $this->forward404();
         }
         $apiUser->is_active = 1;
         $apiUser->save();
         //send approval email
         $mailBody = $this->getPartial('accountcreatenotify', array('user' => $apiUser));
         $mailer = new Swift(new Swift_Connection_NativeMail());
         $message = new Swift_Message('Your LittleSis API key', $mailBody, 'text/plain');
         $from = new Swift_Address(sfConfig::get('app_api_sender_address'), sfConfig::get('app_api_sender_name'));
         $recipients = new Swift_RecipientList();
         $recipients->addTo($apiUser['email'], $apiUser['name_first'] . ' ' . $apiUser['name_last']);
         $recipients->addBcc(sfConfig::get('app_api_sender_address'));
         $mailer->send($message, $recipients, $from);
         $mailer->disconnect();
     }
     $this->redirect('api/users');
 }
Beispiel #5
0
 /**
  * Send Email
  * 
  * @param int $id_lang Language of the email (to translate the template)
  * @param string $template Template: the name of template not be a var but a string !
  * @param string $subject
  * @param string $template_vars
  * @param string $to
  * @param string $to_name
  * @param string $from
  * @param string $from_name
  * @param array $file_attachment Array with three parameters (content, mime and name). You can use an array of array to attach multiple files
  * @param bool $modeSMTP
  * @param string $template_path
  * @param bool $die
  * @param string $bcc Bcc recipient
  */
 public static function Send($id_lang, $template, $subject, $template_vars, $to, $to_name = null, $from = null, $from_name = null, $file_attachment = null, $mode_smtp = null, $template_path = _PS_MAIL_DIR_, $die = false, $id_shop = null, $bcc = null)
 {
     $configuration = Configuration::getMultiple(array('PS_SHOP_EMAIL', 'PS_MAIL_METHOD', 'PS_MAIL_SERVER', 'PS_MAIL_USER', 'PS_MAIL_PASSWD', 'PS_SHOP_NAME', 'PS_MAIL_SMTP_ENCRYPTION', 'PS_MAIL_SMTP_PORT', 'PS_MAIL_TYPE'), null, null, $id_shop);
     // Returns immediatly if emails are deactivated
     if ($configuration['PS_MAIL_METHOD'] == 3) {
         return true;
     }
     $theme_path = _PS_THEME_DIR_;
     // Get the path of theme by id_shop if exist
     if (is_numeric($id_shop) && $id_shop) {
         $shop = new Shop((int) $id_shop);
         $theme_name = $shop->getTheme();
         if (_THEME_NAME_ != $theme_name) {
             $theme_path = _PS_ROOT_DIR_ . '/themes/' . $theme_name . '/';
         }
     }
     if (!isset($configuration['PS_MAIL_SMTP_ENCRYPTION'])) {
         $configuration['PS_MAIL_SMTP_ENCRYPTION'] = 'off';
     }
     if (!isset($configuration['PS_MAIL_SMTP_PORT'])) {
         $configuration['PS_MAIL_SMTP_PORT'] = 'default';
     }
     // Sending an e-mail can be of vital importance for the merchant, when his password is lost for example, so we must not die but do our best to send the e-mail
     if (!isset($from) || !Validate::isEmail($from)) {
         $from = $configuration['PS_SHOP_EMAIL'];
     }
     if (!Validate::isEmail($from)) {
         $from = null;
     }
     // $from_name is not that important, no need to die if it is not valid
     if (!isset($from_name) || !Validate::isMailName($from_name)) {
         $from_name = $configuration['PS_SHOP_NAME'];
     }
     if (!Validate::isMailName($from_name)) {
         $from_name = null;
     }
     // It would be difficult to send an e-mail if the e-mail is not valid, so this time we can die if there is a problem
     if (!is_array($to) && !Validate::isEmail($to)) {
         Tools::dieOrLog(Tools::displayError('Error: parameter "to" is corrupted'), $die);
         return false;
     }
     if (!is_array($template_vars)) {
         $template_vars = array();
     }
     // Do not crash for this error, that may be a complicated customer name
     if (is_string($to_name) && !empty($to_name) && !Validate::isMailName($to_name)) {
         $to_name = null;
     }
     if (!Validate::isTplName($template)) {
         Tools::dieOrLog(Tools::displayError('Error: invalid e-mail template'), $die);
         return false;
     }
     if (!Validate::isMailSubject($subject)) {
         Tools::dieOrLog(Tools::displayError('Error: invalid e-mail subject'), $die);
         return false;
     }
     /* Construct multiple recipients list if needed */
     $to_list = new Swift_RecipientList();
     if (is_array($to) && isset($to)) {
         foreach ($to as $key => $addr) {
             $addr = trim($addr);
             if (!Validate::isEmail($addr)) {
                 Tools::dieOrLog(Tools::displayError('Error: invalid e-mail address'), $die);
                 return false;
             }
             if (is_array($to_name)) {
                 if ($to_name && is_array($to_name) && Validate::isGenericName($to_name[$key])) {
                     $to_name = $to_name[$key];
                 }
             }
             if ($to_name == null || $to_name == $addr) {
                 $to_name = '';
             } else {
                 if (function_exists('mb_encode_mimeheader')) {
                     $to_name = mb_encode_mimeheader($to_name, 'utf-8');
                 } else {
                     $to_name = self::mimeEncode($to_name);
                 }
             }
             $to_list->addTo($addr, $to_name);
         }
         $to_plugin = $to[0];
     } else {
         /* Simple recipient, one address */
         $to_plugin = $to;
         if ($to_name == null || $to_name == $to) {
             $to_name = '';
         } else {
             if (function_exists('mb_encode_mimeheader')) {
                 $to_name = mb_encode_mimeheader($to_name, 'utf-8');
             } else {
                 $to_name = self::mimeEncode($to_name);
             }
         }
         $to_list->addTo($to, $to_name);
     }
     if (isset($bcc)) {
         $to_list->addBcc($bcc);
     }
     $to = $to_list;
     try {
         /* Connect with the appropriate configuration */
         if ($configuration['PS_MAIL_METHOD'] == 2) {
             if (empty($configuration['PS_MAIL_SERVER']) || empty($configuration['PS_MAIL_SMTP_PORT'])) {
                 Tools::dieOrLog(Tools::displayError('Error: invalid SMTP server or SMTP port'), $die);
                 return false;
             }
             $connection = new Swift_Connection_SMTP($configuration['PS_MAIL_SERVER'], $configuration['PS_MAIL_SMTP_PORT'], $configuration['PS_MAIL_SMTP_ENCRYPTION'] == 'ssl' ? Swift_Connection_SMTP::ENC_SSL : ($configuration['PS_MAIL_SMTP_ENCRYPTION'] == 'tls' ? Swift_Connection_SMTP::ENC_TLS : Swift_Connection_SMTP::ENC_OFF));
             $connection->setTimeout(4);
             if (!$connection) {
                 return false;
             }
             if (!empty($configuration['PS_MAIL_USER'])) {
                 $connection->setUsername($configuration['PS_MAIL_USER']);
             }
             if (!empty($configuration['PS_MAIL_PASSWD'])) {
                 $connection->setPassword($configuration['PS_MAIL_PASSWD']);
             }
         } else {
             $connection = new Swift_Connection_NativeMail();
         }
         if (!$connection) {
             return false;
         }
         $swift = new Swift($connection, Configuration::get('PS_MAIL_DOMAIN', null, null, $id_shop));
         /* Get templates content */
         $iso = Language::getIsoById((int) $id_lang);
         if (!$iso) {
             Tools::dieOrLog(Tools::displayError('Error - No ISO code for email'), $die);
             return false;
         }
         $template = $iso . '/' . $template;
         $module_name = false;
         $override_mail = false;
         // get templatePath
         if (preg_match('#' . __PS_BASE_URI__ . 'modules/#', str_replace(DIRECTORY_SEPARATOR, '/', $template_path)) && preg_match('#modules/([a-z0-9_-]+)/#ui', str_replace(DIRECTORY_SEPARATOR, '/', $template_path), $res)) {
             $module_name = $res[1];
         }
         if ($module_name !== false && (file_exists($theme_path . 'modules/' . $module_name . '/mails/' . $template . '.txt') || file_exists($theme_path . 'modules/' . $module_name . '/mails/' . $template . '.html'))) {
             $template_path = $theme_path . 'modules/' . $module_name . '/mails/';
         } elseif (file_exists($theme_path . 'mails/' . $template . '.txt') || file_exists($theme_path . 'mails/' . $template . '.html')) {
             $template_path = $theme_path . 'mails/';
             $override_mail = true;
         }
         if (!file_exists($template_path . $template . '.txt') && ($configuration['PS_MAIL_TYPE'] == Mail::TYPE_BOTH || $configuration['PS_MAIL_TYPE'] == Mail::TYPE_TEXT)) {
             Tools::dieOrLog(Tools::displayError('Error - The following e-mail template is missing:') . ' ' . $template_path . $template . '.txt', $die);
             return false;
         } else {
             if (!file_exists($template_path . $template . '.html') && ($configuration['PS_MAIL_TYPE'] == Mail::TYPE_BOTH || $configuration['PS_MAIL_TYPE'] == Mail::TYPE_HTML)) {
                 Tools::dieOrLog(Tools::displayError('Error - The following e-mail template is missing:') . ' ' . $template_path . $template . '.html', $die);
                 return false;
             }
         }
         $template_html = file_get_contents($template_path . $template . '.html');
         $template_txt = strip_tags(html_entity_decode(file_get_contents($template_path . $template . '.txt'), null, 'utf-8'));
         if ($override_mail && file_exists($template_path . $iso . '/lang.php')) {
             include_once $template_path . $iso . '/lang.php';
         } else {
             if ($module_name && file_exists($theme_path . 'mails/' . $iso . '/lang.php')) {
                 include_once $theme_path . 'mails/' . $iso . '/lang.php';
             } else {
                 if (file_exists(_PS_MAIL_DIR_ . $iso . '/lang.php')) {
                     include_once _PS_MAIL_DIR_ . $iso . '/lang.php';
                 } else {
                     Tools::dieOrLog(Tools::displayError('Error - The lang file is missing for :') . ' ' . $iso, $die);
                     return false;
                 }
             }
         }
         /* Create mail and attach differents parts */
         $message = new Swift_Message('[' . Configuration::get('PS_SHOP_NAME', null, null, $id_shop) . '] ' . $subject);
         $message->setCharset('utf-8');
         /* Set Message-ID - getmypid() is blocked on some hosting */
         $message->setId(Mail::generateId());
         $message->headers->setEncoding('Q');
         if (Configuration::get('PS_LOGO_MAIL') !== false && file_exists(_PS_IMG_DIR_ . Configuration::get('PS_LOGO_MAIL', null, null, $id_shop))) {
             $logo = _PS_IMG_DIR_ . Configuration::get('PS_LOGO_MAIL', null, null, $id_shop);
         } else {
             if (file_exists(_PS_IMG_DIR_ . Configuration::get('PS_LOGO', null, null, $id_shop))) {
                 $logo = _PS_IMG_DIR_ . Configuration::get('PS_LOGO', null, null, $id_shop);
             } else {
                 $template_vars['{shop_logo}'] = '';
             }
         }
         ShopUrl::cacheMainDomainForShop((int) $id_shop);
         /* don't attach the logo as */
         if (isset($logo)) {
             $template_vars['{shop_logo}'] = $message->attach(new Swift_Message_EmbeddedFile(new Swift_File($logo), null, ImageManager::getMimeTypeByExtension($logo)));
         }
         if (Context::getContext()->link instanceof Link === false) {
             Context::getContext()->link = new Link();
         }
         $template_vars['{shop_name}'] = Tools::safeOutput(Configuration::get('PS_SHOP_NAME', null, null, $id_shop));
         $template_vars['{shop_url}'] = Context::getContext()->link->getPageLink('index', true, Context::getContext()->language->id);
         $template_vars['{my_account_url}'] = Context::getContext()->link->getPageLink('my-account', true, Context::getContext()->language->id);
         $template_vars['{guest_tracking_url}'] = Context::getContext()->link->getPageLink('guest-tracking', true, Context::getContext()->language->id);
         $template_vars['{history_url}'] = Context::getContext()->link->getPageLink('history', true, Context::getContext()->language->id);
         $template_vars['{color}'] = Tools::safeOutput(Configuration::get('PS_MAIL_COLOR', null, null, $id_shop));
         $swift->attachPlugin(new Swift_Plugin_Decorator(array($to_plugin => $template_vars)), 'decorator');
         if ($configuration['PS_MAIL_TYPE'] == Mail::TYPE_BOTH || $configuration['PS_MAIL_TYPE'] == Mail::TYPE_TEXT) {
             $message->attach(new Swift_Message_Part($template_txt, 'text/plain', '8bit', 'utf-8'));
         }
         if ($configuration['PS_MAIL_TYPE'] == Mail::TYPE_BOTH || $configuration['PS_MAIL_TYPE'] == Mail::TYPE_HTML) {
             $message->attach(new Swift_Message_Part($template_html, 'text/html', '8bit', 'utf-8'));
         }
         if ($file_attachment && !empty($file_attachment)) {
             // Multiple attachments?
             if (!is_array(current($file_attachment))) {
                 $file_attachment = array($file_attachment);
             }
             foreach ($file_attachment as $attachment) {
                 if (isset($attachment['content']) && isset($attachment['name']) && isset($attachment['mime'])) {
                     $message->attach(new Swift_Message_Attachment($attachment['content'], $attachment['name'], $attachment['mime']));
                 }
             }
         }
         /* Send mail */
         $send = $swift->send($message, $to, new Swift_Address($from, $from_name));
         $swift->disconnect();
         ShopUrl::resetMainDomainCache();
         return $send;
     } catch (Swift_Exception $e) {
         return false;
     }
 }
Beispiel #6
0
 /**
  * Send a mail, using params passed :
  * 
  * - connection:
  *     type (*)
  *     params
  * - from (*) is an address
  * - reply-to is an address or an array of addresses
  * - return-path is an address
  * - to (*) is an address or an array of addresses
  * - cc is an address or an array of addresses
  * - bcc is an address or an array of addresses
  * - subject-template
  * - subject (*)
  * - body (*) can also be a direct string
  *     content (*)
  *     content-type (default is text/plain)
  *     encoding
  *     charset
  * - parts is an array of bodies
  * - attachments
  * - embed-images
  * 
  * body[content] can be a direct string (which will be the body's content), or an associative array :
  * - type (partial or component)
  * - name is the name of the partial or component, as "module/partialName" or "module/componentName"
  * - vars is an associative array of variables, passed to the view
  * 
  * attachments is an array of attachment, an attachement is either a string (path name), or a list of these options :
  * - path (*) is the real path on filesystem
  * - filename
  * - mime-type
  * 
  * an address can be either :
  * - a string as an email address or in the "name <email>" format
  * - an array of two strings [name, email] or [email, name]
  * 
  * embed-images is an associative array of key => image light path.
  * You can use "%%IMG_name-of-image%%" in the body or in any part to reference the corresponding embedded image.
  *  
  * @param array $options
  * @throws Exception
  * @return int The number of successful recipients
  */
 protected static function _send(array $options)
 {
     $options = array_merge(sfConfig::get('app_mailer_defaults', array()), $options);
     // Mailer
     if (!isset($options['connection'])) {
         throw new Exception('Connection configuration required');
     }
     if (!isset($options['connection']['type'])) {
         throw new Exception('Connection type undefined');
     }
     if (!isset($options['connection']['params'])) {
         $options['connection']['params'] = array();
     }
     $connection = self::getConnection($options['connection']['type'], $options['connection']['params']);
     $mailer = new Swift($connection);
     $to = new Swift_RecipientList();
     $to->addTo(self::getSwiftAddresses($options['to']));
     // Basic elements
     $from = self::getSwiftAddress($options['from']);
     if (!isset($options['subject'])) {
         throw new Exception('Subject required');
     }
     if (!isset($options['subject-template'])) {
         $options['subject-template'] = '%s';
     }
     if (!isset($options['i18n-catalogue'])) {
         $options['i18n-catalogue'] = 'messages';
     }
     $subject = self::getI18NString($options['subject'], $options['subject-template'], $options['i18n-catalogue']);
     // Message to be sent
     $mail = new Swift_Message($subject);
     // Embedded images
     if (isset($options['embed-images'])) {
         $embedded_images = self::embedImages($mail, @$options['embed-images']);
     } else {
         $embedded_images = array();
     }
     // Get body as the main part
     if (!isset($options['body'])) {
         throw new Exception('Body is required');
     }
     if (!is_array($options['body'])) {
         $options['body'] = array('content' => $options['body']);
     }
     $body = self::getPart($options['body'], $embedded_images);
     // Attach files
     if (isset($options['attachments']) && is_array($options['attachments'])) {
         // Known bug : When we have attachments, we must have body declared as a part, or the
         // mail will be received with no body. We fix this here :
         if (!isset($options['parts'])) {
             $options['parts'] = array();
         }
         foreach ($options['attachments'] as $attachment) {
             $mail->attach(self::getAttachment($attachment));
         }
     }
     // Attach parts (body is the first one)
     if (isset($options['parts']) && is_array($options['parts'])) {
         $parts = self::getParts($options['parts'], $embedded_images);
         array_unshift($parts, $body);
         foreach ($parts as $part) {
             $mail->attach($part);
         }
     } else {
         $mail->setBody($body->getData());
         $mail->setCharset($body->getCharset());
         $mail->setEncoding($body->getEncoding());
         $mail->setContentType($body->getContentType());
     }
     // Handle other options
     if (isset($options['bcc'])) {
         $to->addBcc(self::getSwiftAddresses($options['bcc']));
     }
     if (isset($options['cc'])) {
         $to->addCc(self::getSwiftAddresses($options['cc']));
     }
     if (isset($options['reply-to'])) {
         $mail->setReplyTo(self::getSwiftAddresses($options['reply-to']));
     }
     if (isset($options['return-path'])) {
         $mail->setReturnPath(self::getSwiftAddress($options['return-path']));
     }
     try {
         // Try to send the mail
         $result = $mailer->send($mail, $to, $from);
         $mailer->disconnect();
         return $result;
     } catch (Exception $e) {
         // An error occured, disconnect an eventual connection, and forwards the exception
         $mailer->disconnect();
         throw $e;
     }
 }
Beispiel #7
0
 static function sendTicketMessage($properties = array())
 {
     $settings = CerberusSettings::getInstance();
     $helpdesk_senders = CerberusApplication::getHelpdeskSenders();
     @($from_addy = $settings->get(CerberusSettings::DEFAULT_REPLY_FROM, $_SERVER['SERVER_ADMIN']));
     @($from_personal = $settings->get(CerberusSettings::DEFAULT_REPLY_PERSONAL, ''));
     // [TODO] If we still don't have a $from_addy we need a graceful failure.
     /*
     	     * [TODO] Move these into constants?
     	    'message_id'
     	    -----'ticket_id'
     'subject'
     	    'to'
     	    'cc'
     	    'bcc'
     	    'content'
     	    'files'
     	    'closed'
     	    'ticket_reopen'
     	    'unlock_date'
     	    'bucket_id'
     	    'agent_id',
     'is_autoreply',
     'dont_save_copy'
     */
     $mail_succeeded = true;
     try {
         // objects
         $mail_service = DevblocksPlatform::getMailService();
         $mailer = $mail_service->getMailer(CerberusMail::getMailerDefaults());
         $mail = $mail_service->createMessage();
         // properties
         @($reply_message_id = $properties['message_id']);
         @($content =& $properties['content']);
         @($files = $properties['files']);
         @($forward_files = $properties['forward_files']);
         @($worker_id = $properties['agent_id']);
         @($subject = $properties['subject']);
         $message = DAO_Ticket::getMessage($reply_message_id);
         $message_headers = DAO_MessageHeader::getAll($reply_message_id);
         $ticket_id = $message->ticket_id;
         $ticket = DAO_Ticket::getTicket($ticket_id);
         // [TODO] Check that message|ticket isn't NULL
         // If this ticket isn't spam trained and our outgoing message isn't an autoreply
         if ($ticket->spam_training == CerberusTicketSpamTraining::BLANK && (!isset($properties['is_autoreply']) || !$properties['is_autoreply'])) {
             CerberusBayes::markTicketAsNotSpam($ticket_id);
         }
         // Allow teams to override the default from/personal
         @($group_reply = DAO_GroupSettings::get($ticket->team_id, DAO_GroupSettings::SETTING_REPLY_FROM, ''));
         @($group_personal = DAO_GroupSettings::get($ticket->team_id, DAO_GroupSettings::SETTING_REPLY_PERSONAL, ''));
         @($group_personal_with_worker = DAO_GroupSettings::get($ticket->team_id, DAO_GroupSettings::SETTING_REPLY_PERSONAL_WITH_WORKER, 0));
         if (!empty($group_reply)) {
             $from_addy = $group_reply;
         }
         if (!empty($group_personal)) {
             $from_personal = $group_personal;
         }
         // Prefix the worker name on the personal line?
         if (!empty($group_personal_with_worker) && null != ($reply_worker = DAO_Worker::getAgent($worker_id))) {
             $from_personal = $reply_worker->getName() . (!empty($from_personal) ? ', ' . $from_personal : "");
         }
         $sendFrom = new Swift_Address($from_addy, $from_personal);
         // Headers
         $mail->setFrom($sendFrom);
         $mail->generateId();
         $mail->headers->set('X-Mailer', 'Cerberus Helpdesk (Build ' . APP_BUILD . ')');
         // Subject
         if (empty($subject)) {
             $subject = $ticket->subject;
         }
         if (!empty($properties['to'])) {
             // forward
             $mail->setSubject($subject);
         } else {
             // reply
             @($group_has_subject = intval(DAO_GroupSettings::get($ticket->team_id, DAO_GroupSettings::SETTING_SUBJECT_HAS_MASK, 0)));
             @($group_subject_prefix = DAO_GroupSettings::get($ticket->team_id, DAO_GroupSettings::SETTING_SUBJECT_PREFIX, ''));
             $prefix = sprintf("[%s#%s] ", !empty($group_subject_prefix) ? $group_subject_prefix . ' ' : '', $ticket->mask);
             $mail->setSubject(sprintf('Re: %s%s', $group_has_subject ? $prefix : '', $subject));
         }
         $sendTo = new Swift_RecipientList();
         // References
         if (!empty($message) && false !== @($in_reply_to = $message_headers['message-id'])) {
             $mail->headers->set('References', $in_reply_to);
             $mail->headers->set('In-Reply-To', $in_reply_to);
         }
         // Auto-reply handling (RFC-3834 compliant)
         if (isset($properties['is_autoreply']) && $properties['is_autoreply']) {
             $mail->headers->set('Auto-Submitted', 'auto-replied');
             if (null == ($first_address = DAO_Address::get($ticket->first_wrote_address_id))) {
                 return;
             }
             // Don't send e-mail to ourselves
             if (isset($helpdesk_senders[$first_address->email])) {
                 return;
             }
             // Make sure we haven't mailed this address an autoreply within 5 minutes
             if ($first_address->last_autoreply > 0 && $first_address->last_autoreply > time() - 300) {
                 return;
             }
             $first_email = strtolower($first_address->email);
             $first_split = explode('@', $first_email);
             if (!is_array($first_split) || count($first_split) != 2) {
                 return;
             }
             // If return-path is blank
             if (isset($message_headers['return-path']) && $message_headers['return-path'] == '<>') {
                 return;
             }
             // Ignore bounces
             if ($first_split[0] == "postmaster" || $first_split[0] == "mailer-daemon") {
                 return;
             }
             // Ignore autoresponses to autoresponses
             if (isset($message_headers['auto-submitted']) && $message_headers['auto-submitted'] != 'no') {
                 return;
             }
             if (isset($message_headers['precedence']) && ($message_headers['precedence'] == 'list' || $message_headers['precedence'] == 'junk' || ($message_headers['precedence'] = 'bulk'))) {
                 return;
             }
             // Set the auto-reply date for this address to right now
             DAO_Address::update($ticket->first_wrote_address_id, array(DAO_Address::LAST_AUTOREPLY => time()));
             // Auto-reply just to the initial requester
             $sendTo->addTo($first_address->email);
             $mail->setTo($first_address->email);
             // Not an auto-reply
         } else {
             // Forwards
             if (!empty($properties['to'])) {
                 $to = array();
                 $aTo = DevblocksPlatform::parseCsvString(str_replace(';', ',', $properties['to']));
                 foreach ($aTo as $addy) {
                     $to[] = new Swift_Address($addy);
                     $sendTo->addTo($addy);
                 }
                 if (!empty($to)) {
                     $mail->setTo($to);
                 }
                 // Replies
             } else {
                 // Recipients
                 $to = array();
                 $requesters = DAO_Ticket::getRequestersByTicket($ticket_id);
                 if (is_array($requesters)) {
                     foreach ($requesters as $requester) {
                         /* @var $requester Model_Address */
                         $to[] = new Swift_Address($requester->email);
                         $sendTo->addTo($requester->email);
                     }
                 }
                 $mail->setTo($to);
             }
             // Ccs
             if (!empty($properties['cc'])) {
                 $ccs = array();
                 $aCc = DevblocksPlatform::parseCsvString(str_replace(';', ',', $properties['cc']));
                 foreach ($aCc as $addy) {
                     $sendTo->addCc($addy);
                     $ccs[] = new Swift_Address($addy);
                 }
                 if (!empty($ccs)) {
                     $mail->setCc($ccs);
                 }
             }
             // Bccs
             if (!empty($properties['bcc'])) {
                 $aBcc = DevblocksPlatform::parseCsvString(str_replace(';', ',', $properties['bcc']));
                 foreach ($aBcc as $addy) {
                     $sendTo->addBcc($addy);
                 }
             }
         }
         /*
          * [IMPORTANT -- Yes, this is simply a line in the sand.]
          * You're welcome to modify the code to meet your needs, but please respect 
          * our licensing.  Buy a legitimate copy to help support the project!
          * http://www.cerberusweb.com/
          */
         $license = CerberusLicense::getInstance();
         if (empty($license) || @empty($license['serial'])) {
             $content .= base64_decode("DQoNCi0tLQ0KQ29tYmF0IHNwYW0gYW5kIGltcHJvdmUgcmVzc" . "G9uc2UgdGltZXMgd2l0aCBDZXJiZXJ1cyBIZWxwZGVzayA0LjAhDQpodHRwOi8vd3d3LmNlc" . "mJlcnVzd2ViLmNvbS8NCg");
         }
         // Body
         $mail->attach(new Swift_Message_Part($content, 'text/plain', 'base64', LANG_CHARSET_CODE));
         // Mime Attachments
         if (is_array($files) && !empty($files)) {
             foreach ($files['tmp_name'] as $idx => $file) {
                 if (empty($file) || empty($files['name'][$idx])) {
                     continue;
                 }
                 $mail->attach(new Swift_Message_Attachment(new Swift_File($file), $files['name'][$idx], $files['type'][$idx]));
             }
         }
         // Forward Attachments
         if (!empty($forward_files) && is_array($forward_files)) {
             $attachments_path = APP_STORAGE_PATH . '/attachments/';
             foreach ($forward_files as $file_id) {
                 $attachment = DAO_Attachment::get($file_id);
                 $attachment_path = $attachments_path . $attachment->filepath;
                 $mail->attach(new Swift_Message_Attachment(new Swift_File($attachment_path), $attachment->display_name, $attachment->mime_type));
             }
         }
         if (!DEMO_MODE) {
             if (!$mailer->send($mail, $sendTo, $sendFrom)) {
                 $mail_succeeded = false;
                 throw new Exception('Mail not sent.');
             }
         }
     } catch (Exception $e) {
         // tag failure, so we can add a note to the message later
         $mail_succeeded = false;
     }
     // Handle post-mail actions
     $change_fields = array();
     $fromAddressInst = CerberusApplication::hashLookupAddress($from_addy, true);
     $fromAddressId = $fromAddressInst->id;
     if ((!isset($properties['dont_keep_copy']) || !$properties['dont_keep_copy']) && (!isset($properties['is_autoreply']) || !$properties['is_autoreply'])) {
         $change_fields[DAO_Ticket::LAST_WROTE_ID] = $fromAddressId;
         $change_fields[DAO_Ticket::UPDATED_DATE] = time();
         if (!empty($worker_id)) {
             $change_fields[DAO_Ticket::LAST_WORKER_ID] = $worker_id;
             $change_fields[DAO_Ticket::LAST_ACTION_CODE] = CerberusTicketActionCode::TICKET_WORKER_REPLY;
         }
         // Only change the subject if not forwarding
         if (!empty($subject) && empty($properties['to'])) {
             $change_fields[DAO_Ticket::SUBJECT] = $subject;
         }
         $fields = array(DAO_Message::TICKET_ID => $ticket_id, DAO_Message::CREATED_DATE => time(), DAO_Message::ADDRESS_ID => $fromAddressId, DAO_Message::IS_OUTGOING => 1, DAO_Message::WORKER_ID => !empty($worker_id) ? $worker_id : 0);
         $message_id = DAO_Message::create($fields);
         // Content
         DAO_MessageContent::create($message_id, $content);
         // Headers
         if (!empty($mail->headers) && method_exists($mail->headers, 'getList')) {
             foreach ($mail->headers->getList() as $hdr => $v) {
                 if (null != ($hdr_val = $mail->headers->getEncoded($hdr))) {
                     if (!empty($hdr_val)) {
                         DAO_MessageHeader::create($message_id, $ticket_id, $hdr, CerberusParser::fixQuotePrintableString($hdr_val));
                     }
                 }
             }
         }
         if (is_array($files) && !empty($files)) {
             $attachment_path = APP_STORAGE_PATH . '/attachments/';
             reset($files);
             foreach ($files['tmp_name'] as $idx => $file) {
                 if (empty($file) || empty($files['name'][$idx]) || !file_exists($file)) {
                     continue;
                 }
                 $fields = array(DAO_Attachment::MESSAGE_ID => $message_id, DAO_Attachment::DISPLAY_NAME => $files['name'][$idx], DAO_Attachment::MIME_TYPE => $files['type'][$idx], DAO_Attachment::FILE_SIZE => filesize($file));
                 $file_id = DAO_Attachment::create($fields);
                 $attachment_bucket = sprintf("%03d/", mt_rand(1, 100));
                 $attachment_file = $file_id;
                 if (!file_exists($attachment_path . $attachment_bucket)) {
                     mkdir($attachment_path . $attachment_bucket, 0775, true);
                 }
                 if (!is_writeable($attachment_path . $attachment_bucket)) {
                     echo "Can't write to " . $attachment_path . $attachment_bucket . "<BR>";
                 }
                 copy($file, $attachment_path . $attachment_bucket . $attachment_file);
                 @unlink($file);
                 DAO_Attachment::update($file_id, array(DAO_Attachment::FILEPATH => $attachment_bucket . $attachment_file));
             }
         }
         // add note to message if email failed
         if ($mail_succeeded === false) {
             $fields = array(DAO_MessageNote::MESSAGE_ID => $message_id, DAO_MessageNote::CREATED => time(), DAO_MessageNote::WORKER_ID => 0, DAO_MessageNote::CONTENT => 'Exception thrown while sending email: ' . $e->getMessage(), DAO_MessageNote::TYPE => Model_MessageNote::TYPE_ERROR);
             DAO_MessageNote::create($fields);
         }
     }
     // Post-Reply Change Properties
     if (isset($properties['closed'])) {
         switch ($properties['closed']) {
             case 0:
                 // open
                 $change_fields[DAO_Ticket::IS_WAITING] = 0;
                 $change_fields[DAO_Ticket::IS_CLOSED] = 0;
                 $change_fields[DAO_Ticket::IS_DELETED] = 0;
                 $change_fields[DAO_Ticket::DUE_DATE] = 0;
                 break;
             case 1:
                 // closed
                 $change_fields[DAO_Ticket::IS_WAITING] = 0;
                 $change_fields[DAO_Ticket::IS_CLOSED] = 1;
                 $change_fields[DAO_Ticket::IS_DELETED] = 0;
                 if (isset($properties['ticket_reopen'])) {
                     @($time = intval(strtotime($properties['ticket_reopen'])));
                     $change_fields[DAO_Ticket::DUE_DATE] = $time;
                 }
                 break;
             case 2:
                 // waiting
                 $change_fields[DAO_Ticket::IS_WAITING] = 1;
                 $change_fields[DAO_Ticket::IS_CLOSED] = 0;
                 $change_fields[DAO_Ticket::IS_DELETED] = 0;
                 if (isset($properties['ticket_reopen'])) {
                     @($time = intval(strtotime($properties['ticket_reopen'])));
                     $change_fields[DAO_Ticket::DUE_DATE] = $time;
                 }
                 break;
         }
     }
     // Who should handle the followup?
     if (isset($properties['next_worker_id'])) {
         $change_fields[DAO_Ticket::NEXT_WORKER_ID] = $properties['next_worker_id'];
     }
     // Allow anybody to reply after
     if (isset($properties['unlock_date']) && !empty($properties['unlock_date'])) {
         $unlock = strtotime($properties['unlock_date']);
         if (intval($unlock) > 0) {
             $change_fields[DAO_Ticket::UNLOCK_DATE] = $unlock;
         }
     }
     // Move
     if (!empty($properties['bucket_id'])) {
         // [TODO] Use API to move, or fire event
         // [TODO] Ensure team/bucket exist
         list($team_id, $bucket_id) = CerberusApplication::translateTeamCategoryCode($properties['bucket_id']);
         $change_fields[DAO_Ticket::TEAM_ID] = $team_id;
         $change_fields[DAO_Ticket::CATEGORY_ID] = $bucket_id;
     }
     if (!empty($ticket_id) && !empty($change_fields)) {
         DAO_Ticket::updateTicket($ticket_id, $change_fields);
     }
     // Outbound Reply Event (not automated reply, etc.)
     if (!empty($worker_id)) {
         $eventMgr = DevblocksPlatform::getEventService();
         $eventMgr->trigger(new Model_DevblocksEvent('ticket.reply.outbound', array('ticket_id' => $ticket_id, 'worker_id' => $worker_id)));
     }
 }