From 1fb3d381e832d5c5871c6cb2fd8b2c1ae3dbd029 Mon Sep 17 00:00:00 2001 From: Sashi20 Date: Thu, 20 Feb 2020 12:58:59 +0530 Subject: Add user registration, login and abstract submission interfaces --- arduino_blog/send_emails.py | 60 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 arduino_blog/send_emails.py (limited to 'arduino_blog/send_emails.py') diff --git a/arduino_blog/send_emails.py b/arduino_blog/send_emails.py new file mode 100644 index 0000000..59dd824 --- /dev/null +++ b/arduino_blog/send_emails.py @@ -0,0 +1,60 @@ +# Local imports +try: + from string import letters +except ImportError: + from string import ascii_letters as letters +from string import digits, punctuation +import hashlib +from textwrap import dedent +import os + +# Django imports +from django.utils.crypto import get_random_string +from django.conf import settings +from django.core.mail import EmailMultiAlternatives, send_mail +from django.core.files.storage import default_storage +from django.core.files.base import ContentFile + + +def generate_activation_key(username): + """ Generate hashed secret key for email activation """ + chars = letters + digits + punctuation + secret_key = get_random_string(20, chars) + return hashlib.sha256((secret_key + username).encode('utf-8')).hexdigest() + + +def send_user_mail(user_mail, key): + """ Send mail to user whose email is to be verified + This function should get two args i.e user_email and secret_key. + The activation url is generated from settings.PRODUCTION_URL and key. + """ + try: + to = user_mail + subject = 'Arduino - User Registration Confirmation' + message = dedent("""\ + To activate your account and verify your email address, + please click the following link: + {0}/activate/{1} + If clicking the link above does not work, + copy and paste the URL in a new browser window instead. + For any issue, please write us on {2} + Regards, + {3} + """.format(settings.PRODUCTION_URL, key, settings.REPLY_EMAIL, + settings.SENDER_NAME + ) + ) + + send_mail(subject, message, settings.SENDER_EMAIL, [to]) + + msg = "An activation link is sent to your registered email.\ + Please activate the link within 60 minutes." + success = True + + except Exception as exc_msg: + msg = """Error: {0}. Please check your email address.\ + If email address is correct then + Please contact {1}.""".format(exc_msg, settings.REPLY_EMAIL) + success = False + + return success, msg \ No newline at end of file -- cgit