def send_comment_email(user, comment, media, request): """ Sends comment email to user when a comment is made on their media. Args: - user: the user object to whom the email is sent - comment: the comment object referencing user's media - media: the media object the comment is about - request: the request """ comment_url = request.urlgen( 'mediagoblin.user_pages.media_home.view_comment', comment=comment.id, user=media.get_uploader.username, media=media.slug_or_id, qualified=True) + '#comment' comment_author = comment.get_author.username rendered_email = render_template( request, 'mediagoblin/user_pages/comment_email.txt', {'username': user.username, 'comment_author': comment_author, 'comment_content': comment.content, 'comment_url': comment_url}) send_email( mg_globals.app_config['email_sender_address'], [user.email], '{instance_title} - {comment_author} '.format( comment_author=comment_author, instance_title=mg_globals.app_config['html_title']) \ + _('commented on your post'), rendered_email)
def test_send_email(): mail._clear_test_inboxes() # send the email mail.send_email( "*****@*****.**", ["*****@*****.**", "*****@*****.**"], "Testing is so much fun!", """HAYYY GUYS! I hope you like unit tests JUST AS MUCH AS I DO!""") # check the main inbox assert len(mail.EMAIL_TEST_INBOX) == 1 message = mail.EMAIL_TEST_INBOX.pop() assert message['From'] == "*****@*****.**" assert message['To'] == "[email protected], [email protected]" assert message['Subject'] == "Testing is so much fun!" assert message.get_payload(decode=True) == """HAYYY GUYS! I hope you like unit tests JUST AS MUCH AS I DO!""" # Check everything that the FakeMhost.sendmail() method got is correct assert len(mail.EMAIL_TEST_MBOX_INBOX) == 1 mbox_dict = mail.EMAIL_TEST_MBOX_INBOX.pop() assert mbox_dict['from'] == "*****@*****.**" assert mbox_dict['to'] == ["*****@*****.**", "*****@*****.**"] mbox_message = email.message_from_string(mbox_dict['message']) assert mbox_message['From'] == "*****@*****.**" assert mbox_message['To'] == "[email protected], [email protected]" assert mbox_message['Subject'] == "Testing is so much fun!" assert mbox_message.get_payload(decode=True) == """HAYYY GUYS!
def send_fp_verification_email(user, request): """ Send the verification email to users to change their password. Args: - user: a user object - request: the request """ fp_verification_key = get_timed_signer_url('mail_verification_token') \ .dumps(user.id) rendered_email = render_template( request, 'mediagoblin/plugins/basic_auth/fp_verification_email.txt', {'username': user.username, 'verification_url': EMAIL_FP_VERIFICATION_TEMPLATE.format( uri=request.urlgen('mediagoblin.plugins.basic_auth.verify_forgot_password', qualified=True), fp_verification_key=fp_verification_key)}) # TODO: There is no error handling in place send_email( mg_globals.app_config['email_sender_address'], [user.email], 'GNU MediaGoblin - Change forgotten password!', rendered_email)
def send_fp_verification_email(user, request): """ Send the verification email to users to change their password. Args: - user: a user object - request: the request """ fp_verification_key = get_timed_signer_url('mail_verification_token') \ .dumps(user.id) rendered_email = render_template( request, 'mediagoblin/plugins/basic_auth/fp_verification_email.txt', { 'username': user.username, 'verification_url': EMAIL_FP_VERIFICATION_TEMPLATE.format( uri=request.urlgen( 'mediagoblin.plugins.basic_auth.verify_forgot_password', qualified=True), fp_verification_key=fp_verification_key) }) # TODO: There is no error handling in place send_email(mg_globals.app_config['email_sender_address'], [user.email], 'GNU MediaGoblin - Change forgotten password!', rendered_email)
def send_verification_email(user, request, email=None, rendered_email=None): """ Send the verification email to users to activate their accounts. Args: - user: a user object - request: the request """ if not email: email = user.email if not rendered_email: verification_key = get_timed_signer_url('mail_verification_token') \ .dumps(user.id) rendered_email = render_template( request, 'mediagoblin/auth/verification_email.txt', {'username': user.username, 'verification_url': EMAIL_VERIFICATION_TEMPLATE.format( uri=request.urlgen('mediagoblin.auth.verify_email', qualified=True), verification_key=verification_key)}) # TODO: There is no error handling in place send_email( mg_globals.app_config['email_sender_address'], [email], # TODO # Due to the distributed nature of GNU MediaGoblin, we should # find a way to send some additional information about the # specific GNU MediaGoblin instance in the subject line. For # example "GNU MediaGoblin @ Wandborg - [...]". 'GNU MediaGoblin - Verify your email!', rendered_email)
def send_verification_email(user, request): """ Send the verification email to users to activate their accounts. Args: - user: a user object - request: the request """ rendered_email = render_template( request, 'mediagoblin/auth/verification_email.txt', {'username': user.username, 'verification_url': EMAIL_VERIFICATION_TEMPLATE.format( host=request.host, uri=request.urlgen('mediagoblin.auth.verify_email'), userid=unicode(user._id), verification_key=user.verification_key)}) # TODO: There is no error handling in place send_email( mg_globals.app_config['email_sender_address'], [user.email], # TODO # Due to the distributed nature of GNU MediaGoblin, we should # find a way to send some additional information about the # specific GNU MediaGoblin instance in the subject line. For # example "GNU MediaGoblin @ Wandborg - [...]". 'GNU MediaGoblin - Verify your email!', rendered_email)
def take_punitive_actions(request, form, report, user): message_body = "" # The bulk of this action is running through all of the different # punitive actions that a moderator could take. if u"takeaway" in form.action_to_resolve.data: for privilege_name in form.take_away_privileges.data: take_away_privileges(user.username, privilege_name) form.resolution_content.data += _(u"\n{mod} took away {user}'s {privilege} privileges.").format( mod=request.user.username, user=user.username, privilege=privilege_name ) # If the moderator elects to ban the user, a new instance of user_ban # will be created. if u"userban" in form.action_to_resolve.data: user_ban = ban_user( form.targeted_user.data, expiration_date=form.user_banned_until.data, reason=form.why_user_was_banned.data ) Session.add(user_ban) form.resolution_content.data += _(u"\n{mod} banned user {user} {expiration_date}.").format( mod=request.user.username, user=user.username, expiration_date=( _("until {date}").format(date=form.user_banned_until.data) if form.user_banned_until.data else _("indefinitely") ), ) # If the moderator elects to send a warning message. An email will be # sent to the email address given at sign up if u"sendmessage" in form.action_to_resolve.data: message_body = form.message_to_user.data form.resolution_content.data += _(u"\n{mod} sent a warning email to the {user}.").format( mod=request.user.username, user=user.username ) if u"delete" in form.action_to_resolve.data and report.is_comment_report(): deleted_comment = report.obj() Session.delete(deleted_comment) form.resolution_content.data += _(u"\n{mod} deleted the comment.").format(mod=request.user.username) elif u"delete" in form.action_to_resolve.data and report.is_media_entry_report(): deleted_media = report.obj() deleted_media.delete() form.resolution_content.data += _(u"\n{mod} deleted the media entry.").format(mod=request.user.username) report.archive(resolver_id=request.user.id, resolved=datetime.now(), result=form.resolution_content.data) Session.add(report) Session.commit() if message_body: send_email( mg_globals.app_config["email_sender_address"], [user.email], _("Warning from") + "- {moderator} ".format(moderator=request.user.username), message_body, ) return redirect(request, "mediagoblin.moderation.users_detail", user=user.username)
def test_email_force_starttls(starttls_enabled_app): common.TESTS_ENABLED = False SMTP = lambda *args, **kwargs: mail.FakeMhost() with mock.patch('smtplib.SMTP', SMTP): with pytest.raises(smtplib.SMTPException): mail.send_email(from_addr="*****@*****.**", to_addrs="*****@*****.**", subject="Testing is so much fun!", message_body="Ohai ^_^")
def test_email_force_starttls(starttls_enabled_app): common.TESTS_ENABLED = False SMTP = lambda *args, **kwargs: mail.FakeMhost() with mock.patch('smtplib.SMTP', SMTP): with pytest.raises(smtplib.SMTPException): mail.send_email( from_addr="*****@*****.**", to_addrs="*****@*****.**", subject="Testing is so much fun!", message_body="Ohai ^_^" )
def test_no_smtp_host(self): """ Empty email_smtp_host """ with pytest.raises(mail.NoSMTPServerError), mock.patch("smtplib.SMTP") as smtp_mock: smtp_mock.return_value.connect.side_effect = socket.error mg_globals.app_config = { "email_debug_mode": False, "email_smtp_use_ssl": False, "email_smtp_host": "", "email_smtp_port": 0} common.TESTS_ENABLED = False mail.send_email("", "", "", "")
def test_no_mail_server(self): """ Tests that no smtp server is available """ with pytest.raises(mail.NoSMTPServerError), mock.patch("smtplib.SMTP") as smtp_mock: smtp_mock.side_effect = socket.error mg_globals.app_config = { "email_debug_mode": False, "email_smtp_use_ssl": False, "email_smtp_host": "127.0.0.1", "email_smtp_port": 0} common.TESTS_ENABLED = False mail.send_email("", "", "", "")
def run(self, notification_id, message): cn = Notification.query.filter_by(id=notification_id).first() _log.info(u'Sending notification email about {0}'.format(cn)) return send_email( message['from'], [message['to']], message['subject'], message['body'])
def run(self, notification_id, message): cn = CommentNotification.query.filter_by(id=notification_id).first() _log.info(u'Sending notification email about {0}'.format(cn)) return send_email( message['from'], [message['to']], message['subject'], message['body'])
def send_comment_email(user, comment, media, request): """ Sends comment email to user when a comment is made on their media. Args: - user: the user object to whom the email is sent - comment: the comment object referencing user's media - media: the media object the comment is about - request: the request """ comment_url = ( request.urlgen( "mediagoblin.user_pages.media_home.view_comment", comment=comment.id, user=media.get_uploader.username, media=media.slug_or_id, qualified=True, ) + "#comment" ) comment_author = comment.get_author.username rendered_email = render_template( request, "mediagoblin/user_pages/comment_email.txt", { "username": user.username, "comment_author": comment_author, "comment_content": comment.content, "comment_url": comment_url, }, ) send_email( mg_globals.app_config["email_sender_address"], [user.email], "{instance_title} - {comment_author} ".format( comment_author=comment_author, instance_title=mg_globals.app_config["html_title"] ) + _("commented on your post"), rendered_email, )
def send_fp_verification_email(user, request): """ Send the verification email to users to change their password. Args: - user: a user object - request: the request """ rendered_email = render_template( request, 'mediagoblin/auth/fp_verification_email.txt', {'username': user.username, 'verification_url': EMAIL_FP_VERIFICATION_TEMPLATE.format( host=request.host, uri=request.urlgen('mediagoblin.auth.verify_forgot_password'), userid=unicode(user.id), fp_verification_key=user.fp_verification_key)}) # TODO: There is no error handling in place send_email( mg_globals.app_config['email_sender_address'], [user.email], 'GNU MediaGoblin - Change forgotten password!', rendered_email)
def send_comment_email(user, comment, media, request): """ Sends comment email to user when a comment is made on their media. Args: - user: the user object to whom the email is sent - comment: the comment object referencing user's media - media: the media object the comment is about - request: the request """ comment_url = request.urlgen( 'mediagoblin.user_pages.media_home.view_comment', comment=comment.id, user=media.get_uploader.username, media=media.slug_or_id, qualified=True) + '#comment' comment_author = comment.get_author.username rendered_email = render_template( request, 'mediagoblin/user_pages/comment_email.txt', { 'username': user.username, 'comment_author': comment_author, 'comment_content': comment.content, 'comment_url': comment_url }) send_email( mg_globals.app_config['email_sender_address'], [user.email], '{instance_title} - {comment_author} '.format( comment_author=comment_author, instance_title=mg_globals.app_config['html_title']) \ + _('commented on your post'), rendered_email)
def take_punitive_actions(request, form, report, user): message_body = '' # The bulk of this action is running through all of the different # punitive actions that a moderator could take. if u'takeaway' in form.action_to_resolve.data: for privilege_name in form.take_away_privileges.data: take_away_privileges(user.username, privilege_name) form.resolution_content.data += \ u"\n{mod} took away {user}\'s {privilege} privileges.".format( mod=request.user.username, user=user.username, privilege=privilege_name) # If the moderator elects to ban the user, a new instance of user_ban # will be created. if u'userban' in form.action_to_resolve.data: user_ban = ban_user(form.targeted_user.data, expiration_date=form.user_banned_until.data, reason=form.why_user_was_banned.data) Session.add(user_ban) form.resolution_content.data += \ u"\n{mod} banned user {user} {expiration_date}.".format( mod=request.user.username, user=user.username, expiration_date = ( "until {date}".format(date=form.user_banned_until.data) if form.user_banned_until.data else "indefinitely" ) ) # If the moderator elects to send a warning message. An email will be # sent to the email address given at sign up if u'sendmessage' in form.action_to_resolve.data: message_body = form.message_to_user.data form.resolution_content.data += \ u"\n{mod} sent a warning email to the {user}.".format( mod=request.user.username, user=user.username) if u'delete' in form.action_to_resolve.data and \ report.is_comment_report(): deleted_comment = report.comment Session.delete(deleted_comment) form.resolution_content.data += \ u"\n{mod} deleted the comment.".format( mod=request.user.username) elif u'delete' in form.action_to_resolve.data and \ report.is_media_entry_report(): deleted_media = report.media_entry deleted_media.delete() form.resolution_content.data += \ u"\n{mod} deleted the media entry.".format( mod=request.user.username) report.archive(resolver_id=request.user.id, resolved=datetime.now(), result=form.resolution_content.data) Session.add(report) Session.commit() if message_body: send_email( mg_globals.app_config['email_sender_address'], [user.email], _('Warning from') + '- {moderator} '.format(moderator=request.user.username), message_body) return redirect(request, 'mediagoblin.moderation.users_detail', user=user.username)
def run(self, notification_id, message): cn = Notification.query.filter_by(id=notification_id).first() _log.info(u"Sending notification email about {0}".format(cn)) return send_email(message["from"], [message["to"]], message["subject"], message["body"])