Commit 481204f6 authored by Laury Bueno's avatar Laury Bueno
Browse files

[ProfessorMessage] Send email messages in chunks

parent 57dab870
......@@ -662,18 +662,18 @@ class ProfessorMessage(models.Model):
related_name='message_courses',
blank=True,
)
def send(self):
# bcc = list with all destinataries
# batch = bcc.split( constant )
# for each batch ->
# for each batch ->
# send message
# save status
# if it goes wrong
# split batch by 2 and try it again (binary search)
# check if everything is sent
email_batch_size = settings.PROFESSOR_MESSAGE_CHUNK_SIZE
bcc = [u.email for u in self.users.all() if u.is_active and re.match(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)", u.email)]
try:
et = EmailTemplate.objects.get(name='professor-message')
......@@ -681,9 +681,20 @@ class ProfessorMessage(models.Model):
et = EmailTemplate(name="professor-message", subject="{{subject}}", template="{{message|safe}}")
subject = Template(et.subject).render(Context({'subject': self.subject}))
message = Template(et.template).render(Context({'message': self.message}))
email = EmailMessage(subject, message, settings.DEFAULT_FROM_EMAIL, [settings.DEFAULT_FROM_EMAIL, ], bcc)
email.content_subtype = "html"
return email.send()
# Iterate over the bcc list to send emails in chunks
# Maximum chunk size is email_batch_size
start = 0
end = min(email_batch_size, len(bcc))
while start < len(bcc):
email = EmailMessage(subject, message, settings.DEFAULT_FROM_EMAIL,
[settings.DEFAULT_FROM_EMAIL, ], bcc[start:end])
email.content_subtype = "html"
email.send()
start = end
end = min(end + email_batch_size, len(bcc))
return None
class ProfessorMessageRead(models.Model):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment