Skip to content
Snippets Groups Projects
Verified Commit 07cc9c1a authored by !! Julian Keck (old Account; do not use) !!'s avatar !! Julian Keck (old Account; do not use) !! :ghost:
Browse files

UPD: attach patch_request raw json to email instead of printing it

parent 26629c94
No related branches found
No related tags found
No related merge requests found
Pipeline #400044 passed
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:
......
......@@ -90,10 +90,4 @@
{% endfor %}
<p>Have a nice day!</p>
<br><br>
Raw request:
<pre>
{{ raw_action_json | default('[]') }}
</pre>
</body>
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())
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment