def send_gazette(self): context = aq_inner(self.context) parent = aq_parent(context) now = datetime.now() CheckAuthenticator(self.request) ptool = getToolByName(self.context, 'plone_utils') soup = getSoup(self.context, config.SUBSCRIBERS_SOUP_ID) providers = self._providers() subject = context.Title() url = parent.absolute_url() + '/subscription?uuid=%(uuid)s' footer_text = parent.footer.output.replace('${url}', '$url') footer_text = footer_text.replace('$url', url) count = 0 text = context.text.output + '\n' for p in providers: text += p.get_gazette_text(parent, context) for s in soup.query(active=True): # returns email and fullname taken from memberdata if s.username is set and member exists subscriber_info = s.get_info(context) footer = footer_text % subscriber_info mail_text = "" if subscriber_info['salutation']: mail_text += "%s<br /><br />" % subscriber_info['salutation'] mail_text += "%s------------<br />%s" % (text, footer) try: if utils.send_mail(context, None, subscriber_info['email'], subscriber_info['fullname'], subject, mail_text): count += 1 except (SMTPException, SMTPRecipientsRefused): pass context.sent_at = now parent.most_recent_issue = context ptool.addPortalMessage(_(u'Gazette has been sent to $count recipients', mapping={'count': count})) self.request.response.redirect(context.absolute_url())
def test_send(self): context = aq_inner(self.context) parent = aq_parent(context) email = parent.test_mail ptool = getToolByName(self.context, 'plone_utils') if not email: ptool.addPortalMessage(_(u'No test email set. Please check Gazette folder settings.'), 'error') else: text = context.text.output + '\n' providers = self._providers() for p in providers: text += p.get_gazette_text(parent, context) subject = context.Title() url = parent.absolute_url() + '/subscription?uuid=' # NOT SET - just testing footer_text = parent.footer.output.replace('${url}', '$url') footer_text = footer_text.replace('$url', url) footer = footer_text mail_text = "%s------------<br />%s" % (text, footer) try: utils.send_mail(context, None, email, 'Tester', subject, mail_text) except (SMTPException, SMTPRecipientsRefused): pass ptool.addPortalMessage(_(u'Gazette test has been sent to $email', mapping={'email': email})) self.request.response.redirect(context.absolute_url())
def deactivation_mail(self, subscriber): mail_text = IStringInterpolator(self.context)(self.deactivation_template(key=subscriber.key)) subject = _(u"Please confirm deactivation of your subscription") utils.send_mail(self.context, None, subscriber.email, subscriber.fullname, subject, mail_text)
def render(self, test_mode=False, html_only=False): """ This method does all the hard work """ context = aq_inner(self.context) if not context.auto_enabled: return 'N/A' now = datetime.now() wtool = getToolByName(context, 'portal_workflow') soup = getSoup(self.context, config.SUBSCRIBERS_SOUP_ID) # strftime accepts any text, not only strftime characters subject = now.strftime(context.auto_subject.encode('utf-8')) url = context.absolute_url() + '/subscription?uuid=%(uuid)s' footer_text = context.footer.output.replace('${url}', '$url') footer_text = footer_text.replace('$url', url) count = 0 base_text = '' if context.auto_text: base_text += now.strftime(context.auto_text.output.encode('utf-8')) + '\n' providers = self._providers() gid = 'issue-%s' % now.strftime("%Y-%m-%d-%H-%M-%S.%f") idx = 0 while context.check_id(gid): # python script in skins idx += 1 gid = 'issue-%s-%d' % (now.strftime("%Y-%m-%d-%H-%M-%S.%f"), idx) # create anonymous issue text to be stored to portal text = safe_unicode(base_text) auto_text = u'' provider_names = [] for p in providers: auto_text += safe_unicode(p.get_gazette_text(context, None)) provider_names.append(repr(p)) if not auto_text: # There is no automatically geenrated text. Discard sending of newsletter. return 'Nothing to send' text = text + auto_text # Create PDF version of the newsletter using wkhtml2pdf as archive of the issue pdf_raw = self.make_pdf(text, html_only) if not pdf_raw: logger.warning('Unable to create PDF of automatically issued gazette.') if not test_mode: # create Gazette object representing this issue gid = context.invokeFactory('gazette.GazetteIssue', gid) gazette = context[gid] # Fill the newly create Gazette object with generated data gazette.title = subject gazette.text = RichTextValue(text, mimeType='text/html', outputMimeType='text/html') gazette.providers = provider_names gazette.sent_at = now try: # ignore if there is no publish option for now wtool.doActionFor(gazette, 'publish') except: pass # Attach PDF to gazette but only if it is not HTML only mode if pdf_raw and not html_only: fid = gazette.invokeFactory('File', gid + '.pdf') file_pdf = gazette[fid] file_pdf.setTitle(gazette.title) file_pdf.setFile(pdf_raw, mimetype='application/pdf') file_pdf.processForm() for s in soup.query(active=True): # returns email and fullname taken from memberdata if s.username is set and member exists subscriber_info = s.get_info(context) footer = footer_text % subscriber_info mail_text = "" if subscriber_info['salutation']: mail_text += "%s<br /><br />" % subscriber_info['salutation'] mail_text += "%s------------<br />%s" % (text, footer) try: if utils.send_mail(context, None, subscriber_info['email'], subscriber_info['fullname'], subject, mail_text): count += 1 except (SMTPException, SMTPRecipientsRefused): pass context.most_recent_issue = gazette else: if html_only: self.request.response.setHeader('Content-Type', 'text/html;charset=utf-8') else: self.request.response.setHeader('Content-Type', 'application/pdf') return pdf_raw return str(count)