Coverage for model/utils/smtp.py: 100%

23 statements  

« prev     ^ index     » next       coverage.py v7.9.2, created at 2026-07-24 16:42 +0000

1from email.mime.multipart import MIMEMultipart 

2from email.mime.text import MIMEText 

3from smtplib import SMTP 

4from typing import Optional, Iterable 

5 

6import threading 

7 

8from config import settings 

9 

10__all__ = ['send_noreply'] 

11 

12 

13def send( 

14 from_addr: str, 

15 password: Optional[str], 

16 to_addrs: Iterable[str], 

17 subject: str, 

18 text: str, 

19 html: str, 

20): 

21 if settings.SMTP_SERVER is None: 

22 return 

23 with SMTP(settings.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) 

33 

34 

35def send_noreply( 

36 to_addrs: Iterable[str], 

37 subject: str, 

38 text: str, 

39 html: Optional[str] = None, 

40): 

41 args = ( 

42 settings.SMTP_NOREPLY, 

43 settings.SMTP_NOREPLY_PASSWORD, 

44 to_addrs, 

45 subject, 

46 text, 

47 html or text, 

48 ) 

49 threading.Thread(target=send, args=args).start()