diff --git a/api/patch_request.py b/api/patch_request.py index e55751d0ca5d645a580dbf363bcf388871ebdf65..5af46513743d3094b1e685b03734102cb8eace27 100644 --- a/api/patch_request.py +++ b/api/patch_request.py @@ -1,6 +1,7 @@ import html import json import pathlib +from email.mime.application import MIMEApplication from typing import Optional import jinja2 @@ -356,11 +357,13 @@ def send_patch_request(actions: list[PatchRequestAction], mgr: Mgr, receiver=set raw_data.append(json.loads(action.model_dump_json())) raw_action_json = json.dumps(raw_data, indent=2) + json_attachment = MIMEApplication(raw_action_json, _subtype='json') + json_attachment.add_header('Content-Disposition', 'attachment', filename='request.json') + body = render_jinja_template(path, TEMPLATE, actions=actions, mgr=mgr, host_mode=host_mode, - raw_action_json=raw_action_json, ) building_numbers = ",".join(set([action.building.number for action in actions])) @@ -378,7 +381,8 @@ def send_patch_request(actions: list[PatchRequestAction], mgr: Mgr, receiver=set sender=settings.patch_request_email_sender, reply_to=reply_to or mgr.email, subject=subject, - body=body + body=body, + attachments=[json_attachment], ) return True except Exception as e: diff --git a/templates/patch_request_template.j2 b/templates/patch_request_template.j2 index 3dc98097ebc3b8b21231092b19d38b52aee5bcf8..7ecb09e1e2459abef7168e8378d7796881c26e5a 100644 --- a/templates/patch_request_template.j2 +++ b/templates/patch_request_template.j2 @@ -90,10 +90,4 @@ {% endfor %} <p>Have a nice day!</p> -<br><br> -Raw request: -<pre> -{{ raw_action_json | default('[]') }} -</pre> - </body> diff --git a/util/util.py b/util/util.py index a0eceafd20ef56a2227f465a01100e1d5bc23cc7..f5075ac1d74cc025568ede835fe9c71620e4b2f7 100644 --- a/util/util.py +++ b/util/util.py @@ -1,4 +1,5 @@ import smtplib +from email.mime.application import MIMEApplication from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText @@ -12,13 +13,13 @@ def get_cursor(conn): def send_email(to: str | list[str], sender: str, cc=None, bcc=None, reply_to=None, subject=None, body=None, - body_type: str = 'html'): + body_type: str = 'html', attachments: list[MIMEApplication] = None): if type(to) is not list: to = to.split(',') to_list = [x for x in to if x is not None] - msg = MIMEMultipart('alternative') + msg = MIMEMultipart() msg['From'] = sender msg['Subject'] = subject msg['To'] = ','.join(to_list) @@ -32,6 +33,8 @@ def send_email(to: str | list[str], sender: str, cc=None, bcc=None, reply_to=Non # msg['Auto-Submitted'] = 'auto-generated' msg['Reply-To'] = reply_to msg.attach(MIMEText(body, body_type)) + for attachment in attachments or []: + msg.attach(attachment) with smtplib.SMTP(settings.mail_smarthost) as server: server.sendmail(sender, to_list, msg.as_string())