예제 #1
0
    def get_subreddit(self):
        """checks if the current url refers to a subreddit and returns
        that subreddit object.  The cases here are:

          * the hostname is unset or is g.domain, in which case it
            looks for /r/XXXX or /reddits.  The default in this case
            is Default.
          * the hostname is a cname to a known subreddit.

        On failure to find a subreddit, returns None.
        """
        from pylons import g
        from r2.models import Subreddit, Sub, NotFound, DefaultSR
        try:
            if not self.hostname or self.hostname.startswith(g.domain):
                if self.path.startswith('/r/'):
                    return Subreddit._by_name(self.path.split('/')[2])
                elif self.path.startswith('/reddits/'):
                    return Sub
                else:
                    return DefaultSR()
            elif self.hostname:
                return Subreddit._by_domain(self.hostname)
        except NotFound:
            pass
        return None
예제 #2
0
def send_queued_mail(test=False):
    """sends mail from the mail queue to smtplib for delivery.  Also,
    on successes, empties the mail queue and adds all emails to the
    sent_mail list."""
    from r2.lib.pages import Share, Mail_Opt
    now = datetime.datetime.now(g.tz)
    if not c.site:
        c.site = DefaultSR()

    clear = False
    try:
        for email in Email.get_unsent(now):
            clear = True

            should_queue = email.should_queue()
            # check only on sharing that the mail is invalid
            if email.kind == Email.Kind.SHARE:
                if should_queue:
                    email.body = Share(username=email.from_name(),
                                       msg_hash=email.msg_hash,
                                       link=email.thing,
                                       body=email.body).render(style="email")
                else:
                    email.set_sent(rejected=True)
                    continue
            elif email.kind == Email.Kind.OPTOUT:
                email.body = Mail_Opt(msg_hash=email.msg_hash,
                                      leave=True).render(style="email")
            elif email.kind == Email.Kind.OPTIN:
                email.body = Mail_Opt(msg_hash=email.msg_hash,
                                      leave=False).render(style="email")
            # handle unknown types here
            elif not email.body:
                print("Rejecting email with an empty body from %r and to %r" %
                      (email.fr_addr, email.to_addr))
                email.set_sent(rejected=True)
                continue
            sp = SparkPostEmailProvider()
            sp.send_email(email.to_addr, email.fr_addr, email.subject,
                          email.body)
    except Exception as e:
        sp = SparkPostEmailProvider()
        sp.send_email('*****@*****.**', email.fr_addr, email.subject,
                      unicode(e))

    finally:
        pass
    # clear is true if anything was found and processed above
    if clear:
        Email.handler.clear_queue(now)
예제 #3
0
def send_queued_mail(test=False):
    """sends mail from the mail queue to smtplib for delivery.  Also,
    on successes, empties the mail queue and adds all emails to the
    sent_mail list."""
    from r2.lib.pages import Share, Mail_Opt
    now = datetime.datetime.now(g.tz)
    if not c.site:
        c.site = DefaultSR()

    clear = False
    if not test:
        session = smtplib.SMTP(g.smtp_server)
        if g.smtp_username and g.smtp_password:
            try:
                session.login(g.smtp_username, g.smtp_password)
            except (smtplib.SMTPHeloError, smtplib.SMTPAuthenticationError,
                    smtplib.SMTPException):
                print "Failed to login smtp server"
                traceback.print_exc(file=sys.stdout)
                email.set_sent(rejected=True)

    def sendmail(email):
        try:
            mimetext = email.to_MIMEText()
            if mimetext is None:
                print("Got None mimetext for email from %r and to %r" %
                      (email.fr_addr, email.to_addr))
            if test:
                print mimetext.as_string()
            else:
                session.sendmail(email.fr_addr, email.to_addr,
                                 mimetext.as_string())
                email.set_sent(rejected=False)
        # exception happens only for local recipient that doesn't exist
        except (smtplib.SMTPRecipientsRefused, smtplib.SMTPSenderRefused,
                UnicodeDecodeError, AttributeError, HeaderParseError):
            # handle error and print, but don't stall the rest of the queue
            print "Handled error sending mail (traceback to follow)"
            traceback.print_exc(file=sys.stdout)
            email.set_sent(rejected=True)

    try:
        for email in Email.get_unsent(now):
            clear = True

            should_queue = email.should_queue()
            # check only on sharing that the mail is invalid
            if email.kind == Email.Kind.SHARE:
                if should_queue:
                    email.body = Share(username=email.from_name(),
                                       msg_hash=email.msg_hash,
                                       link=email.thing,
                                       body=email.body).render(style="email")
                else:
                    email.set_sent(rejected=True)
                    continue
            elif email.kind == Email.Kind.OPTOUT:
                email.body = Mail_Opt(msg_hash=email.msg_hash,
                                      leave=True).render(style="email")
            elif email.kind == Email.Kind.OPTIN:
                email.body = Mail_Opt(msg_hash=email.msg_hash,
                                      leave=False).render(style="email")
            # handle unknown types here
            elif not email.body:
                print("Rejecting email with an empty body from %r and to %r" %
                      (email.fr_addr, email.to_addr))
                email.set_sent(rejected=True)
                continue
            sendmail(email)

    finally:
        if not test:
            session.quit()

    # clear is true if anything was found and processed above
    if clear:
        Email.handler.clear_queue(now)
예제 #4
0
파일: emailer.py 프로젝트: zeantsoi/reddit
def send_queued_mail(test=False):
    if not c.site:
        c.site = DefaultSR()

    _send_queued_mail(test)