def emit(self, record): """ Emit a record. Send the record to the specified addresses """ # the app config is accessible after logging is initialized, so set the # arguments and make the decision to send mail or not here from flask import current_app as app try: email_tracebacks = app.cfg.email_tracebacks except RuntimeError: # likely: RuntimeError: working outside of application context # if we get that, we can't access the cfg and can't send mail anyway. email_tracebacks = False if not email_tracebacks: return if self.in_email_handler: return self.in_email_handler = True try: toaddrs = self.toaddrs if self.toaddrs else app.cfg.admin_emails log_level = logging.getLevelName(self.level) subject = self.subject if self.subject else '[{0}][{1}] Log message'.format( app.cfg.sitename, log_level) msg = self.format(record) from moin.mail.sendmail import sendmail sendmail(subject, msg, to=toaddrs) finally: self.in_email_handler = False
def send_notifications(app, fqname, action, data=None, meta=None, new_data=None, new_meta=None, **kwargs): """ Send mail notifications to subscribers on item change :param app: local proxy app :param fqname: fqname of the changed item :param action: type of modification - save, rename, destroy... :param data: the item's data, None if item is new :param meta: the item's meta data, None if item is new :param new_data: open file with new data, None if action is delete or destroy :param new_meta: new meta data, None if action is delete or destroy :param kwargs: optional comment """ if new_meta is None: subscribers = {subscriber for subscriber in get_subscribers(**meta) if subscriber.itemid != flaskg.user.itemid} else: subscribers = {subscriber for subscriber in get_subscribers(**new_meta) if subscriber.itemid != flaskg.user.itemid} if not subscribers: return notification = Notification(app, fqname, action, data, meta, new_data, new_meta, **kwargs) try: content_diff = notification.get_content_diff() except Exception: # current user has likely corrupted an item or fixed a corrupted item # if current item is corrupt, another exception will occur in a downstream script content_diff = ['- ' + _('An error has occurred, the current or prior revision of this item may be corrupt.')] meta_diff = notification.get_meta_diff() subscribers_locale = {subscriber.locale for subscriber in subscribers} for locale in subscribers_locale: with force_locale(locale): txt_msg, html_msg = notification.render_templates(content_diff, meta_diff) subject = _('[%(moin_name)s] Update of "%(fqname)s" by %(user_name)s', moin_name=app.cfg.interwikiname, fqname=str(fqname), user_name=flaskg.user.name0) subscribers_emails = [subscriber.email for subscriber in subscribers if subscriber.locale == locale] sendmail(subject, txt_msg, to=subscribers_emails, html=html_msg)
def send_notifications(app, fqname, **kwargs): """ Send mail notifications to subscribers on item change :param app: local proxy app :param fqname: fqname of the changed item :param kwargs: key/value pairs that contain extra information about the item required in order to create a notification """ action = kwargs.get('action') revs = get_item_last_revisions(app, fqname) if action not in [ DESTROY_REV, DESTROY_ALL, ] else [] notification = Notification(app, fqname, revs, **kwargs) try: content_diff = notification.get_content_diff() except Exception: # current user has likely corrupted an item or fixed a corrupted item # or changed ACL and removed read access for himself # if current item is corrupt, another exception will occur in a downstream script content_diff = [ u'- ' + _('An error has occurred, the current or prior revision of this item may be corrupt.' ) ] meta_diff = notification.get_meta_diff() u = flaskg.user meta = kwargs.get('meta') if action in [ DESTROY_REV, DESTROY_ALL, ] else revs[0].meta._meta subscribers = { subscriber for subscriber in get_subscribers(**meta) if subscriber.itemid != u.itemid } subscribers_locale = {subscriber.locale for subscriber in subscribers} for locale in subscribers_locale: with force_locale(locale): txt_msg, html_msg = notification.render_templates( content_diff, meta_diff) subject = L_( '[%(moin_name)s] Update of "%(fqname)s" by %(user_name)s', moin_name=app.cfg.interwikiname, fqname=unicode(fqname), user_name=u.name0) subscribers_emails = [ subscriber.email for subscriber in subscribers if subscriber.locale == locale ] sendmail(subject, txt_msg, to=subscribers_emails, html=html_msg)
def mail_password_recovery(self, cleartext_passwd=None, subject=None, text=None): """ Mail a user who forgot his password a message enabling him to login again. """ if not self.email: return False, "user has no E-Mail address in his profile." token = self.generate_recovery_token() if subject is None: subject = _('[%(sitename)s] Your wiki password recovery link', sitename='%(sitename)s') subject = subject % dict(sitename=self._cfg.sitename or "Wiki") if text is None: link = url_for('frontend.recoverpass', username=self.name0, token=token, _external=True) text = render_template('mail/password_recovery.txt', link=link) mailok, msg = sendmail.sendmail(subject, text, to=[self.email], mail_from=self._cfg.mail_from) return mailok, msg
def mail_email_verification(self): """ Mail a user a link to verify his email address. """ token = self.generate_recovery_token() link = url_for('frontend.verifyemail', username=self.name0, token=token, _external=True) text = render_template('mail/account_verification.txt', link=link) subject = _('[%(sitename)s] Please verify your email address', sitename=self._cfg.sitename or "Wiki") email = self.profile[EMAIL_UNVALIDATED] mailok, msg = sendmail.sendmail(subject, text, to=[email], mail_from=self._cfg.mail_from) return mailok, msg