DocScoDoc/app/email.py

87 lines
2.4 KiB
Python
Raw Normal View History

2021-05-29 18:22:51 +02:00
# -*- coding: UTF-8 -*
2022-03-04 20:02:50 +01:00
##############################################################################
# ScoDoc
# Copyright (c) 1999 - 2022 Emmanuel Viennet. All rights reserved.
# See LICENSE
##############################################################################
2021-05-29 18:22:51 +02:00
from threading import Thread
2022-03-04 20:02:50 +01:00
from flask import current_app, g
2021-05-29 18:22:51 +02:00
from flask_mail import Message
2022-03-04 20:02:50 +01:00
2021-05-29 18:22:51 +02:00
from app import mail
2022-03-04 20:02:50 +01:00
from app.scodoc import sco_preferences
2021-05-29 18:22:51 +02:00
def send_async_email(app, msg):
2022-04-02 10:56:10 +02:00
"Send an email, async"
2021-05-29 18:22:51 +02:00
with app.app_context():
mail.send(msg)
2021-08-26 23:43:54 +02:00
def send_email(
2022-03-04 20:02:50 +01:00
subject: str,
sender: str,
recipients: list,
text_body: str,
html_body="",
bcc=(),
attachments=(),
2021-08-26 23:43:54 +02:00
):
"""
2022-03-04 20:02:50 +01:00
Send an email. _All_ ScoDoc mails SHOULD be sent using this function.
2021-08-26 23:43:54 +02:00
If html_body is specified, build a multipart message with HTML content,
else send a plain text email.
2022-03-04 20:02:50 +01:00
attachements: list of dict { 'filename', 'mimetype', 'data' }
2021-08-26 23:43:54 +02:00
"""
2022-03-04 20:02:50 +01:00
msg = Message(subject, sender=sender, recipients=recipients, bcc=bcc)
2021-05-29 18:22:51 +02:00
msg.body = text_body
msg.html = html_body
2022-03-04 20:02:50 +01:00
if attachments:
for attachment in attachments:
msg.attach(
attachment["filename"], attachment["mimetype"], attachment["data"]
)
2021-08-26 23:43:54 +02:00
send_message(msg)
2022-03-04 20:02:50 +01:00
def send_message(msg: Message):
"""Send a message.
All ScoDoc emails MUST be sent by this function.
In mail debug mode, addresses are discarded and all mails are sent to the
specified debugging address.
"""
if hasattr(g, "scodoc_dept"):
# on est dans un département, on peut accéder aux préférences
email_test_mode_address = sco_preferences.get_preference(
"email_test_mode_address"
)
if email_test_mode_address:
# Mode spécial test: remplace les adresses de destination
orig_to = msg.recipients
orig_cc = msg.cc
orig_bcc = msg.bcc
msg.recipients = [email_test_mode_address]
msg.cc = None
msg.bcc = None
msg.subject = "[TEST SCODOC] " + msg.subject
msg.body = (
f"""--- Message ScoDoc dérouté pour tests ---
Adresses d'origine:
to : {orig_to}
cc : {orig_cc}
bcc: {orig_bcc}
---
\n\n"""
+ msg.body
)
2021-05-29 18:22:51 +02:00
Thread(
target=send_async_email, args=(current_app._get_current_object(), msg)
).start()