Skip to content

Commit

Permalink
Added support for sending email via smtp
Browse files Browse the repository at this point in the history
  Also kept support for the old method
  • Loading branch information
caparker committed Jun 5, 2024
1 parent cc3c1cd commit ff4ab3b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
9 changes: 9 additions & 0 deletions openaq_api/openaq_api/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ class Settings(BaseSettings):

EMAIL_SENDER: str | None = None

SMTP_EMAIL_HOST: str | None = None
SMTP_EMAIL_USER: str | None = None
SMTP_EMAIL_PASSWORD: str | None = None

EXPLORER_API_KEY: str

@computed_field(return_type=str, alias="DATABASE_READ_URL")
Expand All @@ -49,6 +53,11 @@ def DATABASE_READ_URL(self):
def DATABASE_WRITE_URL(self):
return f"postgresql://{self.DATABASE_WRITE_USER}:{self.DATABASE_WRITE_PASSWORD}@{self.DATABASE_HOST}:{self.DATABASE_PORT}/{self.DATABASE_DB}"

@computed_field(return_type=str, alias="DATABASE_WRITE_URL")
@property
def USE_SMTP_EMAIL(self):
return None not in [self.SMTP_EMAIL_HOST, self.SMTP_EMAIL_USER, self.SMTP_EMAIL_PASSWORD]

model_config = SettingsConfigDict(extra="ignore", env_file=get_env())


Expand Down
45 changes: 25 additions & 20 deletions openaq_api/openaq_api/v3/routers/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import pathlib
from email.message import EmailMessage
import smtplib

import boto3
from fastapi import APIRouter, Body, Depends, HTTPException, Request, status
Expand All @@ -26,6 +27,25 @@
include_in_schema=False,
)

def send_email(destination_email: str, msg: EmailMessage):
if settings.USE_SMTP_EMAIL:
return send_smtp_email(destination_email, msg)
else:
return send_ses_email(destination_email, msg)

def send_smpt_email(destination_email: str, msg: EmailMessage):
with smtplib.SMTP(settings.SMTP_EMAIL_HOST, 587) as s:
s.starttls()
s.login(settings.SMTP_EMAIL_USER, settings.SMTP_EMAIL_PASSWORD)
return server.sendmail(settings.EMAIL_SENDER, destination_email, msg.as_string())


def send_ses_email(destination_email: str, msg: EmailMessage):
return ses_client.send_raw_email(
Source=settings.EMAIL_SENDER,
Destinations=[f"{full_name} <{destination_email}>"],
RawMessage={"Data": msg.as_string()},
)

def send_change_password_email(full_name: str, email: str):
ses_client = boto3.client("ses")
Expand Down Expand Up @@ -55,11 +75,7 @@ def send_change_password_email(full_name: str, email: str):
msg["Subject"] = "OpenAQ Explorer - Password changed"
msg["From"] = settings.EMAIL_SENDER
msg["To"] = email
response = ses_client.send_raw_email(
Source=settings.EMAIL_SENDER,
Destinations=[f"{full_name} <{email}>"],
RawMessage={"Data": msg.as_string()},
)
response = send_smtp_email(email, msg)
logger.info(
SESEmailLog(
detail=json.dumps(
Expand Down Expand Up @@ -103,11 +119,7 @@ def send_verification_email(verification_code: str, full_name: str, email: str):
msg["Subject"] = "OpenAQ Explorer - Verify your email"
msg["From"] = settings.EMAIL_SENDER
msg["To"] = email
response = ses_client.send_raw_email(
Source=settings.EMAIL_SENDER,
Destinations=[f"{full_name} <{email}>"],
RawMessage={"Data": msg.as_string()},
)
response = send_smtp_email(email, msg)
logger.info(
SESEmailLog(
detail=json.dumps(
Expand Down Expand Up @@ -151,11 +163,7 @@ def send_password_reset_email(verification_code: str, email: str):
msg["Subject"] = "OpenAQ Explorer - Reset password request"
msg["From"] = settings.EMAIL_SENDER
msg["To"] = email
response = ses_client.send_raw_email(
Source=settings.EMAIL_SENDER,
Destinations=[f"<{email}>"],
RawMessage={"Data": msg.as_string()},
)
response = send_smtp_email(email, msg)
logger.info(
SESEmailLog(
detail=json.dumps(
Expand Down Expand Up @@ -199,11 +207,8 @@ def send_password_changed_email(email: str):
msg["Subject"] = "OpenAQ Explorer - Reset password success"
msg["From"] = settings.EMAIL_SENDER
msg["To"] = email
response = ses_client.send_raw_email(
Source=settings.EMAIL_SENDER,
Destinations=[f"<{email}>"],
RawMessage={"Data": msg.as_string()},
)
response = send_smtp_email(email, msg)

logger.info(
SESEmailLog(
detail=json.dumps(
Expand Down

0 comments on commit ff4ab3b

Please sign in to comment.