def send_post_to_user(community, post, member): recipient = member.email subject = "[{}] {}".format(community.name, post.title) config = current_app.config sender = config.get("BULK_MAIL_SENDER", config["MAIL_SENDER"]) SBE_FORUM_REPLY_BY_MAIL = config.get("SBE_FORUM_REPLY_BY_MAIL", False) SERVER_NAME = config.get("SERVER_NAME", "example.com") list_id = '"{} forum" <forum.{}.{}>'.format(community.name, community.slug, SERVER_NAME) forum_url = url_for("forum.index", community_id=community.slug, _external=True) forum_archive = url_for("forum.archives", community_id=community.slug, _external=True) extra_headers = { "List-Id": list_id, "List-Archive": "<{}>".format(forum_archive), "List-Post": "<{}>".format(forum_url), "X-Auto-Response-Suppress": "All", "Auto-Submitted": "auto-generated", } if SBE_FORUM_REPLY_BY_MAIL and config["MAIL_ADDRESS_TAG_CHAR"] is not None: name = sender.rsplit("@", 1)[0] domain = sender.rsplit("@", 1)[1] replyto = build_reply_email_address(name, post, member, domain) msg = Message( subject, sender=sender, recipients=[recipient], reply_to=replyto, extra_headers=extra_headers, ) else: msg = Message(subject, sender=sender, recipients=[recipient], extra_headers=extra_headers) ctx = { "community": community, "post": post, "member": member, "MAIL_REPLY_MARKER": MAIL_REPLY_MARKER, "SBE_FORUM_REPLY_BY_MAIL": SBE_FORUM_REPLY_BY_MAIL, } msg.body = render_template_i18n("forum/mail/new_message.txt", **ctx) msg.html = render_template_i18n("forum/mail/new_message.html", **ctx) logger.debug("Sending new post by email to %r", member.email) try: mail.send(msg) except BaseException: logger.error("Send mail to user failed", exc_info=True) # log to sentry if enabled
def send_post_to_user(community, post, member): recipient = member.email subject = f"[{community.name}] {post.title}" config = current_app.config SENDER = config.get("BULK_MAIL_SENDER", config["MAIL_SENDER"]) SBE_FORUM_REPLY_BY_MAIL = config.get("SBE_FORUM_REPLY_BY_MAIL", False) SBE_FORUM_REPLY_ADDRESS = config.get("SBE_FORUM_REPLY_ADDRESS", SENDER) SERVER_NAME = config.get("SERVER_NAME", "example.com") list_id = '"{} forum" <forum.{}.{}>'.format( community.name, community.slug, SERVER_NAME ) forum_url = url_for("forum.index", community_id=community.slug, _external=True) forum_archive_url = url_for( "forum.archives", community_id=community.slug, _external=True ) extra_headers = { "List-Id": list_id, "List-Archive": f"<{forum_archive_url}>", "List-Post": f"<{forum_url}>", "X-Auto-Response-Suppress": "All", "Auto-Submitted": "auto-generated", } if SBE_FORUM_REPLY_BY_MAIL: name = SBE_FORUM_REPLY_ADDRESS.rsplit("@", 1)[0] domain = SBE_FORUM_REPLY_ADDRESS.rsplit("@", 1)[1] replyto = build_reply_email_address(name, post, member, domain) msg = Message( subject, sender=SBE_FORUM_REPLY_ADDRESS, recipients=[recipient], reply_to=replyto, extra_headers=extra_headers, ) else: msg = Message( subject, sender=SENDER, recipients=[recipient], extra_headers=extra_headers ) ctx = { "community": community, "post": post, "member": member, "MAIL_REPLY_MARKER": MAIL_REPLY_MARKER, "SBE_FORUM_REPLY_BY_MAIL": SBE_FORUM_REPLY_BY_MAIL, } msg.body = render_template_i18n("forum/mail/new_message.txt", **ctx) msg.html = render_template_i18n("forum/mail/new_message.html", **ctx) logger.debug("Sending new post by email to %r", member.email) try: mail.send(msg) except BaseException: # log to sentry if enabled logger.error("Send mail to user failed", exc_info=True)
def document_send(doc_id): doc = get_document(doc_id) recipient = request.form.get("recipient") user_msg = request.form.get("message") site_name = "[{}] ".format(current_app.config["SITE_NAME"]) sender_name = current_user.name subject = site_name + _("{sender} sent you a file").format( sender=sender_name) msg = Message(subject) msg.sender = current_user.email msg.recipients = [recipient] msg.body = render_template_i18n( "documents/mail_file_sent.txt", sender_name=sender_name, message=user_msg, document_url=url_for(doc), filename=doc.title, ) filename = doc.title msg.attach(filename, doc.content_type, doc.content) mail.send(msg) flash(_("Email successfully sent"), "success") return redirect(url_for(doc))
def document_send(doc_id): doc = get_document(doc_id) recipient = request.form.get("recipient") user_msg = request.form.get("message") site_name = "[{}] ".format(current_app.config["SITE_NAME"]) sender_name = current_user.name subject = site_name + _("{sender} sent you a file").format(sender=sender_name) msg = Message(subject) msg.sender = current_user.email msg.recipients = [recipient] msg.body = render_template_i18n( "documents/mail_file_sent.txt", sender_name=sender_name, message=user_msg, document_url=url_for(doc), filename=doc.title, ) filename = doc.title msg.attach(filename, doc.content_type, doc.content) mail.send(msg) flash(_("Email successfully sent"), "success") return redirect(url_for(doc))
def unsubscribe_sbe(token): expired, invalid, user = get_token_status(token, TOKEN_SERIALIZER_NAME) if expired or invalid: return render_template_i18n("notifications/invalid-token.html") if request.method == 'GET': return render_template_i18n("notifications/confirm-unsubscribe.html", token=token) elif request.method == 'POST': preferences = app.services['preferences'] preferences.set_preferences(user, **{'sbe:notifications:daily': False}) db.session.commit() return render_template_i18n("notifications/unsubscribed.html", token=token) else: raise MethodNotAllowed()
def unsubscribe_sbe(token): expired, invalid, user = get_token_status(token, TOKEN_SERIALIZER_NAME) if expired or invalid: return render_template_i18n("notifications/invalid-token.html") if request.method == "GET": return render_template_i18n( "notifications/confirm-unsubscribe.html", token=token ) elif request.method == "POST": preferences = app.services["preferences"] preferences.set_preferences(user, **{"sbe:notifications:daily": False}) db.session.commit() return render_template_i18n("notifications/unsubscribed.html", token=token) else: raise MethodNotAllowed()
def reset_password(token): expired, invalid, user = reset_password_token_status(token) if invalid: flash(_("Invalid reset password token."), "error") elif expired: flash(_("Password reset expired"), "error") if invalid or expired: return redirect(url_for("login.forgotten_pw")) return render_template_i18n("login/password_reset.html")
def send_mail(subject, recipient, template, **context): """Send an email using the Flask-Mail extension. :param subject: Email subject :param recipient: Email recipient :param template: The name of the email template :param context: The context to render the template with """ config = current_app.config sender = config["MAIL_SENDER"] msg = Message(subject, sender=sender, recipients=[recipient]) template_name = "login/email/{}.txt".format(template) msg.body = render_template_i18n(template_name, **context) # msg.html = render_template('%s/%s.html' % ctx, **context) mail = current_app.extensions.get("mail") current_app.logger.debug("Sending mail...") mail.send(msg)
def send_mail(subject, recipient, template, **context): """Send an email using the Flask-Mail extension. :param subject: Email subject :param recipient: Email recipient :param template: The name of the email template :param context: The context to render the template with """ config = current_app.config sender = config["MAIL_SENDER"] msg = Message(subject, sender=sender, recipients=[recipient]) template_name = f"login/email/{template}.txt" msg.body = render_template_i18n(template_name, **context) # msg.html = render_template('%s/%s.html' % ctx, **context) mail = current_app.extensions.get("mail") current_app.logger.debug("Sending mail...") mail.send(msg)
def make_message(user): config = current_app.config sender = config.get("BULK_MAIL_SENDER", config["MAIL_SENDER"]) sbe_config = config["ABILIAN_SBE"] subject = sbe_config["DAILY_SOCIAL_DIGEST_SUBJECT"] recipient = user.email digests = [] happened_after = datetime.utcnow() - timedelta(days=1) list_id = '"{} daily digest" <daily.digest.{}>'.format( config["SITE_NAME"], config.get("SERVER_NAME", "example.com") ) base_extra_headers = { "List-Id": list_id, "List-Post": "NO", "Auto-Submitted": "auto-generated", "X-Auto-Response-Suppress": "All", "Precedence": "bulk", } for membership in user.communautes_membership: community = membership.community if not community: # TODO: should not happen but it does. Fix root cause instead. continue # create an empty digest digest = CommunityDigest(community) AE = ActivityEntry activities = ( AE.query.order_by(AE.happened_at.asc()) .filter( and_( AE.happened_at > happened_after, or_( and_( AE.target_type == community.object_type, AE.target_id == community.id, ), and_( AE.object_type == community.object_type, AE.object_id == community.id, ), ), ) ) .all() ) # fill the internal digest lists with infos # seen_entities, new_members, new_documents, updated_documents ... for activity in activities: digest.update_from_activity(activity, user) # if activities: # import ipdb; ipdb.set_trace() # save the current digest in the master digests list if not digest.is_empty(): digests.append(digest) if not digests: return None token = generate_unsubscribe_token(user) unsubscribe_url = url_for( "notifications.unsubscribe_sbe", token=token, _external=True, _scheme=config["PREFERRED_URL_SCHEME"], ) extra_headers = dict(base_extra_headers) extra_headers["List-Unsubscribe"] = f"<{unsubscribe_url}>" msg = Message( subject, sender=sender, recipients=[recipient], extra_headers=extra_headers ) ctx = {"digests": digests, "token": token, "unsubscribe_url": unsubscribe_url} msg.body = render_template_i18n("notifications/daily-social-digest.txt", **ctx) msg.html = render_template_i18n("notifications/daily-social-digest.html", **ctx) return msg
def send_post_to_user(community, post, member): recipient = member.email subject = u'[%s] %s' % (community.name, post.title) config = current_app.config sender = config.get('BULK_MAIL_SENDER', config['MAIL_SENDER']) SBE_FORUM_REPLY_BY_MAIL = config.get('SBE_FORUM_REPLY_BY_MAIL', False) SERVER_NAME = config.get('SERVER_NAME', u'example.com') list_id = u'"{} forum" <forum.{}.{}>'.format(community.name, community.slug, SERVER_NAME) forum_url = url_for('forum.index', community_id=community.slug, _external=True) forum_archive = url_for('forum.archives', community_id=community.slug, _external=True) extra_headers = { 'List-Id': list_id, 'List-Archive': u'<{}>'.format(forum_archive), 'List-Post': '<{}>'.format(forum_url), 'X-Auto-Response-Suppress': 'All', 'Auto-Submitted': 'auto-generated', } if SBE_FORUM_REPLY_BY_MAIL and config['MAIL_ADDRESS_TAG_CHAR'] is not None: name = sender.rsplit('@', 1)[0] domain = sender.rsplit('@', 1)[1] replyto = build_reply_email_address(name, post, member, domain) msg = Message(subject, sender=sender, recipients=[recipient], reply_to=replyto, extra_headers=extra_headers) else: msg = Message(subject, sender=sender, recipients=[recipient], extra_headers=extra_headers) msg.body = render_template_i18n( "forum/mail/new_message.txt", community=community, post=post, member=member, MAIL_REPLY_MARKER=MAIL_REPLY_MARKER, SBE_FORUM_REPLY_BY_MAIL=SBE_FORUM_REPLY_BY_MAIL, ) msg.html = render_template_i18n( "forum/mail/new_message.html", community=community, post=post, member=member, MAIL_REPLY_MARKER=MAIL_REPLY_MARKER, SBE_FORUM_REPLY_BY_MAIL=SBE_FORUM_REPLY_BY_MAIL, ) logger.debug("Sending new post by email to %r", member.email) try: mail.send(msg) except: logger.error("Send mail to user failed", exc_info=True) # log to sentry if enabled
def make_message(user): config = current_app.config sbe_config = config['ABILIAN_SBE'] sender = config.get('BULK_MAIL_SENDER', config['MAIL_SENDER']) recipient = user.email subject = sbe_config['DAILY_SOCIAL_DIGEST_SUBJECT'] digests = [] happened_after = datetime.utcnow() - timedelta(days=1) list_id = u'"{} daily digest" <daily.digest.{}>'.format( config['SITE_NAME'], config.get('SERVER_NAME', u'example.com'), ) base_extra_headers = { 'List-Id': list_id, 'List-Post': 'NO', 'Auto-Submitted': 'auto-generated', 'X-Auto-Response-Suppress': 'All', 'Precedence': 'bulk', } for membership in user.communautes_membership: community = membership.community if not community: # TODO: should not happen but it does. Fix root cause instead. continue # create an empty digest digest = CommunityDigest(community) AE = ActivityEntry activities = AE.query \ .order_by(AE.happened_at.asc()) \ .filter(and_(AE.happened_at > happened_after, or_(and_(AE.target_type == community.object_type, AE.target_id == community.id), and_(AE.object_type == community.object_type, AE.object_id == community.id), ))) \ .all() # fill the internal digest lists with infos # seen_entities, new_members, new_documents, updated_documents ... for activity in activities: digest.update_from_activity(activity, user) # if activities: # import ipdb; ipdb.set_trace() # save the current digest in the master digests list if not digest.is_empty(): digests.append(digest) if not digests: return None token = generate_unsubscribe_token(user) unsubscribe_url = url_for('notifications.unsubscribe_sbe', token=token, _external=True, _scheme=config['PREFERRED_URL_SCHEME']) extra_headers = dict(base_extra_headers) extra_headers['List-Unsubscribe'] = u'<{}>'.format(unsubscribe_url) msg = Message(subject, sender=sender, recipients=[recipient], extra_headers=extra_headers) ctx = { 'digests': digests, 'token': token, 'unsubscribe_url': unsubscribe_url } msg.body = render_template_i18n("notifications/daily-social-digest.txt", **ctx) msg.html = render_template_i18n("notifications/daily-social-digest.html", **ctx) return msg
def make_message(user): config = current_app.config sbe_config = config["ABILIAN_SBE"] sender = config.get("BULK_MAIL_SENDER", config["MAIL_SENDER"]) recipient = user.email subject = sbe_config["DAILY_SOCIAL_DIGEST_SUBJECT"] digests = [] happened_after = datetime.utcnow() - timedelta(days=1) list_id = '"{} daily digest" <daily.digest.{}>'.format( config["SITE_NAME"], config.get("SERVER_NAME", "example.com")) base_extra_headers = { "List-Id": list_id, "List-Post": "NO", "Auto-Submitted": "auto-generated", "X-Auto-Response-Suppress": "All", "Precedence": "bulk", } for membership in user.communautes_membership: community = membership.community if not community: # TODO: should not happen but it does. Fix root cause instead. continue # create an empty digest digest = CommunityDigest(community) AE = ActivityEntry activities = (AE.query.order_by(AE.happened_at.asc()).filter( and_( AE.happened_at > happened_after, or_( and_( AE.target_type == community.object_type, AE.target_id == community.id, ), and_( AE.object_type == community.object_type, AE.object_id == community.id, ), ), )).all()) # fill the internal digest lists with infos # seen_entities, new_members, new_documents, updated_documents ... for activity in activities: digest.update_from_activity(activity, user) # if activities: # import ipdb; ipdb.set_trace() # save the current digest in the master digests list if not digest.is_empty(): digests.append(digest) if not digests: return None token = generate_unsubscribe_token(user) unsubscribe_url = url_for( "notifications.unsubscribe_sbe", token=token, _external=True, _scheme=config["PREFERRED_URL_SCHEME"], ) extra_headers = dict(base_extra_headers) extra_headers["List-Unsubscribe"] = "<{}>".format(unsubscribe_url) msg = Message(subject, sender=sender, recipients=[recipient], extra_headers=extra_headers) ctx = { "digests": digests, "token": token, "unsubscribe_url": unsubscribe_url } msg.body = render_template_i18n("notifications/daily-social-digest.txt", **ctx) msg.html = render_template_i18n("notifications/daily-social-digest.html", **ctx) return msg