From 2ccea0088e2da3fb2bfc970fe205e45a442b5aa6 Mon Sep 17 00:00:00 2001 From: Sai Rohith Achanta Date: Thu, 25 Apr 2019 18:48:51 -0400 Subject: [PATCH] #186 : Fixed email issue while registration --- .../templates/confirmRegistration.html | 11 +++ AuthServices/Services/email_service.py | 78 +++++++++++++++---- AuthServices/requirements.txt | 2 + 3 files changed, 78 insertions(+), 13 deletions(-) create mode 100644 AuthServices/Controllers/templates/confirmRegistration.html diff --git a/AuthServices/Controllers/templates/confirmRegistration.html b/AuthServices/Controllers/templates/confirmRegistration.html new file mode 100644 index 0000000..0a33bf2 --- /dev/null +++ b/AuthServices/Controllers/templates/confirmRegistration.html @@ -0,0 +1,11 @@ + + +

Hi {{username}},

+ We are happy that you have joined us. From now on, we will try to keep you happy

+ Thanks,
+ Utopia Team,
+ Bloomington,
+ IN, US.
+

+ + diff --git a/AuthServices/Services/email_service.py b/AuthServices/Services/email_service.py index 20a0d60..d30ca8c 100644 --- a/AuthServices/Services/email_service.py +++ b/AuthServices/Services/email_service.py @@ -1,23 +1,75 @@ import smtplib, ssl from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart +import boto.ses +from jinja2 import Environment, PackageLoader sender_email = "eutopia.aaps@gmail.com" receiver_email = "eutopia.aaps@gmail.com" password = "eutopia@2019" +# Loads templates from the yourapp.templates folder +env = Environment(loader=PackageLoader('Controllers', 'templates')) +class Email(object): + def __init__(self, to, subject): + self.to = to + self.subject = subject + self._html = None + self._text = None + + def _render(self, filename, context): + template = env.get_template(filename) + return template.render(context) + + def html(self, filename, context): + self._html = self._render(filename, context) + + def text(self, filename, context): + self._text = self._render(filename, context) + + + def send(self, from_addr=None): + body = self._html + + if not from_addr: + from_addr = sender_email + if not self._html and not self._text: + raise Exception('You must provide a text or html body.') + if not self._html: + body = self._text + + connection = boto.ses.connect_to_region( + 'us-east-1', + aws_access_key_id='AKIAJAR4RJMB6DDV6HEA', + aws_secret_access_key='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' + ) + + return connection.send_email( + from_addr, + self.subject, + None, + self.to, + text_body=self._text, + html_body=self._html + ) + + class EmailService: def send_email(self, receiver_email, subject, content): - message = MIMEMultipart("alternative") - message["Subject"] = subject - message["From"] = sender_email - message["To"] = receiver_email - html_content = MIMEText(content, "html") - message.attach(html_content) - - # Create secure connection with server and send email - with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=None) as server: - server.login(sender_email, password) - server.sendmail( - sender_email, receiver_email, message.as_string() - ) + # message = MIMEMultipart("alternative") + # message["Subject"] = subject + # message["From"] = sender_email + # message["To"] = receiver_email + # html_content = MIMEText(content, "html") + # message.attach(html_content) + # + # # Create secure connection with server and send email + # with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=None) as server: + # server.login(sender_email, password) + # server.sendmail( + # sender_email, receiver_email, message.as_string() + # ) + email = Email(to=receiver_email, subject=subject) + ctx = {'username': content} + email.html('confirmRegistration.html', ctx) + email.send() diff --git a/AuthServices/requirements.txt b/AuthServices/requirements.txt index 4aecbac..e3efb88 100644 --- a/AuthServices/requirements.txt +++ b/AuthServices/requirements.txt @@ -8,3 +8,5 @@ passlib kazoo requests pika +boto +jinja2