* backend: - refactor invite system, move to separate InviteOps object, used by archives and user - supporting three invite use cases: 1) superuser invites any user not registered, not added to any archive 2) archive admin invites any user not registered, add to one of their archives 3) archive admin invites existing registered user, add to one of their archives - support superadmin invite via /users/invite (fixes #37) - superadmin invite has no archive set and does not add user to archive - don't send verification email when accepting from invite, fixes #50 - use different email template / accept url for existing user invite, eg, `/invite/accept/` - fix default token value in chart
80 lines
2.5 KiB
Python
80 lines
2.5 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}/verify?token={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}/join/{token}?email={receiver_email}
|
|
|
|
The invite token is: {token}"""
|
|
|
|
self._send_encrypted(receiver_email, message)
|
|
|
|
def send_existing_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}/invite/accept/{token}?email={receiver_email}
|
|
|
|
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)
|