Fixing the endpoint used to send emails

pull/157/head
Matheus Lima 2019-05-23 13:29:23 -03:00
parent 26fe7a2fc9
commit 875b17f5c7
2 changed files with 14 additions and 13 deletions

View File

@ -33,16 +33,6 @@ public.config.from_object(get_base_config())
public.config['GOOGLE_STT_KEY'] = os.environ['GOOGLE_STT_KEY']
public.config['SELENE_CACHE'] = SeleneCache()
# Initializing email client
host = os.environ['EMAIL_SERVICE_HOST']
port = os.environ['EMAIL_SERVICE_PORT']
user = os.environ['EMAIL_SERVICE_USER']
password = os.environ['EMAIL_SERVICE_PASSWORD']
# TODO: test with another email service and move this logic to the email endpoint
#email_client = smtplib.SMTP(host, port)
#email_client.login(user, password)
#public.config['EMAIL_CLIENT'] = email_client
public.config['METRICS_SERVICE'] = MetricsService()
public.response_class = SeleneResponse

View File

@ -1,4 +1,5 @@
import json
import os
import smtplib
from email.message import EmailMessage
from http import HTTPStatus
@ -21,7 +22,6 @@ class DeviceEmailEndpoint(PublicEndpoint):
def __init__(self):
super(DeviceEmailEndpoint, self).__init__()
self.email_client: smtplib.SMTP = self.config['EMAIL_CLIENT']
def post(self, device_id):
self._authenticate(device_id)
@ -37,9 +37,20 @@ class DeviceEmailEndpoint(PublicEndpoint):
message['From'] = str(send_email.sender)
message.set_content(str(send_email.body))
message['To'] = account.email_address
self.email_client.send_message(message)
self.email_client.quit()
self._send_email(message)
response = '', HTTPStatus.OK
else:
response = '', HTTPStatus.NO_CONTENT
return response
def _send_email(self, message: EmailMessage):
email_client = self.config.get('EMAIL_CLIENT')
if email_client is None:
host = os.environ['EMAIL_SERVICE_HOST']
port = os.environ['EMAIL_SERVICE_PORT']
user = os.environ['EMAIL_SERVICE_USER']
password = os.environ['EMAIL_SERVICE_PASSWORD']
email_client = smtplib.SMTP(host, port)
email_client.login(user, password)
email_client.send_message(message)
email_client.quit()