Coverage for model/utils/smtp.py: 100%
26 statements
« prev ^ index » next coverage.py v7.3.2, created at 2024-11-05 04:22 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2024-11-05 04:22 +0000
1from email.mime.multipart import MIMEMultipart
2from email.mime.text import MIMEText
3from smtplib import SMTP
4from typing import Optional, Iterable
6import os
7import threading
9__all__ = ['send_noreply']
12def send(
13 from_addr: str,
14 password: Optional[str],
15 to_addrs: Iterable[str],
16 subject: str,
17 text: str,
18 html: str,
19):
20 SMTP_SERVER = os.environ.get('SMTP_SERVER')
21 if SMTP_SERVER is None:
22 return
23 with SMTP(SMTP_SERVER, 587) as server:
24 if password is not None:
25 server.login(from_addr, password)
26 msg = MIMEMultipart('alternative')
27 msg['From'] = from_addr
28 msg['To'] = ', '.join(to_addrs)
29 msg['Subject'] = subject
30 msg.attach(MIMEText(text, 'plain'))
31 msg.attach(MIMEText(html, 'html'))
32 server.send_message(msg, from_addr, to_addrs)
35def send_noreply(
36 to_addrs: Iterable[str],
37 subject: str,
38 text: str,
39 html: Optional[str] = None,
40):
41 SMTP_NOREPLY = os.environ.get('SMTP_NOREPLY')
42 SMTP_NOREPLY_PASSWORD = os.environ.get('SMTP_NOREPLY_PASSWORD')
43 args = (
44 SMTP_NOREPLY,
45 SMTP_NOREPLY_PASSWORD,
46 to_addrs,
47 subject,
48 text,
49 html or text,
50 )
51 threading.Thread(target=send, args=args).start()