From b5b308a75764bc632baeda4d23481f0b82aa5182 Mon Sep 17 00:00:00 2001 From: nswain Date: Wed, 26 Aug 2020 10:37:07 -0600 Subject: [PATCH 1/3] Removes embedded bytestring syntax from email message body. e.g.: "b'Hello user, here's your code: 81244'" -> "Hello user, here's your code: 81244" --- mfa/Email.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mfa/Email.py b/mfa/Email.py index f873e18..37ccdc8 100644 --- a/mfa/Email.py +++ b/mfa/Email.py @@ -14,7 +14,9 @@ def sendEmail(request,username,secret): kwargs = {key: username} user = User.objects.get(**kwargs) res=render(request,"mfa_email_token_template.html",{"request":request,"user":user,'otp':secret}) - return send([user.email],"OTP", str(res.content)) + body = str(res.content).replace("b'", "") + body = body[:-1] if body.endswith("'") else body + return send([user.email],"OTP", body) @never_cache def start(request): From d48e464c16b6efb5ef9fbde7b3f3ae287355ec89 Mon Sep 17 00:00:00 2001 From: nswain Date: Wed, 26 Aug 2020 10:39:01 -0600 Subject: [PATCH 2/3] Use DEFAULT_FROM_EMAIL instead of EMAIL_HOST_USER for the from email address if EMAIL_HOST_USER does not have an "@" sign in it. Some email relay services require a username that is not an email address for the EMAIL_HOST_USER (e.g.: https://sendgrid.com/docs/API_Reference/SMTP_API/integrating_with_the_smtp_api.html) --- mfa/Common.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mfa/Common.py b/mfa/Common.py index c2f36f7..340c82b 100644 --- a/mfa/Common.py +++ b/mfa/Common.py @@ -2,7 +2,10 @@ from django.conf import settings from django.core.mail import EmailMessage def send(to,subject,body): - From = "%s <%s>" % (settings.EMAIL_FROM, settings.EMAIL_HOST_USER) + from_email_address = settings.EMAIL_HOST_USER + if '@' not in from_email_address: + from_email_address = settings.DEFAULT_FROM_EMAIL + From = "%s <%s>" % (settings.EMAIL_FROM, from_email_address) email = EmailMessage(subject,body,From,to) email.content_subtype = "html" return email.send(False) \ No newline at end of file From c34efd6ba94145db1629f9fe422ba54d91d4f69f Mon Sep 17 00:00:00 2001 From: nswain Date: Wed, 9 Sep 2020 09:04:44 -0600 Subject: [PATCH 3/3] Use decode method to decode the byte string instead of casting it to string and replacing the byte string syntax in the string. --- mfa/Email.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/mfa/Email.py b/mfa/Email.py index 37ccdc8..a2c3119 100644 --- a/mfa/Email.py +++ b/mfa/Email.py @@ -14,9 +14,7 @@ def sendEmail(request,username,secret): kwargs = {key: username} user = User.objects.get(**kwargs) res=render(request,"mfa_email_token_template.html",{"request":request,"user":user,'otp':secret}) - body = str(res.content).replace("b'", "") - body = body[:-1] if body.endswith("'") else body - return send([user.email],"OTP", body) + return send([user.email],"OTP", res.content.decode()) @never_cache def start(request):