Пример #1
0
def send_happy_messages():
    with app.app_context():
        app.logger.info("Sending happy messages")
        count = 0
        personality = Personality('happybot', 'happybot/personalities/happybot.txt')
        for entry in MessageSchedule.query.filter(MessageSchedule.msg_time < datetime.datetime.now().time()).all():
            subscription = Subscription.query.filter_by(id=entry.user_id).first()
            subject = Text.message_subject.format(subscription.user_name)
            sender = (subscription.sender_name, app.extensions['mail'].default_sender)
            msg = Message(subject, sender=sender, recipients=[subscription.user_email])
            unsub_url = url_for('unsubscribe', code=subscription.confirmation_code, _external=True)
            home_url = url_for('index', _external=True)
            message = personality.get_message()
            msg.html = render_template('happy_message.html', user_name=subscription.user_name, message=message,
                                       sender_name=subscription.sender_name, home_url=home_url,
                                       unsubscribe_url=unsub_url)
            msg.body = render_template('happy_message.txt', user_name=subscription.user_name, message=message,
                                        sender_name=subscription.sender_name, home_url=home_url,
                                        unsubscribe_url=unsub_url)

            try:
                mail.send(msg)
                count += 1
            except SMTPException, e:
                app.logger.error("Sending happy message failed with '" + e.message + "'")
            db.session.delete(entry)
        db.session.commit()
        app.logger.info("Number of messages sent: {0}".format(count))
Пример #2
0
def send_confirm_email(user_data):
    with app.app_context():
        app.logger.info("Sending confirmation to " + user_data['user_email'])
        sender = ("HappyBot", app.extensions['mail'].default_sender)
        msg = Message(Text.confirm_subject, sender=sender, recipients=[user_data['user_email']])
        confirm_url = url_for('confirm', code=user_data['confirmation_code'], _external=True)
        home_url = url_for('index', _external=True)
        msg.html = render_template('confirm_email.html', sender_name=user_data['sender_name'], confirm_url=confirm_url,
                                   home_url=home_url)
        msg.body = render_template('confirm_email.txt', sender_name=user_data['sender_name'], confirm_url=confirm_url,
                                   home_url=home_url)
        try:
            mail.send(msg)
        except SMTPException, e:
            app.logger.error("Sending confirmation mail failed with '" + e.message + "'")