function send()
 {
     $db =& JFactory::getDBO();
     $msg = false;
     $error = '';
     $list_error = false;
     $params =& JComponentHelper::getParams('com_joomailermailchimpintegration');
     $paramsPrefix = version_compare(JVERSION, '1.6.0', 'ge') ? 'params.' : '';
     $archiveDir = $params->get($paramsPrefix . 'archiveDir', '/administrator/components/com_joomailermailchimpintegration/archive');
     $model =& $this->getModel('send');
     $clientDetails = $model->getClientDetails();
     $MCapi = $params->get($paramsPrefix . 'MCapi');
     $MC = new joomlamailerMCAPI($MCapi);
     //	$MCSTS = new jmsts($MCapi);
     $time = JRequest::getVar('time', 0, '', 'string');
     $listId = JRequest::getVar('listId', '', 'post', 'string');
     $test = JRequest::getVar('test', 0, 'post', 'int');
     $trackOpens = (bool) JRequest::getBool('trackOpens', false, 'post');
     $trackHTML = (bool) JRequest::getBool('trackHTML', false, 'post');
     $trackText = (bool) JRequest::getBool('trackText', false, 'post');
     $ecomm360 = (bool) JRequest::getBool('ecomm360', false, 'post');
     $campaignType = JRequest::getVar('campaignType', 0, 'post');
     $offsetTime = JRequest::getVar('offset-time', 0, 'post');
     $offsetUnits = JRequest::getVar('offset-units', 0, 'post');
     $offsetDir = JRequest::getVar('offset-dir', 0, 'post');
     $event = JRequest::getVar('event', 0, 'post');
     $mergefield = JRequest::getVar('mergefield', 0, 'post');
     $emails = JRequest::getVar('email', '', '', 'array');
     $noGos = array('Email 1', 'Email 2', 'Email 3', 'Email 4', 'Email 5');
     foreach ($emails as $key => $value) {
         if ($value == "" || in_array($value, $noGos)) {
             unset($emails[$key]);
         }
     }
     $emails = array_values($emails);
     $timewarp = JRequest::getVar('timewarp', 0, '', 'int');
     $schedule = JRequest::getVar('schedule', 0, '', 'int');
     if (!$schedule) {
         $delivery = 'Immediately';
     } else {
         $deliveryDate = JRequest::getVar('deliveryDate', 'Immediately', '', 'string');
         $deliveryTime = JRequest::getVar('deliveryTime', '', '', 'string');
         //		if($test) { $delivery = 'Immediately'; }
         //		else
         if ($deliveryDate != 'Immediately') {
             $delivery = $deliveryDate . ' ' . $deliveryTime . ':00';
             // convert time to GMT
             setlocale(LC_TIME, 'en_GB');
             $delivery = gmstrftime("%Y-%m-%d %H:%M:%S", strtotime($delivery));
         } else {
             $delivery = 'Immediately';
         }
     }
     $useSegments = JRequest::getVar('useSegments', 0, '', 'int');
     if ($useSegments) {
         $type = $condition = $conditionDetailValue = array();
         for ($i = 1; $i < 11; $i++) {
             $type[] = JRequest::getVar('segmenttype' . $i, '');
             $condition[] = JRequest::getVar('segmentTypeCondition_' . $i, '');
             $conditionDetailValue[] = JRequest::getVar('segmentTypeConditionDetailValue_' . $i, '');
         }
         // remove empty values
         $type = array_values(array_filter($type));
         $condition = array_values(array_filter($condition));
         $conditionDetailValue = array_values(array_filter($conditionDetailValue));
         $conditions = array();
         for ($i = 0; $i < count($type); $i++) {
             if (is_numeric($type[$i])) {
                 $type[$i] = 'interests-' . $type[$i];
             }
             $conditions[] = array('field' => $type[$i], 'op' => $condition[$i], 'value' => $conditionDetailValue[$i]);
         }
         $segment_opts = array('match' => JRequest::getVar('match', 'any', 'post', 'string'), 'conditions' => $conditions);
     } else {
         $segment_opts = '';
     }
     $query = "SELECT * FROM #__joomailermailchimpintegration_campaigns WHERE creation_date = '" . $time . "'";
     $db->setQuery($query);
     $cDetails = $db->loadObjectList();
     $cDetails = $cDetails[0];
     $campaign_name_ent = htmlentities($cDetails->name);
     $campaign_name_ent = str_replace(' ', '_', $campaign_name_ent);
     $html_file = JURI::root() . substr($archiveDir, 1) . "/" . $campaign_name_ent . ".html";
     $content = array('url' => $html_file);
     // remove cache-preventing meta tags from campaign to avoid rendering issues in email clients
     $metaData = array("<meta http-Equiv=\"Cache-Control\" Content=\"no-cache\">\n", "<meta http-Equiv=\"Pragma\" Content=\"no-cache\">\n", "<meta http-Equiv=\"Expires\" Content=\"0\">\n");
     $filename = JPATH_SITE . $archiveDir . '/' . $campaign_name_ent . ".html";
     $template = JFile::read($filename);
     $template = str_replace($metaData, '', $template);
     $handle = JFile::write($filename, $template);
     if (!$msg) {
         $lists = $MC->lists();
         if (!$listId) {
             $i = 0;
             if ($clientDetails['plan_type'] == 'free') {
                 for ($i = 0; $i < count($lists); $i++) {
                     if ($lists[$i]['member_count'] <= 2000) {
                         $listId = $lists[$i]['id'];
                         $memberCount = $lists[$i]['member_count'];
                         break;
                     }
                 }
                 if (!$listId) {
                     $error = 'error';
                     $msg = JText::_('JM_TO_MANY_RECIPIENTS');
                     $link = 'index.php?option=com_joomailermailchimpintegration&view=send&campaign=' . $time;
                     $this->setRedirect($link, $msg);
                 }
             } else {
                 $listId = $lists[$i]['id'];
             }
         } else {
             foreach ($lists as $list) {
                 if ($list['id'] == $listId) {
                     $memberCount = $list['member_count'];
                     break;
                 }
             }
         }
         if (!$test && $clientDetails['plan_type'] == 'free' && $memberCount > 2000) {
             $error = 'error';
             $msg = JText::_('JM_TO_MANY_RECIPIENTS');
             $link = 'index.php?option=com_joomailermailchimpintegration&view=send&campaign=' . $time;
         } else {
             // submit to MC
             $type = 'regular';
             //		$opts['list_id']	= $cDetails->list_id;
             $opts['list_id'] = $listId;
             $opts['title'] = $cDetails->name;
             if ($test) {
                 $opts['subject'] = JText::_('JM_CAMPAIGN_TEST') . ': ' . $cDetails->subject;
             } else {
                 $opts['subject'] = $cDetails->subject;
                 if ($timewarp) {
                     $opts['timewarp'] = true;
                 }
             }
             $opts['from_email'] = $cDetails->from_email;
             $opts['from_name'] = $cDetails->from_name;
             $opts['tracking'] = array('opens' => $trackOpens, 'html_clicks' => $trackHTML, 'text_clicks' => $trackText);
             $opts['ecomm360'] = $ecomm360;
             $opts['authenticate'] = true;
             //		$opts['analytics'] = array('google'=>'my_google_analytics_key');
             $opts['inline_css'] = true;
             $opts['generate_text'] = true;
             $opts['auto_footer'] = false;
             $opts['folder_id'] = $cDetails->folder_id;
             //Check for auto_tweet
             $tweet = JRequest::getVar('useTwitter');
             if ($tweet) {
                 $opts['auto_tweet'] = true;
             }
             //Check for autoresponder
             $type_opts = array();
             if ($campaignType == 1) {
                 $type = 'auto';
                 $type_opts['offset-units'] = $offsetUnits;
                 $type_opts['offset-time'] = $offsetTime;
                 $type_opts['offset-dir'] = $offsetDir;
                 $type_opts['event'] = $event;
                 $type_opts['event-datemerge'] = $mergefield;
                 // TODO: implement autoresponder folders
                 unset($opts['folder_id']);
             }
             $c_id = $MC->campaignCreate($type, $opts, $content, $segment_opts, $type_opts);
             if (!$MC->errorCode) {
                 if ($test) {
                     $send = $MC->campaignSendTest($c_id, $emails);
                     if ($c_id) {
                         // wait 5 seconds for the campaign to be sent
                         sleep(5);
                         $MC->campaignDelete($c_id);
                     }
                 } else {
                     if (!$schedule) {
                         $send = $MC->campaignSendNow($c_id);
                     } else {
                         $send = $MC->campaignSchedule($c_id, $delivery);
                     }
                 }
             }
             if ($MC->errorCode) {
                 $msg = MCerrorHandler::getErrorMsg($MC);
                 $link = 'index.php?option=com_joomailermailchimpintegration&view=send&campaign=' . $time;
             } else {
                 if ($test) {
                     $msg = JText::_('JM_TEST_CAMPAIGN_SENT');
                     $link = 'index.php?option=com_joomailermailchimpintegration&view=send&campaign=' . $time;
                 } else {
                     if ($schedule) {
                         $query = "UPDATE #__joomailermailchimpintegration_campaigns SET `sent`=1, `cid`='" . $c_id . "' WHERE creation_date = '" . $time . "'";
                         $db->setQuery($query);
                         $db->query();
                         $msg = JText::_('JM_CAMPAIGN_SCHEDULED');
                         $link = 'index.php?option=com_joomailermailchimpintegration&view=main';
                     } else {
                         $query = "UPDATE #__joomailermailchimpintegration_campaigns SET `sent`=2, `cid`='" . $c_id . "' WHERE creation_date = '" . $time . "'";
                         $db->setQuery($query);
                         $db->query();
                         if ($campaignType == 1) {
                             $msg = JText::_('JM_AUTORESPONDER_CREATED');
                         } else {
                             $msg = JText::_('JM_CAMPAIGN_SENT');
                             // clear reports cache
                             require_once JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_joomailermailchimpintegration' . DS . 'controllers' . DS . 'campaigns.php';
                             joomailermailchimpintegrationsControllerCampaigns::clearReportsCache();
                         }
                         $link = 'index.php?option=com_joomailermailchimpintegration&view=campaigns';
                     }
                 }
             }
         }
     }
     $this->setRedirect($link, $msg, $error);
 }