/** * Handle all plans for subscription. * Main goal - to stop all automailings for unsubscribed user. */ public function processUnsubscription($options) { // Check required parameters if (empty($options['subscriberId'])) { LogHelper::addError('COM_NEWSLETTER_AUTOMAILING_UNSUBSCRIPTION_SUBSCRIBER_ID_ABSENT', LogHelper::CAT_AUTOMAILING); return false; } }
/** * The main send of one letter to one or mode recipients. * The mail content generates for each user * * TODO: Need to refactor it all to: * sendNewsletterToSubscriber($nid, $sid, $options) * sentNewsletterToEmail($nid, array $emailAndName, $options) * sentLetterToEmail(array $letterData, array $emailAndName, $options) * * @param array $params newsletter_id, subscriber(object), type ('html'|'plain'), tracking(bool) * * @return object * @since 1.0 */ public function send($params = null) { // load letter to send.... if (empty($params['newsletter_id'])) { $msg = 'Newsletter id is absent. There is nothing to send.'; $this->setError($msg); throw new Exception($msg); } if (empty($params['subscriber'])) { $msg = 'Subscriber is absent. There is no one to send.'; $this->setError($msg); throw new Exception($msg); } if (empty($params['tracking'])) { $params['tracking'] = false; } // Load newsletter... $letter = JModel::getInstance('Newsletter', 'NewsletterModelEntity'); if (!$letter->load($params['newsletter_id'])) { $msg = 'Loading letter error or newsletter_id is not defined. Id:' . $params['newsletter_id']; $this->setError($msg); throw new Exception($msg); } // Load newsletter's SMTP profile... $smtpProfile = JModel::getInstance('Smtpprofile', 'NewsletterModelEntity'); if (!$smtpProfile->load($letter->smtp_profile_id)) { $msg = 'Cant load SMTP profile with id: ' . $letter->smtp_profile_id; $this->setError($msg); throw new Exception($msg); } // Load mailbox profile bound to loaded SMTP profile... $mailboxProfile = JModel::getInstance('Mailboxprofile', 'NewsletterModelEntity'); if (!$mailboxProfile->load($smtpProfile->mailbox_profile_id)) { LogHelper::addWarning('COM_NEWSLETTER_CANT_LOAD_MAILBOX_CANT_SET_SOME_HEADERS', LogHelper::CAT_MAILER, array('Mailbox profile id' => $smtpProfile->mailbox_profile_id, 'SMTP profile' => $smtpProfile->smtp_profile_name)); } // Now we have newsletter, subscriber, SMTP profile and, probably, Mailbox profile. // So we can start to send... // Use the phpMailer exceptions $sender = new MigurMailerSender(array('exceptions' => true)); $subscriber = $params['subscriber']; $type = MailHelper::filterType(!empty($params['type']) ? $params['type'] : null); if (!$type) { $msg = 'The type "' . $type . '" is not supported'; $this->setError($msg); throw new Exception($msg); } // emulate user environment SubscriberHelper::saveRealUser(); if (!SubscriberHelper::emulateUser(array('email' => $subscriber->email))) { $msg = 'The user "' . $subscriber->email . '" is absent'; $this->setError($msg); throw new Exception($msg); } PlaceholderHelper::setPlaceholder('newsletter id', $letter->newsletter_id); // render the content of letter for each user $letter->content = $this->render(array('type' => $type, 'newsletter_id' => $letter->newsletter_id, 'tracking' => true)); $letter->subject = $this->renderSubject($letter->subject); $letter->encoding = $letter->params->encoding; SubscriberHelper::restoreRealUser(); // Result object $res = new StdClass(); $res->state = false; $res->errors = array(); $res->content = $letter->content; if ($letter->content === false) { return $res; } // Add custom headers // Set the email to bounce if (!empty($mailboxProfile->username)) { $sender->AddCustomHeader('Return-Path:' . $mailboxProfile->username); $sender->AddCustomHeader('Return-Receipt-To:' . $mailboxProfile->username); $sender->AddCustomHeader('Errors-To:' . $mailboxProfile->username); } // Add info about newsleerter and subscriber $sender->AddCustomHeader(MailHelper::APPLICATION_HEADER); $sender->AddCustomHeader(MailHelper::EMAIL_NAME_HEADER . ':' . $letter->name); $sender->AddCustomHeader(MailHelper::NEWSLETTER_ID_HEADER . ':' . $params['newsletter_id']); $sender->AddCustomHeader(MailHelper::SUBSCRIBER_ID_HEADER . ':' . $subscriber->subscriber_id); // Get attachments $atts = DownloadHelper::getByNewsletterId($params['newsletter_id']); if (!$smtpProfile->isJoomlaProfile()) { $fromName = $smtpProfile->from_name; $fromEmail = $smtpProfile->from_email; $toName = $smtpProfile->reply_to_name; $toEmail = $smtpProfile->reply_to_email; } else { $jConfig = new JConfig(); $fromName = isset($letter->params->from_name) ? $letter->params->from_name : $jConfig->fromname; $fromEmail = isset($letter->params->from_email) ? $letter->params->from_email : $jConfig->mailfrom; $toName = isset($letter->params->to_name) ? $letter->params->to_name : $jConfig->fromname; $toEmail = isset($letter->params->to_email) ? $letter->params->to_email : $jConfig->mailfrom; } // Check if we dan determine all parameters... if (empty($fromName) || empty($fromEmail) || empty($toName) || empty($toEmail)) { LogHelper::addWarning('COM_NEWSLETTER_MAILER_CANT_DETERMINE SOME FROMTO', LogHelper::CAT_MAILER, array('From name' => $fromName, 'From email' => $fromEmail, 'Reply to name' => $toName, 'Reply to email' => $toEmail, 'SMTP profile' => $smtpProfile->smtp_profile_name, 'Newsletter' => $letter->name)); } try { // send the unique letter to each recipient $sendRes = $sender->send(array('letter' => $letter->toObject(), 'attach' => $atts, 'emails' => array($subscriber), 'smtpProfile' => $smtpProfile->toObject(), 'fromName' => $fromName, 'fromEmail' => $fromEmail, 'toName' => $toName, 'toEmail' => $toEmail, 'type' => $type, 'tracking' => $params['tracking'])); // If sending failed if (!$sendRes && !empty($sender->ErrorInfo)) { throw new Exception($sender->ErrorInfo); } } catch (Exception $e) { $error = JError::getError('unset'); if (!empty($error)) { $msg = $error->get('message'); $this->setError($msg); $res->errors[] = $msg; } $res->errors[] = $e->getMessage(); LogHelper::addError('COM_NEWSLETTER_MAILER_SEND_ERROR', LogHelper::CAT_MAILER, array('Error' => $e->getMessage(), 'Email' => $subscriber->email, 'Mail type' => $type, 'SMTP profile' => $smtpProfile->smtp_profile_name, 'Newsletter' => $letter->name)); return $res; } $res->state = true; return $res; }
/** * The method to unsubscribe the subscriber * from one or more lists. * * @return void * @since 1.0 */ public function unsubscribe() { // Initialise variables. $app = JFactory::getApplication(); $user = JFactory::getUser(); $db = JFactory::getDbo(); // Get variables from request $uid = JRequest::getString('newsletter-uid', ''); $nid = JRequest::getString('newsletter-nid', ''); $lists = JRequest::getVar('newsletter-lists', array()); try { // If we parameters are not enough... if (empty($uid) || empty($lists)) { throw new Exception('COM_NEWSLETTER_UNSUBSCRIPTION_FAILED_PARAMETERS_NOT_FOUND'); } // Insert into db // TODO: Add santiy checks, use model instead $db->setQuery("SELECT * FROM #__newsletter_subscribers WHERE subscription_key = " . $db->quote(addslashes($uid))); $subscriber = $db->loadObject(); if (empty($subscriber->subscriber_id)) { throw new Exception('COM_NEWSLETTER_UNSUBSCRIPTION_FAILED_SUBSCRIBER_NOT_FOUND'); } // Check the newsletter if nid is present if (!empty($nid)) { $db->setQuery("SELECT newsletter_id FROM #__newsletter_newsletters WHERE newsletter_id = " . $db->quote(addslashes($nid))); $newsletter = $db->loadObject(); if (empty($newsletter->newsletter_id)) { throw new Exception('COM_NEWSLETTER_UNSUBSCRIPTION_FAILED_NEWSLETTER_NOT_FOUND'); } } $app->triggerEvent('onMigurNewsletterBeforeUnsubscribe', array('subscriber' => $subscriber, 'lists' => $lists)); foreach ($lists as $list) { // Delete subscriptions from list $db->setQuery("DELETE FROM #__newsletter_sub_list " . "WHERE subscriber_id = " . $db->quote((int) $subscriber->subscriber_id) . " AND list_id = " . $db->quote((int) $list)); $db->query(); // Add to history $db->setQuery("INSERT IGNORE INTO #__newsletter_sub_history SET " . " newsletter_id=" . $db->quote((int) $nid) . ", " . " subscriber_id=" . $db->quote((int) $subscriber->subscriber_id) . ", " . " list_id=" . $db->quote((int) $list) . ", " . " date=" . $db->quote(date('Y-m-d H:i:s')) . ", " . " action=" . $db->quote(NewsletterTableHistory::ACTION_UNSUBSCRIBED) . ", " . " text=''"); $res = $db->query(); } // Process automailing unsubscription $amManager = new NewsletterAutomailingManager(); $amManager->processUnsubscription(array('subscriberId' => (int) $subscriber->subscriber_id)); $app->triggerEvent('onMigurNewsletterAfterUnsubscribe', array('subscriber' => $subscriber, 'lists' => $lists, 'result' => $res)); } catch (Exception $e) { // Log about this incedent $msg = $e->getMessage(); LogHelper::addError($msg, LogHelper::CAT_SUBSCRIPTION, array('Newsletter id' => $nid, 'Subscriber id' => $uid, 'Lists ids' => $lists)); $this->setRedirect(JRoute::_('index.php?option=com_newsletter&view=subscribe&layout=unsubscribe&uid=' . $uid . '&nid=' . $nid, false), JText::_($msg), 'error'); return; } // Logging for debug LogHelper::addDebug('Unsubscription complete.', LogHelper::CAT_SUBSCRIPTION, array('Newsletter id' => $nid, 'Subscriber id' => $uid, 'Lists ids' => $lists)); // Redirect to page $this->setRedirect(JRoute::_('index.php?option=com_newsletter&view=subscribe&layout=unsubscribe&uid=' . $uid . '&nid=' . $nid, false), JText::sprintf('COM_NEWSLETTER_THANK_YOU_FOR_USING_SERVICE', $subscriber->name), 'message'); }