Add bcc to current user in send_confirmation_mail
This commit is contained in:
parent
6980298266
commit
7aac91bfac
2 changed files with 18 additions and 6 deletions
|
|
@ -3,7 +3,7 @@ from datetime import date
|
|||
|
||||
from django import forms
|
||||
from django.contrib import admin
|
||||
from django.core.mail import send_mail
|
||||
from django.core.mail import EmailMessage
|
||||
from django.db.models import BooleanField
|
||||
from django.template import loader
|
||||
|
||||
|
|
@ -65,8 +65,17 @@ def send_confirmation_mail(modeladmin, request, queryset):
|
|||
body = loader.render_to_string('email/candidate_confirm_EDE.txt', context)
|
||||
else:
|
||||
body = loader.render_to_string('email/candidate_confirm_FE.txt', context)
|
||||
|
||||
email = EmailMessage(
|
||||
subject=subject,
|
||||
body=body,
|
||||
from_email=from_email,
|
||||
to=to,
|
||||
bcc=[from_email]
|
||||
)
|
||||
|
||||
try:
|
||||
send_mail(subject, body, from_email, to, fail_silently=False)
|
||||
email.send()
|
||||
email_sent += 1
|
||||
except Exception as err:
|
||||
modeladmin.message_user(request, "Échec d’envoi pour le candidat {0} ({1})".format(candidate, err))
|
||||
|
|
|
|||
|
|
@ -40,8 +40,9 @@ class CandidateTests(TestCase):
|
|||
'_selected_action': Candidate.objects.values_list('pk', flat=True)
|
||||
}, follow=True)
|
||||
self.assertEqual(len(mail.outbox), 2)
|
||||
self.assertEqual(mail.outbox[0].recipients(), ['henri@example.org'])
|
||||
self.assertEqual(mail.outbox[1].recipients(), ['joe@example.org'])
|
||||
# Logged-in user also receives as Bcc
|
||||
self.assertEqual(mail.outbox[0].recipients(), ['henri@example.org', 'me@example.org'])
|
||||
self.assertEqual(mail.outbox[1].recipients(), ['joe@example.org', 'me@example.org'])
|
||||
# Mail content differ depending on the section
|
||||
self.assertEqual(mail.outbox[0].body, """Monsieur,
|
||||
|
||||
|
|
@ -77,16 +78,18 @@ me@example.org
|
|||
|
||||
def test_send_confirmation_error(self):
|
||||
ede = Section.objects.create(name='EDE')
|
||||
Candidate.objects.create(
|
||||
henri = Candidate.objects.create(
|
||||
first_name='Henri', last_name='Dupond', gender='M', section=ede,
|
||||
email='henri@example.org', deposite_date=date.today()
|
||||
)
|
||||
change_url = reverse('admin:candidats_candidate_changelist')
|
||||
self.client.login(username='me', password='mepassword')
|
||||
with mock.patch('candidats.admin.send_mail') as mocked:
|
||||
with mock.patch('django.core.mail.EmailMessage.send') as mocked:
|
||||
mocked.side_effect = Exception("Error sending mail")
|
||||
response = self.client.post(change_url, {
|
||||
'action': 'send_confirmation_mail',
|
||||
'_selected_action': Candidate.objects.values_list('pk', flat=True)
|
||||
}, follow=True)
|
||||
self.assertContains(response, "Échec d’envoi pour le candidat Dupond Henri (Error sending mail)")
|
||||
henri.refresh_from_db()
|
||||
self.assertIsNone(henri.date_confirmation_mail)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue