From 03c367a5b934299dec687e37570b8612f225e60b Mon Sep 17 00:00:00 2001 From: LV-426 Date: Mon, 20 Apr 2015 15:08:35 +0300 Subject: rz_mail: rename send_email_message -> send_email__flask_ctx + doc --- src/server/rz_feedback.py | 4 ++-- src/server/rz_mail.py | 16 +++++++++------- src/server/rz_user.py | 6 +++--- 3 files changed, 14 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/server/rz_feedback.py b/src/server/rz_feedback.py index 557bd808..48f64258 100644 --- a/src/server/rz_feedback.py +++ b/src/server/rz_feedback.py @@ -5,7 +5,7 @@ from flask import request from flask import session import logging -from rz_mail import send_email_message +from rz_mail import send_email__flask_ctx from rz_req_handling import make_response__json @@ -67,7 +67,7 @@ def rest__send_user_feedback__email(): msg_body = '\n'.join(msg_body) try: - send_email_message(recipients=[current_app.rz_config.feedback_recipient], + send_email__flask_ctx(recipients=[current_app.rz_config.feedback_recipient], subject="User Feedback", body=msg_body, attachments=[('feedback_screenshot.png', 'image/png', u_feedback.img), diff --git a/src/server/rz_mail.py b/src/server/rz_mail.py index 82c71d8f..38d894c0 100644 --- a/src/server/rz_mail.py +++ b/src/server/rz_mail.py @@ -11,13 +11,16 @@ import smtplib log = logging.getLogger('rhizi') -def send_email_message(recipients, subject, body, attachments=[]): +def send_email__flask_ctx(recipients, subject, body, attachments=[]): + """ + Flask context dependent email sending utility + """ send_from = current_app.rz_config.mail_default_sender mta_host = current_app.rz_config.mta_host mta_port = current_app.rz_config.mta_port - send_message_helper(mta_host, + send_email(mta_host, mta_port, send_from=send_from, recipients=recipients, @@ -27,12 +30,11 @@ def send_email_message(recipients, subject, body, attachments=[]): log.info('email sent: recipients: %s: subject: %s, attachment-count: %d' % (recipients, subject, len(attachments))) -def send_message_helper(mta_host, mta_port, send_from, recipients, subject, attachments, body): +def send_email(mta_host, mta_port, send_from, recipients, subject, attachments, body): """ - attachments is a list of tuples (filename, mimetype, data) + Note: to allow for easy testing this function should not depend on any flask app/request context - note: helper exists for ease of testing, since it doesn't use flask only - python batteries-included packages + @param attachments: is a list of tuples (filename, mimetype, data) """ assert isinstance(recipients, list) @@ -58,5 +60,5 @@ def send_message_helper(mta_host, mta_port, send_from, recipients, subject, atta if __name__ == '__main__': # FIXME - move to actual test suite - send_message_helper('localhost', 'alon@localhost', ['alon@localhost'], 'test subject', [ + send_email('localhost', 'alon@localhost', ['alon@localhost'], 'test subject', [ ('diary.txt', 'text/plain', "bla bla bla yeah that's right bla")], 'this is it pal') diff --git a/src/server/rz_user.py b/src/server/rz_user.py index 44540f75..cfc2475d 100644 --- a/src/server/rz_user.py +++ b/src/server/rz_user.py @@ -12,7 +12,7 @@ import re import uuid from crypt_util import hash_pw -from rz_mail import send_email_message +from rz_mail import send_email__flask_ctx from rz_req_handling import make_response__json, make_response__json__html from rz_user_db import User_Account from rz_api_common import API_Exception__bad_request @@ -467,7 +467,7 @@ def send_user_activation_link__email(us_req): ] msg_body = '\n'.join(msg_body) - send_email_message(recipients=[us_req['email_address']], + send_email__flask_ctx(recipients=[us_req['email_address']], subject="Rhizi sign up request", body=msg_body) return activation_link @@ -497,7 +497,7 @@ def send_user_pw_reset__email(u_account, pw_reset_token): ] msg_body = '\n'.join(msg_body) - send_email_message(recipients=[u_account.email_address], + send_email__flask_ctx(recipients=[u_account.email_address], subject="Rhizi password reset request", body=msg_body) return pw_reset_link -- cgit v1.3.1