Skip to content

Commit

Permalink
#186 : Fixed email issue while registration
Browse files Browse the repository at this point in the history
  • Loading branch information
sairohithA007 committed Apr 25, 2019
1 parent ef03744 commit 2ccea00
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 13 deletions.
11 changes: 11 additions & 0 deletions AuthServices/Controllers/templates/confirmRegistration.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>
<body>
<p>Hi {{username}},<br><br>
We are happy that you have joined us. From now on, we will try to keep you happy<br><br>
Thanks,<br>
Utopia Team,<br>
Bloomington,<br>
IN, US.<br>
</p>
</body>
</html>
78 changes: 65 additions & 13 deletions AuthServices/Services/email_service.py
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]"
receiver_email = "[email protected]"
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()
2 changes: 2 additions & 0 deletions AuthServices/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ passlib
kazoo
requests
pika
boto
jinja2

0 comments on commit 2ccea00

Please sign in to comment.