示例#1
0
    def send(self):
        if not self.recipients:
            raise Exception("No email recipients set.")
        if not self.subject:
            raise Exception("No email subject set.")
        if not self.content:
            raise Exception("No email content set.")

        if app.config["MAIL_ENABLED"]:
            if not app.config["MAIL_SENDER"]:
                raise Exception("No email sender set in config (MAIL_SENDER).")

            with mail.connect() as connection:
                for recipient in self.recipients:
                    msg = Message(self.subject, recipients = [recipient], sender = app.config["MAIL_SENDER"])
                    msg.html = self.content
                    connection.send(msg)
        else:
            print "Sending mail to: "
            for p in self.recipients:
                print "  - " + p
            print "=" * 40
            print "Mail Content:"
            print "=" * 40
            print self.content
            print "=" * 40
示例#2
0
文件: jam.py 项目: ThYpHo0n/flamejam
    def send_notification(self, n):
        if not JamStatusCode.ANNOUNCED <= n <= JamStatusCode.FINISHED:
            return False

        kwargs = {}

        if n == JamStatusCode.ANNOUNCED:
            template = "announcement"
            notify = "new_jam"
            subject = f"Jam announced: {self.title}"
        elif n == JamStatusCode.REGISTRATION:
            template = "registration_start"
            notify = "new_jam"
            subject = f"Registrations for {self.title} now open"
        elif n == JamStatusCode.RUNNING:
            template = "start"
            notify = "jam_start"
            subject = f"{self.title} starts now!"
        elif n == JamStatusCode.PACKAGING:
            template = "packaging_start"
            notify = "jam_finish"
            subject = f"{self.title} is over"
        elif n == JamStatusCode.RATING:
            template = "rating_start"
            notify = "jam_finish"
            subject = f"Rating for {self.title} starts now"
        elif n == JamStatusCode.FINISHED:
            template = "finished"
            notify = "jam_finish"
            subject = f"Rating for {self.title} finished - Winners"
            kwargs = {"games": self.games_by_score()[:3]}

        if n >= JamStatusCode.RUNNING and n != JamStatusCode.RATING:
            users = [r.user for r in self.participations]
        else:
            from flamejam.models import User
            users = User.query.all()

        # Set this first because we might send for longer than a minute at which point the
        # next tick will come around.
        self.last_notification_sent = n
        db.session.commit()

        subject = app.config["LONG_NAME"] + ": " + subject

        with mail.connect() as conn:
            for user in users:
                if getattr(user, "notify_" + notify):
                    body = render_template("emails/jam/" + template + ".txt",
                                           recipient=user, jam=self, **kwargs)
                    sender = app.config['MAIL_DEFAULT_SENDER']
                    recipients = [user.email]
                    message = Message(subject=subject, sender=sender,
                                      body=body, recipients=recipients)
                    try:
                        conn.send(message)
                    except SMTPRecipientsRefused:
                        pass
        return True
示例#3
0
def admin_announcement():
    form = AdminWriteAnnouncement()

    if form.validate_on_submit():
        with mail.connect() as conn:
            for user in User.query.filter_by(notify_newsletter=True).all():
                body = render_template("emails/newsletter.txt", recipient=user, message=form.message.data)
                subject = app.config["LONG_NAME"] + " Newsletter: " + form.subject.data
                sender = app.config['MAIL_DEFAULT_SENDER']
                recipients = [user.email]
                message = Message(subject=subject, sender=sender, body=body, recipients=recipients)
                try:
                    conn.send(message)
                except SMTPRecipientsRefused:
                    pass
        flash("Your announcement has been sent to the users.")

    return render_template("admin/announcement.html", form=form)
示例#4
0
def admin_announcement():
    form = AdminWriteAnnouncement()

    if form.validate_on_submit():
        with mail.connect() as conn:
            for user in User.query.filter_by(notify_newsletter = True).all():
                body = render_template("emails/newsletter.txt", recipient=user, message=form.message.data)
                subject = app.config["LONG_NAME"] + " Newsletter: " + form.subject.data
                sender = app.config['MAIL_DEFAULT_SENDER']
                recipients = [user.email]
                message = Message(subject=subject, sender=sender, body=body, recipients=recipients)
                try:
                    conn.send(message)
                except SMTPRecipientsRefused:
                    pass
        flash("Your announcement has been sent to the users.")

    return render_template("admin/announcement.html", form = form)
示例#5
0
文件: jam.py 项目: imfht/flaskapps
    def send_notification(self, n):
        if not JamStatusCode.ANNOUNCED <= n <= JamStatusCode.FINISHED:
            return False

        kwargs = {}

        if n == JamStatusCode.ANNOUNCED:
            template = "announcement"
            notify = "new_jam"
            subject = f"Jam announced: {self.title}"
        elif n == JamStatusCode.REGISTRATION:
            template = "registration_start"
            notify = "new_jam"
            subject = f"Registrations for {self.title} now open"
        elif n == JamStatusCode.RUNNING:
            template = "start"
            notify = "jam_start"
            subject = f"{self.title} starts now!"
        elif n == JamStatusCode.PACKAGING:
            template = "packaging_start"
            notify = "jam_finish"
            subject = f"{self.title} is over"
        elif n == JamStatusCode.RATING:
            template = "rating_start"
            notify = "jam_finish"
            subject = f"Rating for {self.title} starts now"
        elif n == JamStatusCode.FINISHED:
            template = "finished"
            notify = "jam_finish"
            subject = f"Rating for {self.title} finished - Winners"
            kwargs = {"games": self.games_by_score()[:3]}

        if n >= JamStatusCode.RUNNING and n != JamStatusCode.RATING:
            users = [r.user for r in self.participations]
        else:
            from flamejam.models.user import User
            users = User.query.all()

        # Set this first because we might send for longer than a minute at which point the
        # next tick will come around.
        self.last_notification_sent = n
        db.session.commit()

        subject = app.config["LONG_NAME"] + ": " + subject

        with mail.connect() as conn:
            for user in users:
                if getattr(user, "notify_" + notify):
                    body = render_template("emails/jam/" + template + ".txt",
                                           recipient=user,
                                           jam=self,
                                           **kwargs)
                    sender = app.config['MAIL_DEFAULT_SENDER']
                    recipients = [user.email]
                    message = Message(subject=subject,
                                      sender=sender,
                                      body=body,
                                      recipients=recipients)
                    try:
                        conn.send(message)
                    except SMTPRecipientsRefused:
                        pass
        return True