88from couchers .config import config
99from couchers .crypto import EMAIL_SOURCE_DATA_KEY_NAME , random_hex , simple_hash_signature
1010from couchers .models import Email
11+ from couchers .proto .internal import jobs_pb2
1112
1213template_base = Path (Path (__file__ ).parent / ".." / ".." / ".." / "templates" / "v2" )
1314
@@ -18,51 +19,43 @@ def make_cid(sender_email: str) -> tuple[str, str]:
1819 return cid , without_tag
1920
2021
21- def send_smtp_email (
22- sender_name : str ,
23- sender_email : str ,
24- recipient : str ,
25- subject : str ,
26- plain : str ,
27- html : str ,
28- list_unsubscribe_header : str | None ,
29- source_data : str | None ,
30- ) -> Email :
22+ def send_smtp_email (payload : jobs_pb2 .SendEmailPayload ) -> Email :
3123 """
3224 Sends out the email through SMTP, settings from config.
3325
3426 Returns a models.Email object that can be straight away added to the database.
3527 """
3628 message_id = random_hex ()
3729 msg = EmailMessage ()
38- msg ["Subject" ] = subject
39- msg ["From" ] = Address (sender_name , addr_spec = sender_email )
40- msg ["To" ] = Address (addr_spec = recipient )
30+ msg ["Subject" ] = payload . subject
31+ msg ["From" ] = Address (payload . sender_name , addr_spec = payload . sender_email )
32+ msg ["To" ] = Address (addr_spec = payload . recipient )
4133 msg ["X-Couchers-ID" ] = message_id
4234
43- if list_unsubscribe_header :
44- msg ["List-Unsubscribe" ] = list_unsubscribe_header
35+ if payload . list_unsubscribe_header :
36+ msg ["List-Unsubscribe" ] = payload . list_unsubscribe_header
4537
46- if source_data :
47- msg ["X-Couchers-Source-Data" ] = source_data
48- msg ["X-Couchers-Source-Sig" ] = simple_hash_signature (source_data , EMAIL_SOURCE_DATA_KEY_NAME )
38+ if payload . source_data :
39+ msg ["X-Couchers-Source-Data" ] = payload . source_data
40+ msg ["X-Couchers-Source-Sig" ] = simple_hash_signature (payload . source_data , EMAIL_SOURCE_DATA_KEY_NAME )
4941
50- msg .set_content (plain )
42+ msg .set_content (payload . plain )
5143
52- if html :
44+ updated_html = payload .html
45+ if updated_html :
5346 # for any png files in attachment_imgs/, goes through and replaces instances of the filename with attachment
5447 used_attachments = []
5548 for attachment in (template_base / "attachment_imgs" ).glob ("*.png" ):
5649 attachment_html_path = str (attachment .relative_to (template_base ))
57- if attachment_html_path not in html :
50+ if attachment_html_path not in updated_html :
5851 continue
5952 # it's used in this template, so attach and replace it
6053 data = attachment .read_bytes ()
61- cid , wcid = make_cid (sender_email )
62- html = html .replace (attachment_html_path , f"cid:{ wcid } " )
54+ cid , wcid = make_cid (payload . sender_email )
55+ updated_html = updated_html .replace (attachment_html_path , f"cid:{ wcid } " )
6356 used_attachments .append ((cid , "image" , "png" , data ))
6457
65- msg .add_alternative (html , subtype = "html" )
58+ msg .add_alternative (updated_html , subtype = "html" )
6659
6760 for cid , mime_type , mime_subtype , data in used_attachments :
6861 payloads = cast (list [MIMEPart ], msg .get_payload ())
@@ -75,16 +68,16 @@ def send_smtp_email(
7568 # stmplib docs recommend calling ehlo() before and after starttls()
7669 server .ehlo ()
7770 server .login (config ["SMTP_USERNAME" ], config ["SMTP_PASSWORD" ])
78- server .sendmail (sender_email , recipient , msg .as_string ())
71+ server .sendmail (payload . sender_email , payload . recipient , msg .as_string ())
7972
8073 return Email (
8174 id = message_id ,
82- sender_name = sender_name ,
83- sender_email = sender_email ,
84- recipient = recipient ,
85- subject = subject ,
86- plain = plain ,
87- html = html ,
88- list_unsubscribe_header = list_unsubscribe_header ,
89- source_data = source_data ,
75+ sender_name = payload . sender_name ,
76+ sender_email = payload . sender_email ,
77+ recipient = payload . recipient ,
78+ subject = payload . subject ,
79+ plain = payload . plain ,
80+ html = updated_html ,
81+ list_unsubscribe_header = payload . list_unsubscribe_header ,
82+ source_data = payload . source_data ,
9083 )
0 commit comments