first setup configuration
Build LinkLog Development Image / development-image (push) Successful in 10s
Build LinkLog Development Image / development-image (push) Successful in 10s
This commit is contained in:
@@ -3,31 +3,71 @@
|
||||
|
||||
from email.message import EmailMessage
|
||||
from smtplib import SMTP
|
||||
import json
|
||||
|
||||
from backend.app.core.config import settings
|
||||
from backend.app.database import get_connection
|
||||
|
||||
|
||||
def get_smtp_settings() -> dict:
|
||||
values = {
|
||||
'smtp_host': settings.smtp_host,
|
||||
'smtp_port': settings.smtp_port,
|
||||
'smtp_username': settings.smtp_username,
|
||||
'smtp_password': settings.smtp_password,
|
||||
'smtp_from': settings.smtp_from,
|
||||
'smtp_use_tls': settings.smtp_use_tls,
|
||||
}
|
||||
with get_connection() as conn:
|
||||
row = conn.execute('SELECT value FROM app_settings WHERE name = ?', ('smtp',)).fetchone()
|
||||
if row:
|
||||
values.update(json.loads(row['value']))
|
||||
return values
|
||||
|
||||
|
||||
def save_smtp_settings(values: dict) -> None:
|
||||
with get_connection() as conn:
|
||||
conn.execute(
|
||||
'''INSERT INTO app_settings (name, value, updated_at) VALUES (?, ?, CURRENT_TIMESTAMP)
|
||||
ON CONFLICT(name) DO UPDATE SET value = excluded.value, updated_at = CURRENT_TIMESTAMP''',
|
||||
('smtp', json.dumps(values)),
|
||||
)
|
||||
conn.commit()
|
||||
|
||||
|
||||
def smtp_configured() -> bool:
|
||||
return bool(settings.smtp_host and settings.smtp_from)
|
||||
smtp = get_smtp_settings()
|
||||
return bool(smtp['smtp_host'] and smtp['smtp_from'])
|
||||
|
||||
|
||||
def send_verification_email(email: str, username: str, verification_url: str) -> None:
|
||||
def send_message(email: str, subject: str, body: str) -> None:
|
||||
smtp = get_smtp_settings()
|
||||
if not smtp_configured():
|
||||
raise RuntimeError('SMTP is not configured; set LINKLOG_SMTP_HOST and LINKLOG_SMTP_FROM')
|
||||
|
||||
message = EmailMessage()
|
||||
message['Subject'] = 'Verify your LinkLog email address'
|
||||
message['From'] = settings.smtp_from
|
||||
message['Subject'] = subject
|
||||
message['From'] = smtp['smtp_from']
|
||||
message['To'] = email
|
||||
message.set_content(
|
||||
message.set_content(body)
|
||||
|
||||
with SMTP(smtp['smtp_host'], smtp['smtp_port'], timeout=10) as connection:
|
||||
if smtp['smtp_use_tls']:
|
||||
connection.starttls()
|
||||
if smtp['smtp_username']:
|
||||
connection.login(smtp['smtp_username'], smtp['smtp_password'])
|
||||
connection.send_message(message)
|
||||
|
||||
|
||||
def send_verification_email(email: str, username: str, verification_url: str) -> None:
|
||||
send_message(
|
||||
email,
|
||||
'Verify your LinkLog email address',
|
||||
f'Hello {username},\n\n'
|
||||
f'Verify your LinkLog email address by opening this link:\n{verification_url}\n\n'
|
||||
f'This link expires in {settings.email_verification_expiry_hours} hours.\n'
|
||||
f'This link expires in {settings.email_verification_expiry_hours} hours.\n',
|
||||
)
|
||||
|
||||
with SMTP(settings.smtp_host, settings.smtp_port, timeout=10) as smtp:
|
||||
if settings.smtp_use_tls:
|
||||
smtp.starttls()
|
||||
if settings.smtp_username:
|
||||
smtp.login(settings.smtp_username, settings.smtp_password)
|
||||
smtp.send_message(message)
|
||||
|
||||
def send_test_email(email: str) -> None:
|
||||
send_message(email, 'LinkLog SMTP test', 'This is a test message from LinkLog. SMTP is configured correctly.\n')
|
||||
Reference in New Issue
Block a user