mirror of
https://git.v0id.ovh/n3wt-innov/n3wt-school.git
synced 2026-01-28 23:43:22 +00:00
chore: Application du linter
This commit is contained in:
@ -50,6 +50,8 @@ class SendEmailView(APIView):
|
||||
def post(self, request):
|
||||
data = request.data
|
||||
recipients = data.get('recipients', [])
|
||||
cc = data.get('cc', [])
|
||||
bcc = data.get('bcc', [])
|
||||
subject = data.get('subject', 'Notification')
|
||||
message = data.get('message', '')
|
||||
establishment_id = data.get('establishment_id', '')
|
||||
@ -63,9 +65,12 @@ class SendEmailView(APIView):
|
||||
|
||||
# Envoyer l'email
|
||||
return mailer.sendMail(
|
||||
recipients=recipients,
|
||||
subject=subject,
|
||||
message=message,
|
||||
recipients=recipients,
|
||||
cc=cc,
|
||||
bcc=bcc,
|
||||
attachments=[],
|
||||
connection=connection
|
||||
)
|
||||
except NotFound as e:
|
||||
|
||||
@ -34,14 +34,31 @@ def getConnection(id_establishement):
|
||||
raise NotFound(f"Aucun paramètre SMTP trouvé pour l'établissement {id_establishement}")
|
||||
|
||||
|
||||
def sendMail(recipients, subject, message, connection=None):
|
||||
def sendMail(subject, message, recipients, cc=[], bcc=[], attachments=[], connection=None):
|
||||
try:
|
||||
plain_message = strip_tags(message)
|
||||
from_email = settings.EMAIL_HOST_USER
|
||||
if connection is None:
|
||||
send_mail(subject, plain_message, from_email, recipients, html_message=message, fail_silently=False)
|
||||
else:
|
||||
send_mail(subject, plain_message, from_email, recipients, html_message=message, connection=connection, fail_silently=False)
|
||||
if connection is not None:
|
||||
from_email = connection.username
|
||||
|
||||
email = EmailMultiAlternatives(
|
||||
subject=subject,
|
||||
body=plain_message,
|
||||
from_email=from_email,
|
||||
to=recipients,
|
||||
cc=cc,
|
||||
bcc=bcc,
|
||||
connection=connection
|
||||
)
|
||||
email.attach_alternative(message, "text/html")
|
||||
|
||||
# Ajout des pièces jointes
|
||||
for attachment in attachments:
|
||||
# attachment doit être un tuple (filename, content, mimetype)
|
||||
# ex: ("document.pdf", fichier.read(), "application/pdf")
|
||||
email.attach(*attachment)
|
||||
|
||||
email.send(fail_silently=False)
|
||||
return Response({'message': 'Email envoyé avec succès.'}, status=status.HTTP_200_OK)
|
||||
except Exception as e:
|
||||
return Response({'error': str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||
@ -56,7 +73,7 @@ def envoieReinitMotDePasse(recipients, code):
|
||||
}
|
||||
subject = EMAIL_REINIT_SUBJECT
|
||||
html_message = render_to_string('emails/resetPassword.html', context)
|
||||
sendMail(recipients, subject, html_message)
|
||||
sendMail(subject, html_message, recipients)
|
||||
|
||||
except Exception as e:
|
||||
errorMessage = str(e)
|
||||
@ -77,7 +94,7 @@ def sendRegisterForm(recipients, establishment_id):
|
||||
|
||||
subject = EMAIL_INSCRIPTION_SUBJECT
|
||||
html_message = render_to_string('emails/inscription.html', context)
|
||||
sendMail(recipients, subject, html_message)
|
||||
sendMail(subject, html_message, recipients)
|
||||
|
||||
|
||||
except Exception as e:
|
||||
@ -98,7 +115,7 @@ def sendMandatSEPA(recipients, establishment_id):
|
||||
|
||||
subject = EMAIL_INSCRIPTION_SUBJECT
|
||||
html_message = render_to_string('emails/sepa.html', context)
|
||||
sendMail(recipients, subject, html_message)
|
||||
sendMail(subject, html_message, recipients)
|
||||
|
||||
except Exception as e:
|
||||
errorMessage = str(e)
|
||||
@ -110,7 +127,7 @@ def envoieRelanceDossierInscription(recipients, code):
|
||||
EMAIL_RELANCE_CORPUS = 'Bonjour,\nN\'ayant pas eu de retour de votre part, nous vous renvoyons le lien vers le formulaire d\'inscription : ' + BASE_URL + '/users/login\nCordialement'
|
||||
errorMessage = ''
|
||||
try:
|
||||
sendMail(recipients, EMAIL_RELANCE_SUBJECT, EMAIL_RELANCE_CORPUS%str(code))
|
||||
sendMail(EMAIL_RELANCE_SUBJECT, EMAIL_RELANCE_CORPUS%str(code), recipients)
|
||||
|
||||
except Exception as e:
|
||||
errorMessage = str(e)
|
||||
|
||||
Reference in New Issue
Block a user