summaryrefslogtreecommitdiffstats
path: root/packages/meshbay-hub/src/meshbay_hub/mail.py
blob: ce6e2645117e0d97edcfe05d92a5169d9cc0dbae (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""
MeshBay Hub — email sending via localhost Postfix.

Postfix listens on loopback only (inet_interfaces = loopback-only), so no
authentication is needed. See docs/MAIL-SERVER.md for the full setup.
"""

import logging
import smtplib
from email.message import EmailMessage

log = logging.getLogger(__name__)

_hub_domain: str = "meshbay.org"
_hub_url: str = "https://meshbay.org"


def configure(hub_id: str) -> None:
    global _hub_domain, _hub_url
    _hub_domain = hub_id
    _hub_url = f"https://{hub_id}"


def _send(msg: EmailMessage) -> bool:
    try:
        with smtplib.SMTP("localhost", 25, timeout=10) as s:
            s.send_message(msg)
        return True
    except Exception:
        log.exception("Failed to send email to %s", msg["To"])
        return False


def send_verification_code(to: str, code: str) -> None:
    msg = EmailMessage()
    msg["From"] = f"noreply@{_hub_domain}"
    msg["To"] = to
    msg["Subject"] = f"MeshBay — Your verification code: {code}"
    msg.set_content(
        f"Your verification code is: {code}\n"
        "\n"
        "Enter this code to verify your email address.\n"
        "This code expires in 24 hours.\n"
        "\n"
        "If you did not create a MeshBay account, ignore this email.\n"
        "\n"
        f"{_hub_url}\n"
    )
    _send(msg)
    log.info("Verification code sent to %s", _mask_email(to))


def send_email_change_code(to: str, code: str) -> None:
    msg = EmailMessage()
    msg["From"] = f"noreply@{_hub_domain}"
    msg["To"] = to
    msg["Subject"] = f"MeshBay — Confirm your new email: {code}"
    msg.set_content(
        f"Your verification code is: {code}\n"
        "\n"
        "Enter this code to confirm your new email address.\n"
        "This code expires in 24 hours.\n"
        "\n"
        "If you did not request this change, ignore this email.\n"
        "\n"
        f"{_hub_url}\n"
    )
    _send(msg)
    log.info("Email change code sent to %s", _mask_email(to))


def send_invite_notification(
    to: str, code: str, inviter: str, group_name: str,
) -> None:
    msg = EmailMessage()
    msg["From"] = f"noreply@{_hub_domain}"
    msg["To"] = to
    msg["Subject"] = f"MeshBay — {inviter} invited you to {group_name}"
    msg.set_content(
        f"{inviter} invited you to the group \"{group_name}\" on MeshBay.\n"
        "\n"
        f"Your one-time code is: {code}\n"
        "\n"
        "Open the group and enter this code when prompted.\n"
        "The code works once and expires in 7 days.\n"
        "\n"
        f"{_hub_url}\n"
    )
    _send(msg)
    log.info("Invite notification sent to %s", _mask_email(to))


def _mask_email(email: str) -> str:
    local, _, domain = email.partition("@")
    if len(local) <= 2:
        return f"{'*' * len(local)}@{domain}"
    return f"{local[0]}{'*' * (len(local) - 2)}{local[-1]}@{domain}"