def update(self): request = self.request if "forum.button.unsubscribe" in request: pids = request.get("pids", ()) if not pids: IStatusMessage(request).add(_("Please select principals.")) else: auth = getUtility(IAuthentication) subs = ISubscribersManagement(self.context) for pid in pids: subs.unsubscribePrincipal(auth.getPrincipal(pid)) IStatusMessage(request).add(_("Selected principals have been unsubscribed."))
def update(self): super(SendNewsletter, self).update() if 'form.send' in self.request: subs = ISubscribersManagement(self.context.__parent__) emails = [] for principal in subs.getSubscribers(): mail = IMailAddress(principal, None) if mail is not None: email = mail.address if email: emails.append( formataddr((principal.title or principal.id, email))) if emails: message = self.generateMessage() message['Subject'] = make_header(((self.context.title, 'utf-8'),)) mailer = getUtility(IMailer) from_address = str(formataddr( (mailer.email_from_name, mailer.email_from_address))) message['From'] = from_address mailer.send(from_address, emails, message.as_string()) IStatusMessage(self.request).add(_('Newsletter has been sent.'))
def update(self): request = self.request product = getUtility(INewsletterProduct) if 'subscribe' in request: newsletter = product.get(request.get('subscribe', '')) if newsletter is not None: subs = ISubscription(newsletter) subs.subscribe() IStatusMessage(request).add( _('You have been subscribed to newsletter.')) return self.redirect('./') if 'unsubscribe' in request: newsletter = product.get(request.get('unsubscribe', '')) if newsletter is not None: subs = ISubscription(newsletter) subs.unsubscribe() IStatusMessage(request).add( _('You have been unsubscribed from newsletter.')) return self.redirect('./') newsletters = [] for key, nl in product.items(): subs = ISubscription(nl) newsletters.append( (nl.title, {'id': nl.__name__, 'title': nl.title, 'description': nl.description, 'subscribed': subs.isSubscribed(), 'newsletter': nl})) newsletters.sort() self.newsletters = [nl for title, nl in newsletters]
def listSubscribers(self): subs = ISubscribersManagement(self.context) subscriptions = [] for principal in subs.getSubscribers(): mail = IMailAddress(principal, None) if mail is not None: email = mail.address else: email = _("Unknown") data = {"id": principal.id, "title": principal.title, "email": email} subscriptions.append((data["title"], data)) subscriptions.sort() return [data for t, data in subscriptions]
def principalSubscribedEvent(newsletter, event): request = getRequest() if request is None: return principal = event.principal mail = IMailAddress(principal, None) if mail is not None: template = SubscriptionTemplate(newsletter, request) template.update() try: template.send((mail.address,), principal=principal) except: IStatusMessage(request).add( _("Can't send confirmation email."), 'warning')
def handleSendTesting(self, action): data, errors = self.extractData() if errors: IStatusMessage(self.request).add( (self.formErrorsMessage,) + errors, 'formError') else: message = self.generateMessage() message['Subject'] = make_header( ((u'Test message: %s'%self.context.title, 'utf-8'),)) mailer = getUtility(IMailer) from_address = formataddr( (mailer.email_from_name, mailer.email_from_address)) mailer.send(from_address, (data['email'],), message.as_string()) IStatusMessage(self.request).add( _('Test mail for newsletter has been sent.'))