public function opt_in()
 {
     $this->setCache(false);
     $this->_return = self::PROCESS;
     //Redirect if no post data
     if (!isset($_POST) || count($_POST) < 1) {
         header("Location: " . PageTool::getSiteRoot());
         exit;
     }
     $anchor = '';
     if (isset($_POST["anchor"])) {
         $anchor = $_POST["anchor"];
     }
     $backto = '';
     if (isset($_POST["backto"])) {
         $backto = $_POST["backto"];
     }
     $from = '';
     if (isset($_POST["from"])) {
         $from = $_POST["from"];
     }
     $sprott = 0;
     if (isset($_POST["sprott"]) && $_POST["sprott"] == "on") {
         $sprott = 1;
     }
     //Redirect with error if bad email
     $email = SystemTool::sanitize($_POST["email"], 2);
     if (!SystemTool::validate($email)) {
         header("Location: " . PageTool::getSiteRoot() . $backto . "?e=no" . $anchor);
         exit;
     }
     //Ensure we are in a session, if we are not then set up DynamoDB
     SystemTool::ensureSession();
     //Initialize client library
     Casey_Client::init(SystemTool::getAPIHost(), CShell::entity(), ENV, SystemTool::getAPIUser(), SystemTool::getAPIPass());
     Linkserver_Client::init(SystemTool::getLinkServerHost(), ENV, SystemTool::getLinkServerUser(), SystemTool::getLinkServerPass());
     //Set the product sku
     $skus = CShell::skus();
     if ($from == "cr") {
         $sku = $skus['cr'];
     } else {
         $sku = $skus['primary'];
     }
     //Assemble tracking data
     $url = TrackingTool::getURL();
     $user_tracking_id = !empty($_SESSION['user_tracking_id']) ? $_SESSION['user_tracking_id'] : null;
     $session_tracking_id = !empty($_SESSION['session_tracking_id']) ? $_SESSION['session_tracking_id'] : null;
     $client_tracking_id = !empty($_SESSION['client_tracking_id']) ? $_SESSION['client_tracking_id'] : null;
     $affiliate_source = !empty($_SESSION['affiliate_source']) ? $_SESSION['affiliate_source'] : null;
     $affiliate_id = !empty($_SESSION['affid']) ? $_SESSION['affid'] : null;
     if (empty($affiliate_id)) {
         $affiliate_id = !empty($_COOKIE['affid']) ? $_COOKIE['affid'] : null;
         $affiliate_source = 'cookie';
         if (empty($affiliate_id)) {
             $affiliate_id = null;
             $affiliate_source = null;
         }
     }
     if (empty($user_tracking_id)) {
         $user_tracking_id = !empty($_COOKIE['user_tracking_id']) ? $_COOKIE['user_tracking_id'] : null;
     }
     $ppref = '';
     if (strlen($affiliate_id) > 2) {
         $ppref = strtoupper(substr($affiliate_id, 0, 3)) . "999" . "ZZ" . "9999" . "Z";
     }
     //Track product purchase.
     try {
         CC_Product_Access::recordProductAccessRegistration($email, $sku, $ppref, $affiliate_id, $affiliate_source, $user_tracking_id, $session_tracking_id, $client_tracking_id, $url);
         if ($sprott) {
             $pubIDs = CShell::pubIDs();
             $EOM = new Email_optsModel();
             $EOM->addRecord($email, $pubIDs['sprott'], $affiliate_id, $affiliate_source, $user_tracking_id, $session_tracking_id, $client_tracking_id, $url, SystemTool::getTimestamp());
         }
     } catch (Exception $e) {
         SystemTool::sendException($e->getFile(), $e->getMessage(), $e->getTraceAsString());
         header("Location: " . PageTool::getSiteRoot() . $backto . "?e=no" . $anchor);
         exit;
     }
     //set cookie to recognize return users!
     setcookie(SystemTool::getCookieName(), base64_encode($email), PageTool::getCookieLength(), '/', CShell::cookieDomain());
     // 1yr cookie
     setcookie(CShell::GA_CONV, '1', PageTool::getCookieLength(), CShell::cookiePath(), CShell::cookieDomain());
     // 1yr cookie
     //Set up and render the email HTML.
     $eTemplate = new Template('email_sign_up');
     if ($from == "cr") {
         $eTemplate->set('cr', true);
     }
     $body = $eTemplate->render(false);
     $alt = strip_tags($body);
     //Send the email.
     $PHPEmails = CShell::emails();
     SystemTool::sendEmail($PHPEmails["registration"], $email, $body, $alt);
     //Redirect to the correct page.
     header("Location: " . PageTool::getSiteRoot() . $backto);
     exit;
 }