Send bulk emails to Django with the same attachment

I want to send emails to members of my site who should be present at the meeting (i.e. guests), each with the (same) PDF attachment. I do this using Django's built-in email features connection.send_messages(messages). At the moment I am doing this:

guests = Guest.objects.all()
connection = mail.get_connection()
connection.open()
messages = []
for guest in guests:
    msg = EmailMultiAlternatives(title, text_content, from_address, [guest.email], connection=connection)
    msg.attach_alternative(html_content, 'text/html')
    pdf_data = open(os.path.join(settings.MEDIA_ROOT, 'uploads/flyer.pdf'))
    msg.attach('Invitation Card.pdf', pdf_data.read(), 'application/pdf')
    pdf_data.close()
    messages.append(msg)
connection.send_messages(messages)
connection.close()

Now when I do this, the same PDF file will be downloaded for each letter attached separately, and then sent separately for each letter, as if it were different PDF files. If the file is 10 MB, then 10 MB will be uploaded to my mail server for each guest, where he could be only once.

, :: , ? ?

:

:

msg.attach_file(os.path.join(settings.MEDIA_ROOT, 'uploads/flyer.pdf'))

?

+3
1

django/core/mail/message.py attach_file - , attach:

def attach_file(self, path, mimetype=None):
    """Attaches a file from the filesystem."""
    filename = os.path.basename(path)
    content = open(path, 'rb').read()
    self.attach(filename, content, mimetype)

, EmailMultiAlternatives attach. / , celery.

0

All Articles