browsertrix/backend/emailsender.py
Ilya Kreymer d0b54dd752 Enable sending emails in K8S, trigger verification e-mail on registration. (#38)
* k8s: support email configuration
support sending reset password email
fix for #32

* fastapi users: update to latest (8.1.2)
send verification email upon registration

* update to latest fastapi-users(8.1.2), refactor to use UserManager class
ensure verification e-mail sent upon registration, w/o requiring separate apicall
fixes #32

* add email options to default chart/values.yaml

* separate usermanager init from fastapi users init, fix for sending invite emails
2021-11-30 23:50:38 -08:00

66 lines
2.1 KiB
Python

""" Basic Email Sending Support"""
import os
import smtplib
import ssl
# pylint: disable=too-few-public-methods
class EmailSender:
"""SMTP Email Sender"""
def __init__(self):
self.sender = os.environ.get("EMAIL_SENDER")
self.password = os.environ.get("EMAIL_PASSWORD")
self.smtp_server = os.environ.get("EMAIL_SMTP_HOST")
self.host = os.environ.get("APP_ORIGIN")
def _send_encrypted(self, receiver, message):
"""Send Encrypted SMTP Message"""
print(message, flush=True)
if not self.smtp_server:
print("Email: No SMTP Server, not sending", flush=True)
return
context = ssl.create_default_context()
with smtplib.SMTP(self.smtp_server, 587) as server:
server.ehlo() # Can be omitted
server.starttls(context=context)
server.ehlo() # Can be omitted
server.login(self.sender, self.password)
server.sendmail(self.sender, receiver, message)
def send_user_validation(self, receiver_email, token):
"""Send email to validate registration email address"""
message = f"""
Please verify your registration for Browsertrix Cloud for {receiver_email}
You can verify by clicking here: {self.host}/app/verify/{token}
The verification token is: {token}"""
self._send_encrypted(receiver_email, message)
def send_new_user_invite(self, receiver_email, sender, archive_name, token):
"""Send email to invite new user"""
message = f"""
You are invited by {sender} to join their archive, {archive_name} on Browsertrix Cloud!
You can join by clicking here: {self.host}/app/join/{token}
The invite token is: {token}"""
self._send_encrypted(receiver_email, message)
def send_user_forgot_password(self, receiver_email, token):
"""Send password reset email with token"""
message = f"""
We received your password reset request. Please click here: {self.host}/reset-password?token={token}
to create a new password
"""
self._send_encrypted(receiver_email, message)