def submit_buyer_invite_request(): form = auth_forms.BuyerInviteRequestForm(request.form) if not form.validate(): return render_template_with_csrf('auth/buyer-invite-request.html', status_code=400, form=form) token = generate_buyer_creation_token(**form.data) invite_url = url_for('.send_buyer_invite', token=token, _external=True) email_body = render_template( 'emails/buyer_account_invite_request_email.html', form=form, invite_url=invite_url) try: send_email( current_app.config['BUYER_INVITE_REQUEST_ADMIN_EMAIL'], email_body, current_app.config['BUYER_INVITE_REQUEST_SUBJECT'], current_app.config['BUYER_INVITE_REQUEST_EMAIL_FROM'], current_app.config['BUYER_INVITE_REQUEST_EMAIL_NAME'], ) except EmailError as e: rollbar.report_exc_info() current_app.logger.error( 'buyerinvite.fail: Buyer account invite request email failed to send. ' 'error {error} invite email_hash {email_hash}', extra={ 'error': six.text_type(e), 'email_hash': hash_email(form.email_address.data) }) abort(503, response='Failed to send buyer account invite request email.') email_body = render_template( 'emails/buyer_account_invite_manager_confirmation.html', form=form) try: send_email( form.manager_email.data, email_body, current_app.config['BUYER_INVITE_MANAGER_CONFIRMATION_SUBJECT'], current_app.config['DM_GENERIC_NOREPLY_EMAIL'], current_app.config['BUYER_INVITE_MANAGER_CONFIRMATION_NAME'], ) except EmailError as e: rollbar.report_exc_info() current_app.logger.error( 'buyerinvite.fail: Buyer account invite request manager email failed to send. ' 'error {error} invite email_hash {email_hash} manager email_hash {manager_email_hash}', extra={ 'error': six.text_type(e), 'email_hash': hash_email(form.email_address.data), 'manager_email_hash': hash_email(form.manager_email.data) }) abort(503, response= 'Failed to send buyer account invite request manager email.') return render_template('auth/buyer-invite-request-complete.html')
def send_account_activation_manager_email(token, manager_name, manager_email, applicant_name, applicant_email, framework): _send_account_activation_admin_email(token=token, manager_name=manager_name, manager_email=manager_email, applicant_name=applicant_name, applicant_email=applicant_email, framework=framework) email_body = render_email_template( 'buyer_account_invite_manager_confirmation.md', manager_name=escape_markdown(manager_name), applicant_name=escape_markdown(applicant_name), ) try: send_or_handle_error( manager_email, email_body, current_app.config['BUYER_INVITE_MANAGER_CONFIRMATION_SUBJECT'], current_app.config['INVITE_EMAIL_FROM'], current_app.config['INVITE_EMAIL_NAME'], ) session['email_sent_to'] = manager_email except EmailError as e: rollbar.report_exc_info() current_app.logger.error( 'Invitation email to manager failed to send. ' 'error {error} email_hash {email_hash}', extra={ 'error': six.text_type(e), 'email_hash': hash_email(manager_email) })
def send_reset_password_confirm_email(token, email_address, locked, framework): url = '{}{}/reset-password/{}?e={}'.format( current_app.config['FRONTEND_ADDRESS'], get_root_url(framework), token, quote_plus(email_address)) subject = current_app.config['RESET_PASSWORD_EMAIL_SUBJECT'] name = current_app.config['RESET_PASSWORD_EMAIL_NAME'] if locked: email_template = 'reset_password_email_locked_account_marketplace.md' else: email_template = 'reset_password_email_marketplace.md' email_body = render_email_template(email_template, url=url) try: send_or_handle_error(email_address, email_body, subject, current_app.config['RESET_PASSWORD_EMAIL_FROM'], name) session['email_sent_to'] = email_address except EmailError as e: rollbar.report_exc_info() current_app.logger.error( 'Password reset email failed to send. ' 'error {error} email_hash {email_hash}', extra={ 'error': six.text_type(e), 'email_hash': hash_email(email_address) })
def send_team_member_account_activation_email(token, email_address, framework, user_name, supplier_name): url = '{}{}/create-user/{}?e={}'.format( current_app.config['FRONTEND_ADDRESS'], get_root_url(framework), token, quote_plus(email_address)) email_body = render_email_template( 'seller_edit_team_member_create_user_email.md', url=url, user_name=user_name, supplier_name=supplier_name, frontend_url=current_app.config['FRONTEND_ADDRESS']) try: send_or_handle_error( email_address, email_body, current_app.config['INVITE_EMAIL_SUBJECT'], current_app.config['INVITE_EMAIL_FROM'], current_app.config['INVITE_EMAIL_NAME'], ) session['email_sent_to'] = email_address except EmailError as e: rollbar.report_exc_info() current_app.logger.error( 'Invitation email failed to send. ' 'error {error} email_hash {email_hash}', extra={ 'error': six.text_type(e), 'email_hash': hash_email(email_address) })
def send_account_activation_email(name, email_address, user_type, framework): template = 'orams_create_user_email.md' if framework == 'orams' else 'create_user_email.md' subject = 'ORAMS_INVITE_EMAIL_SUBJECT' if framework == 'orams' else 'INVITE_EMAIL_SUBJECT' token = generate_user_creation_token(name=name, email_address=email_address, user_type=user_type, framework=framework) url = '{}{}/create-user/{}'.format(current_app.config['FRONTEND_ADDRESS'], get_root_url(framework), escape_token_markdown(quote(token))) email_body = render_email_template(template, url=url) try: send_or_handle_error( email_address, email_body, current_app.config[subject], current_app.config['INVITE_EMAIL_FROM'], current_app.config['INVITE_EMAIL_NAME'], ) session['email_sent_to'] = email_address except EmailError as e: rollbar.report_exc_info() current_app.logger.error( 'Invitation email failed to send. ' 'error {error} email_hash {email_hash}', extra={ 'error': six.text_type(e), 'email_hash': hash_email(email_address) })
def send_reset_password_confirm_email(email_address, url, locked, framework): if framework == "orams": subject = current_app.config['ORAMS_RESET_PASSWORD_EMAIL_SUBJECT'] name = current_app.config['ORAMS_GENERIC_SUPPORT_NAME'] if locked: email_template = 'reset_password_email_locked_account_orams.md' else: email_template = 'reset_password_email_orams.md' else: subject = current_app.config['RESET_PASSWORD_EMAIL_SUBJECT'] name = current_app.config['RESET_PASSWORD_EMAIL_NAME'] if locked: email_template = 'reset_password_email_locked_account_marketplace.md' else: email_template = 'reset_password_email_marketplace.md' email_body = render_email_template(email_template, url=url) try: send_or_handle_error(email_address, email_body, subject, current_app.config['RESET_PASSWORD_EMAIL_FROM'], name) session['email_sent_to'] = email_address except EmailError as e: rollbar.report_exc_info() current_app.logger.error( 'Password reset email failed to send. ' 'error {error} email_hash {email_hash}', extra={ 'error': six.text_type(e), 'email_hash': hash_email(email_address) })
def send_buyer_account_activation_email(name, email_address, token): token = generate_buyer_creation_token(name=name, email_address=email_address) url = url_for('main.create_buyer_account', token=token, _external=True) email_body = render_template('emails/create_buyer_user_email.html', url=url) try: send_email( email_address, email_body, current_app.config['CREATE_USER_SUBJECT'], current_app.config['RESET_PASSWORD_EMAIL_FROM'], current_app.config['RESET_PASSWORD_EMAIL_NAME'], ) session['email_sent_to'] = email_address except EmailError as e: rollbar.report_exc_info() current_app.logger.error( 'buyercreate.fail: Create user email failed to send. ' 'error {error} email_hash {email_hash}', extra={ 'error': six.text_type(e), 'email_hash': hash_email(email_address) }) abort(503, response='Failed to send user creation email.')
def update_password(token): form = auth_forms.ChangePasswordForm(request.form) decoded = decode_password_reset_token(token.encode(), data_api_client) if decoded.get('error', None): flash(decoded['error'], 'error') return redirect(url_for('.request_password_reset')) user_id = decoded["user"] email_address = decoded["email"] password = form.password.data if form.validate(): if data_api_client.update_user_password(user_id, password, email_address): current_app.logger.info( "User {user_id} ({hashed_email}) successfully changed their password", extra={ 'user_id': user_id, 'hashed_email': hash_email(email_address) }) flash('password_updated') else: flash('password_not_updated', 'error') return redirect(url_for('.render_login')) else: return render_template_with_csrf("auth/reset-password.html", status_code=400, email_address=email_address, form=form, token=token)
def send_account_activation_email(token, email_address, framework): url = '{}{}/create-user/{}?e={}'.format( current_app.config['FRONTEND_ADDRESS'], get_root_url(framework), token, quote_plus(email_address) ) email_body = render_email_template('create_user_email.md', url=url) try: send_or_handle_error( email_address, email_body, current_app.config['INVITE_EMAIL_SUBJECT'], current_app.config['INVITE_EMAIL_FROM'], current_app.config['INVITE_EMAIL_NAME'], ) session['email_sent_to'] = email_address except EmailError as e: rollbar.report_exc_info() current_app.logger.error( 'Invitation email failed to send. ' 'error {error} email_hash {email_hash}', extra={ 'error': six.text_type(e), 'email_hash': hash_email(email_address)})
def send_new_user_onboarding_email(name, email_address, user_type, framework): if user_type != 'buyer': return False assert user_type == 'buyer' email_body = render_email_template( 'buyer_onboarding.md', name=name ) try: send_or_handle_error( email_address, email_body, 'Welcome to the Digital Marketplace', current_app.config['RESET_PASSWORD_EMAIL_FROM'], current_app.config['RESET_PASSWORD_EMAIL_NAME'], ) session['email_sent_to'] = email_address except EmailError as error: rollbar.report_exc_info() current_app.logger.error( 'buyeronboarding.fail: Buyer onboarding email failed to send. ' 'error {error} email_hash {email_hash}', extra={ 'error': six.text_type(error), 'email_hash': hash_email(email_address)}) return jsonify(message='Failed to send buyer onboarding email.'), 503
def process_login(): form = auth_forms.LoginForm(request.form) next_url = request.args.get('next') if form.validate(): user_json = data_api_client.authenticate_user(form.email_address.data, form.password.data) if not user_json: current_app.logger.info( "login.fail: failed to sign in {email_hash}", extra={'email_hash': hash_email(form.email_address.data)}) flash("no_account", "error") return render_template_with_csrf("auth/login.html", status_code=403, form=form, next=next_url) user = User.from_json(user_json) login_user(user) current_app.logger.info('login.success: {user}', extra={'user': user_logging_string(user)}) check_terms_acceptance() return redirect_logged_in_user(next_url) else: return render_template_with_csrf("auth/login.html", status_code=400, form=form, next=next_url)
def send_account_activation_manager_email(token, manager_name, manager_email, applicant_name, applicant_email, framework): _send_account_activation_admin_email( token=token, manager_name=manager_name, manager_email=manager_email, applicant_name=applicant_name, applicant_email=applicant_email, framework=framework ) email_body = render_email_template( 'buyer_account_invite_manager_confirmation.md', manager_name=escape_markdown(manager_name), applicant_name=escape_markdown(applicant_name), ) try: send_or_handle_error( manager_email, email_body, current_app.config['BUYER_INVITE_MANAGER_CONFIRMATION_SUBJECT'], current_app.config['INVITE_EMAIL_FROM'], current_app.config['INVITE_EMAIL_NAME'], ) session['email_sent_to'] = manager_email except EmailError as e: rollbar.report_exc_info() current_app.logger.error( 'Invitation email to manager failed to send. ' 'error {error} email_hash {email_hash}', extra={ 'error': six.text_type(e), 'email_hash': hash_email(manager_email)})
def send_user_existing_password_reset_email(name, email_address): frontend_url = current_app.config['FRONTEND_ADDRESS'] email_body = render_email_template( 'user_existing_password_reset.md', frontend_url=frontend_url, name=name, reset_password_url='{}/2/reset-password'.format(frontend_url) ) try: send_or_handle_error( email_address, email_body, 'You are already registered', current_app.config['DM_GENERIC_NOREPLY_EMAIL'], current_app.config['DM_GENERIC_SUPPORT_NAME'] ) session['email_sent_to'] = email_address except EmailError as e: rollbar.report_exc_info() current_app.logger.error( 'Invitation email failed to send. ' 'error {error} email_hash {email_hash}', extra={ 'error': six.text_type(e), 'email_hash': hash_email(email_address)})
def send_new_user_onboarding_email(name, email_address, user_type, framework): if user_type != 'buyer': return False assert user_type == 'buyer' email_body = render_email_template( 'buyer_onboarding.md', name=name, frontend_url=current_app.config['FRONTEND_ADDRESS'], ) try: send_or_handle_error( email_address, email_body, 'Welcome to the Digital Marketplace', current_app.config['RESET_PASSWORD_EMAIL_FROM'], current_app.config['RESET_PASSWORD_EMAIL_NAME'], ) session['email_sent_to'] = email_address except EmailError as error: rollbar.report_exc_info() current_app.logger.error( 'buyeronboarding.fail: Buyer onboarding email failed to send. ' 'error {error} email_hash {email_hash}', extra={ 'error': six.text_type(error), 'email_hash': hash_email(email_address) }) return jsonify(message='Failed to send buyer onboarding email.'), 503
def _send_account_activation_admin_email(token, manager_name, manager_email, applicant_name, applicant_email, framework): url = '{}{}/send-invite/{}?e={}'.format( current_app.config['FRONTEND_ADDRESS'], get_root_url(framework), token, quote_plus(applicant_email)) email_body = render_email_template( 'buyer_account_invite_request_email.md', manager_name=escape_markdown(manager_name), manager_email=escape_markdown(manager_email), applicant_name=escape_markdown(applicant_name), applicant_email=escape_markdown(applicant_email), invite_url=url) try: send_or_handle_error( current_app.config['BUYER_INVITE_REQUEST_ADMIN_EMAIL'], email_body, current_app.config['BUYER_INVITE_MANAGER_CONFIRMATION_SUBJECT'], current_app.config['INVITE_EMAIL_FROM'], current_app.config['INVITE_EMAIL_NAME'], ) session['email_sent_to'] = current_app.config[ 'BUYER_INVITE_REQUEST_ADMIN_EMAIL'] except EmailError as e: rollbar.report_exc_info() current_app.logger.error( 'Invitation email to manager failed to send. ' 'error {error} email_hash {email_hash}', extra={ 'error': six.text_type(e), 'email_hash': hash_email(manager_email) })
def test_hash_email(): tests = [ (u'*****@*****.**', six.b('lz3-Rj7IV4X1-Vr1ujkG7tstkxwk5pgkqJ6mXbpOgTs=')), (u'☃@example.com', six.b('jGgXle8WEBTTIFhP25dF8Ck-FxQSCZ_N0iWYBWve4Ps=')), ] for test, expected in tests: assert hash_email(test) == expected
def send_invite_user(): form = EmailAddressForm(request.form) if form.validate(): email_address = form.email_address.data token = generate_supplier_invitation_token( name='', supplier_code=current_user.supplier_code, supplier_name=current_user.supplier_name, email_address=email_address ) url = url_for('main.create_user', token=token, _external=True) email_body = render_template( 'emails/invite_user_email.html', url=url, user=current_user.name, supplier=current_user.supplier_name, generic_contact_email=current_app.config['GENERIC_CONTACT_EMAIL']) try: send_email( email_address, email_body, 'Invitation to join {} as a team member'.format( current_user .supplier_name .encode("ascii", "ignore") .decode('ascii') ), current_app.config['INVITE_EMAIL_FROM'], current_app.config['INVITE_EMAIL_NAME'] ) except EmailError as e: rollbar.report_exc_info() current_app.logger.error( 'Invitation email failed to send. ' 'error {error} supplier_code {supplier_code} email_hash {email_hash}', extra={'error': six.text_type(e), 'supplier_code': current_user.supplier_code, 'email_hash': hash_email(current_user.email_address)}) abort(503, 'Failed to send user invite reset') data_api_client.create_audit_event( audit_type=AuditTypes.invite_user, user=current_user.email_address, object_type='suppliers', object_id=current_user.supplier_code, data={'invitedEmail': email_address}, ) form.email_address.data = None return render_template_with_csrf( 'auth/submit_email_address.html', form=form, invited_user=email_address) else: return render_template_with_csrf( 'auth/submit_email_address.html', status_code=400, form=form)
def send_invite_user(): form = EmailAddressForm(request.form) if form.validate(): token = generate_supplier_invitation_token( name='', supplier_code=current_user.supplier_code, supplier_name=current_user.supplier_name, email_address=form.email_address.data ) url = url_for('main.create_user', token=token, _external=True) email_body = render_template( 'emails/invite_user_email.html', url=url, user=current_user.name, supplier=current_user.supplier_name) try: send_email( form.email_address.data, email_body, current_app.config['INVITE_EMAIL_SUBJECT'], current_app.config['INVITE_EMAIL_FROM'], current_app.config['INVITE_EMAIL_NAME'] ) except EmailError as e: rollbar.report_exc_info() current_app.logger.error( 'Invitation email failed to send. ' 'error {error} supplier_code {supplier_code} email_hash {email_hash}', extra={'error': six.text_type(e), 'supplier_code': current_user.supplier_code, 'email_hash': hash_email(current_user.email_address)}) abort(503, 'Failed to send user invite reset') data_api_client.create_audit_event( audit_type=AuditTypes.invite_user, user=current_user.email_address, object_type='suppliers', object_id=current_user.supplier_code, data={'invitedEmail': form.email_address.data}, ) flash('user_invited', 'success') return redirect(url_for('.list_users')) else: return render_template_with_csrf( 'auth/submit_email_address.html', status_code=400, form=form)
def process_login(): form = auth_forms.LoginForm(request.form) next_url = request.args.get('next') if form.validate(): result = data_api_client.authenticate_user( form.email_address.data, form.password.data) if not result: current_app.logger.info( "login.fail: failed to sign in {email_hash}", extra={'email_hash': hash_email(form.email_address.data)}) flash("no_account", "error") return render_template_with_csrf( "auth/login.html", status_code=403, form=form, next=next_url) user = User.from_json(result) if '_csrf_token' in session: session.pop('_csrf_token') if 'csrf' in session: session.pop('csrf') if current_app.config['REDIS_SESSIONS']: session.regenerate() login_user(user) current_app.logger.info('login.success: {user}', extra={'user': user_logging_string(user)}) check_terms_acceptance() if current_user.role == 'buyer': user = User.load_user(data_api_client, current_user.id) if not user.is_team_member and user.must_join_team: next_url = '/2/team/join' return redirect_logged_in_user(next_url, result.get('validation_result', None)) else: return render_template_with_csrf( "auth/login.html", status_code=400, form=form, next=next_url)
def send_buyer_onboarding_email(name, email_address): email_body = render_template('emails/buyer_onboarding.html', name=name) try: send_email( email_address, email_body, 'Welcome to the Digital Marketplace', current_app.config['RESET_PASSWORD_EMAIL_FROM'], current_app.config['RESET_PASSWORD_EMAIL_NAME'], ) session['email_sent_to'] = email_address except EmailError as e: rollbar.report_exc_info() current_app.logger.error( 'buyeronboarding.fail: Buyer onboarding email failed to send. ' 'error {error} email_hash {email_hash}', extra={ 'error': six.text_type(e), 'email_hash': hash_email(email_address)}) abort(503, response='Failed to send buyer onboarding email.')
def send_buyer_account_activation_email(name, email_address, token): token = generate_buyer_creation_token(name=name, email_address=email_address) url = url_for('main.create_buyer_account', token=token, _external=True) email_body = render_template('emails/create_buyer_user_email.html', url=url) try: send_email( email_address, email_body, current_app.config['CREATE_USER_SUBJECT'], current_app.config['RESET_PASSWORD_EMAIL_FROM'], current_app.config['RESET_PASSWORD_EMAIL_NAME'], ) session['email_sent_to'] = email_address except EmailError as e: rollbar.report_exc_info() current_app.logger.error( 'buyercreate.fail: Create user email failed to send. ' 'error {error} email_hash {email_hash}', extra={ 'error': six.text_type(e), 'email_hash': hash_email(email_address)}) abort(503, response='Failed to send user creation email.')
def _send_account_activation_admin_email(manager_name, manager_email, applicant_name, applicant_email, framework): token = generate_user_creation_token(name=applicant_name, email_address=applicant_email, user_type="buyer", framework=framework) url = '{}/buyers/signup/send-invite/{}'.format( current_app.config['FRONTEND_ADDRESS'], escape_token_markdown(quote(token))) email_body = render_email_template('buyer_account_invite_request_email.md', manager_name=manager_name, manager_email=manager_email, applicant_name=applicant_name, applicant_email=applicant_email, invite_url=url) try: send_or_handle_error( current_app.config['BUYER_INVITE_REQUEST_ADMIN_EMAIL'], email_body, current_app.config['BUYER_INVITE_MANAGER_CONFIRMATION_SUBJECT'], current_app.config['INVITE_EMAIL_FROM'], current_app.config['INVITE_EMAIL_NAME'], ) session['email_sent_to'] = current_app.config[ 'BUYER_INVITE_REQUEST_ADMIN_EMAIL'] except EmailError as e: rollbar.report_exc_info() current_app.logger.error( 'Invitation email to manager failed to send. ' 'error {error} email_hash {email_hash}', extra={ 'error': six.text_type(e), 'email_hash': hash_email(manager_email) })
def send_reset_password_confirm_email(token, email_address, locked, framework): url = '{}{}/reset-password/{}?e={}'.format( current_app.config['FRONTEND_ADDRESS'], get_root_url(framework), token, quote_plus(email_address) ) subject = current_app.config['RESET_PASSWORD_EMAIL_SUBJECT'] name = current_app.config['RESET_PASSWORD_EMAIL_NAME'] if locked: email_template = 'reset_password_email_locked_account_marketplace.md' else: email_template = 'reset_password_email_marketplace.md' email_body = render_email_template( email_template, url=url ) try: send_or_handle_error( email_address, email_body, subject, current_app.config['RESET_PASSWORD_EMAIL_FROM'], name ) session['email_sent_to'] = email_address except EmailError as e: rollbar.report_exc_info() current_app.logger.error( 'Password reset email failed to send. ' 'error {error} email_hash {email_hash}', extra={ 'error': six.text_type(e), 'email_hash': hash_email(email_address)})
def _send_account_activation_admin_email(token, manager_name, manager_email, applicant_name, applicant_email, framework): url = '{}{}/send-invite/{}?e={}'.format( current_app.config['FRONTEND_ADDRESS'], get_root_url(framework), token, quote_plus(applicant_email) ) email_body = render_email_template( 'buyer_account_invite_request_email.md', manager_name=escape_markdown(manager_name), manager_email=escape_markdown(manager_email), applicant_name=escape_markdown(applicant_name), applicant_email=escape_markdown(applicant_email), invite_url=url ) try: send_or_handle_error( current_app.config['BUYER_INVITE_REQUEST_ADMIN_EMAIL'], email_body, current_app.config['BUYER_INVITE_MANAGER_CONFIRMATION_SUBJECT'], current_app.config['INVITE_EMAIL_FROM'], current_app.config['INVITE_EMAIL_NAME'], ) session['email_sent_to'] = current_app.config['BUYER_INVITE_REQUEST_ADMIN_EMAIL'] except EmailError as e: rollbar.report_exc_info() current_app.logger.error( 'Invitation email to manager failed to send. ' 'error {error} email_hash {email_hash}', extra={ 'error': six.text_type(e), 'email_hash': hash_email(manager_email)})
def send_user_existing_password_reset_email(name, email_address): frontend_url = current_app.config['FRONTEND_ADDRESS'] email_body = render_email_template( 'user_existing_password_reset.md', frontend_url=frontend_url, name=name, reset_password_url='{}/2/reset-password'.format(frontend_url)) try: send_or_handle_error(email_address, email_body, 'You are already registered', current_app.config['DM_GENERIC_NOREPLY_EMAIL'], current_app.config['DM_GENERIC_SUPPORT_NAME']) session['email_sent_to'] = email_address except EmailError as e: rollbar.report_exc_info() current_app.logger.error( 'Invitation email failed to send. ' 'error {error} email_hash {email_hash}', extra={ 'error': six.text_type(e), 'email_hash': hash_email(email_address) })
def send_reset_password_email(): form = auth_forms.EmailAddressForm(request.form) if form.validate(): email_address = form.email_address.data user_json = data_api_client.get_user(email_address=email_address) if user_json is not None: user = User.from_json(user_json) token = generate_token( { "user": user.id, "email": user.email_address }, current_app.config['SECRET_KEY'], current_app.config['RESET_PASSWORD_SALT']) url = url_for('main.reset_password', token=token, _external=True) email_body = render_template("emails/reset_password_email.html", url=url, locked=user.locked) try: send_email( user.email_address, email_body, current_app.config['RESET_PASSWORD_EMAIL_SUBJECT'], current_app.config['RESET_PASSWORD_EMAIL_FROM'], current_app.config['RESET_PASSWORD_EMAIL_NAME'], ) except EmailError as e: rollbar.report_exc_info() current_app.logger.error( "Password reset email failed to send. " "error {error} email_hash {email_hash}", extra={ 'error': six.text_type(e), 'email_hash': hash_email(user.email_address) }) abort(503, response="Failed to send password reset.") current_app.logger.info( "login.reset-email.sent: Sending password reset email for " "supplier_code {supplier_code} email_hash {email_hash}", extra={ 'supplier_code': user.supplier_code, 'email_hash': hash_email(user.email_address) }) else: current_app.logger.info( "login.reset-email.invalid-email: " "Password reset request for invalid supplier email {email_hash}", extra={'email_hash': hash_email(email_address)}) flash('email_sent') return redirect(url_for('.request_password_reset')) else: return render_template_with_csrf("auth/request-password-reset.html", status_code=400, form=form)
def send_email(to_email_addresses, email_body, subject, from_email, from_name, reply_to=None, bcc_addresses=None): if isinstance(to_email_addresses, string_types): to_email_addresses = [to_email_addresses] if not bcc_addresses: bcc_addresses = [] if isinstance(bcc_addresses, string_types): bcc_addresses = [bcc_addresses] if current_app.config.get('DM_SEND_EMAIL_TO_STDERR', False): email_body = to_text(email_body) subject = to_text(subject) reload(sys) sys.setdefaultencoding('utf8') print (""" ------------------------ To: {to} Bcc: {bcc} Subject: {subject} From: {from_line} Reply-To: {reply_to} {body} ------------------------ """.format( to=', '.join(to_email_addresses), bcc=', '.join(bcc_addresses), subject=subject, from_line='{} <{}>'.format(from_name, from_email), reply_to=reply_to, body=email_body )) try: email_body = to_bytes(email_body) subject = to_bytes(subject) email_client = boto3.client( 'ses', region_name=getenv('AWS_REGION'), aws_access_key_id=getenv('AWS_ACCESS_KEY_ID'), aws_secret_access_key=getenv('AWS_SECRET_ACCESS_KEY'), endpoint_url=getenv('AWS_SES_URL') ) destination_addresses = { 'ToAddresses': to_email_addresses, } if 'DM_EMAIL_BCC_ADDRESS' in current_app.config: bcc_addresses.append(current_app.config['DM_EMAIL_BCC_ADDRESS']) if bcc_addresses: destination_addresses['BccAddresses'] = bcc_addresses return_address = current_app.config.get('DM_EMAIL_RETURN_ADDRESS') result = email_client.send_email( Source=u"{} <{}>".format(from_name, from_email), Destination=destination_addresses, Message={ 'Subject': { 'Data': subject, 'Charset': 'UTF-8' }, 'Body': { 'Html': { 'Data': email_body, 'Charset': 'UTF-8' } } }, ReturnPath=return_address or reply_to or from_email, ReplyToAddresses=[reply_to or from_email], ) current_app.logger.info("Sent email: id={id}, email={email_hash}", extra={ 'id': result['ResponseMetadata']['RequestId'], 'email_hash': hash_email(to_email_addresses[0]) }) except botocore.exceptions.ClientError as e: current_app.logger.error( "An SES error occurred: %s, when sending to %s", e.response['Error']['Message'], (' & ').join(to_email_addresses) ) raise EmailError(e.response['Error']['Message'])
def send_email(to_email_addresses, email_body, subject, from_email, from_name, reply_to=None): if isinstance(to_email_addresses, string_types): to_email_addresses = [to_email_addresses] if current_app.config.get('DM_SEND_EMAIL_TO_STDERR', False): email_body = to_text(email_body) subject = to_text(subject) template = Template( textwrap.dedent("""\ To: $to Subject: $subject From: $from_line Reply-To: $reply_to $body""")) subbed = template.substitute(to=', '.join(to_email_addresses), subject=subject, from_line='{} <{}>'.format( from_name, from_email), reply_to=reply_to, body=email_body) sys.stderr.write(subbed) else: try: email_body = to_bytes(email_body) subject = to_bytes(subject) email_client = boto3.client( 'ses', region_name=getenv('AWS_REGION'), aws_access_key_id=getenv('AWS_ACCESS_KEY_ID'), aws_secret_access_key=getenv('AWS_SECRET_ACCESS_KEY')) destination_addresses = { 'ToAddresses': to_email_addresses, } if 'DM_EMAIL_BCC_ADDRESS' in current_app.config: destination_addresses['BccAddresses'] = [ current_app.config['DM_EMAIL_BCC_ADDRESS'] ] return_address = current_app.config.get('DM_EMAIL_RETURN_ADDRESS') result = email_client.send_email( Source=u"{} <{}>".format(from_name, from_email), Destination=destination_addresses, Message={ 'Subject': { 'Data': subject, 'Charset': 'UTF-8' }, 'Body': { 'Html': { 'Data': email_body, 'Charset': 'UTF-8' } } }, ReturnPath=return_address or reply_to or from_email, ReplyToAddresses=[reply_to or from_email], ) current_app.logger.info( "Sent email: id={id}, email={email_hash}", extra={ 'id': result['ResponseMetadata']['RequestId'], 'email_hash': hash_email(to_email_addresses[0]) }) except botocore.exceptions.ClientError as e: current_app.logger.error( "An SES error occurred: {error}", extra={'error': e.response['Error']['Message']}) raise EmailError(e.response['Error']['Message'])