public function UnsubscribeRequestForm() { $fields = new FieldList(EmailField::create('email', _t("Newsletter.UnsubscribeEmail", "Your subscription email address"))); $actions = new FieldList(FormAction::create('sendLink', _t('Newsletter.SendUnsubscribeLink', 'Send unsubscribe link'))->addExtraClass('ss-ui-action-constructive'), Object::create('ResetFormAction', 'clear', _t('CMSMain_left.ss.RESET', 'Reset'))); $unsubscribeController = new UnsubscribeController(); $form = NewsletterContentControllerExtension::getUnsubscribeFormObject($this, $fields, $actions); $form->setFormMethod('GET'); $form->setFormAction(Controller::join_links(Director::absoluteBaseURL(), $unsubscribeController->relativeLink('sendUnsubscribeLink'))); $form->addExtraClass('cms-search-form'); return $form; }
function done() { $form = new Form($this, "UnsubscribeSuccess", new FieldSet(), new FieldSet()); if (!self::$done_message) { $email = $this->getMember()->Email; $mailingList = $this->getMailingList(); if (is_a($mailingList, "DataObjectSet")) { $nlTitles = array(); foreach ($mailingList as $nlType) { $nlTitles[] = $nlType->Title; } $nlTypes = implode(", ", $nlTitles); } elseif (is_a($mailingList, "NewsletterType")) { $nlTypes = $mailingList->Title; } self::$done_message = sprintf(_t('Unsubscribe.REMOVESUCCESS', 'Thank you. %s will no longer receive the %s.'), $email, $nlTypes); } $form->setMessage(self::$done_message, 'good'); self::$done_message = null; return $this->customise(array('Title' => _t('UNSUBSCRIBEDTITLE', 'Unsubscribed'), 'Form' => $form))->renderWith('Page'); }
/** Send an email with a link to unsubscribe from all this user's newsletters */ public function sendUnsubscribeLink(SS_HTTPRequest $request) { //get the form object (we just need its name to set the session message) $form = NewsletterContentControllerExtension::getUnsubscribeFormObject($this); $email = Convert::raw2sql($request->requestVar('email')); $recipient = Recipient::get()->filter('Email', $email)->First(); if ($recipient) { //get the IDs of all the Mailing Lists this user is subscribed to $lists = $recipient->MailingLists()->column('ID'); $listIDs = implode(',', $lists); $days = UnsubscribeController::get_days_unsubscribe_link_alive(); if ($recipient->ValidateHash) { $recipient->ValidateHashExpired = date('Y-m-d H:i:s', time() + 86400 * $days); $recipient->write(); } else { $recipient->generateValidateHashAndStore($days); } $templateData = array('FirstName' => $recipient->FirstName, 'UnsubscribeLink' => Director::absoluteBaseURL() . "unsubscribe/index/" . $recipient->ValidateHash . "/{$listIDs}"); //send unsubscribe link email $email = new Email(); $email->setTo($recipient->Email); $from = Email::getAdminEmail(); $email->setFrom($from); $email->setTemplate('UnsubscribeLinkEmail'); $email->setSubject(_t('Newsletter.ConfirmUnsubscribeSubject', "Confirmation of your unsubscribe request")); $email->populateTemplate($templateData); $email->send(); $form->sessionMessage(_t('Newsletter.GoodEmailMessage', 'You have been sent an email containing an unsubscribe link'), "good"); } else { //not found Recipient, just reload the form $form->sessionMessage(_t('Newsletter.BadEmailMessage', 'Email address not found'), "bad"); } Controller::curr()->redirectBack(); }
public function subscribeverify() { if ($hash = $this->urlParams['ID']) { $recipient = DataObject::get_one("Recipient", "\"ValidateHash\" = '" . Convert::raw2sql($hash) . "'"); if ($recipient && $recipient->exists()) { $now = date('Y-m-d H:i:s'); if ($now <= $recipient->ValidateHashExpired) { $recipient->Verified = true; // extends the ValidateHashExpired so the a unsubscirbe link will stay alive in that peroid by law $days = UnsubscribeController::get_days_unsubscribe_link_alive(); $recipient->ValidateHashExpired = date('Y-m-d H:i:s', time() + 86400 * $days); $recipient->write(); $mailingLists = $recipient->MailingLists(); $ids = implode(",", $mailingLists->getIDList()); $templateData = array('FirstName' => $recipient->FirstName, 'MailingLists' => $mailingLists, 'UnsubscribeLink' => Director::BaseURL() . "unsubscribe/index/" . $recipient->ValidateHash . "/" . $ids, 'HashText' => $recipient->getHashText(), 'SiteConfig' => $this->SiteConfig()); //send notification email if ($this->SendNotification) { $email = new Email(); $email->setTo($recipient->Email); $from = $this->NotificationEmailFrom ? $this->NotificationEmailFrom : Email::getAdminEmail(); $email->setFrom($from); $email->setTemplate('SubscriptionConfirmationEmail'); $email->setSubject(_t('Newsletter.ConfirmSubject', "Confirmation of your subscription to our mailing lists")); $email->populateTemplate($templateData); $email->send(); } $url = $this->Link('completed') . "/" . $recipient->ID; $this->redirect($url); } } if ($recipient && $recipient->exists()) { $recipientData = $recipient->toMap(); } else { $recipientData = array(); } $daysExpired = SubscriptionPage::get_days_verification_link_alive(); $recipientData['VerificationExpiredContent1'] = sprintf(_t('Newsletter.VerificationExpiredContent1', 'The verification link is only validate for %s days.'), $daysExpired); return $this->customise(array('Title' => _t('Newsletter.VerificationExpired', 'The verification link has been expired'), 'Content' => $this->customise($recipientData)->renderWith('VerificationExpired')))->renderWith('Page'); } }
function UnsubscribeLink() { if ($this->recipient && !$this->fakeRecipient) { //the unsubscribe link is for all MaillingLists that the Recipient is subscribed to, intersected with a //list of all MaillingLists to which the Email was sent $recipientLists = $this->recipient->MailingLists()->column('ID'); $sendLists = $this->mailinglists->column('ID'); $lists = array_intersect($recipientLists, $sendLists); $listIDs = implode(',', $lists); $days = UnsubscribeController::get_days_unsubscribe_link_alive(); if ($this->recipient->ValidateHash) { $this->recipient->ValidateHashExpired = date('Y-m-d H:i:s', time() + 86400 * $days); $this->recipient->write(); } else { $this->recipient->generateValidateHashAndStore($days); } if ($static_base_url = self::get_static_base_url()) { $base_url_changed = true; $base_url = Config::inst()->get('Director', 'alternate_base_url'); Config::inst()->update('Director', 'alternate_base_url', $static_base_url); } else { $base_url_changed = false; } $link = Director::absoluteBaseURL() . "unsubscribe/index/" . $this->recipient->ValidateHash . "/{$listIDs}"; if ($base_url_changed) { // remove our alternative base URL Config::inst()->update('Director', 'alternate_base_url', $base_url); } return $link; } else { $listIDs = implode(",", $this->mailinglists->getIDList()); return Director::absoluteBaseURL() . "unsubscribe/index/fackedvalidatehash/{$listIDs}"; } }