From b0945a10d9fb3216fe213a8f0ce9d37c0104fea1 Mon Sep 17 00:00:00 2001 From: Martin Rejman Date: Fri, 24 May 2019 00:21:54 +0200 Subject: [PATCH] Odesilani newsletteru po castech kvuli limitu API. --- src/nalodeni/news.py | 77 +++++++++++++++++++++++++------------------- 1 file changed, 44 insertions(+), 33 deletions(-) diff --git a/src/nalodeni/news.py b/src/nalodeni/news.py index 7c2439c..4aa9120 100644 --- a/src/nalodeni/news.py +++ b/src/nalodeni/news.py @@ -391,47 +391,58 @@ def msg_send(request, id, realSend=False): } if realSend: + def chunks(l, n): + """Yield successive n-sized chunks from l.""" + for i in range(0, len(l), n): + yield l[i:i + n] + from django.core.mail import get_connection, EmailMultiAlternatives import quopri - mailgun_backend = get_connection('anymail.backends.mailgun.EmailBackend') + for recip_part in chunks(recipients_email, 500): + # process by part, MailGun API hsa a limit of 1000 messages - header_from = '%s <%s>' % ( - appSettings.ANYMAIL['FROM_NAME'], - appSettings.ANYMAIL['FROM_EMAIL'], - ) - html_message = render_to_string(template, context) - plain_message = html2text.html2text(html_message) + mailgun_backend = get_connection('anymail.backends.mailgun.EmailBackend') - ema = EmailMultiAlternatives( - "%s - %s" % (obj.news.name, obj.title), - plain_message, header_from ) - ema.connection = mailgun_backend + header_from = '%s <%s>' % ( + appSettings.ANYMAIL['FROM_NAME'], + appSettings.ANYMAIL['FROM_EMAIL'], + ) + html_message = render_to_string(template, context) + plain_message = html2text.html2text(html_message) - if use_public_to_email: - ema.to = [ appSettings.ANYMAIL['PUBLIC_TO_EMAIL'] ] - else: - # pouzit kontaktni nebo registracni email uzivatele - eml = rcp.email_contact_active - if eml is None or len(eml.strip()) == 0: - eml = rcp.email - ema.to = [ eml ] + ema = EmailMultiAlternatives( + "%s - %s" % (obj.news.name, obj.title), + plain_message, header_from ) + ema.connection = mailgun_backend - ema.bcc = recipients_email - if obj.news.replyToEmail is not None: - try: - eml = obj.news.replyToEmail.strip() - validate_email(eml) - ema.extra_headers = { - 'Reply-To' : eml, - } - except ValidationError: - messages.error(request, "Odpovědní email '%s' není ve tvaru emailu. Odesílání zrušeno." % (eml,) ) - messages.info(request, "Chybu může opravit správce tohoto newsletteru." ) - return redirect('nalodeni:news_list_show', lid=obj.news_id) + if use_public_to_email: + ema.to = [ appSettings.ANYMAIL['PUBLIC_TO_EMAIL'] ] + else: + # pouzit kontaktni nebo registracni email uzivatele + eml = rcp.email_contact_active + if eml is None or len(eml.strip()) == 0: + eml = rcp.email + ema.to = [ eml ] - ema.attach_alternative(html_message, "text/html") - ema.send() + ema.bcc = recip_part + if obj.news.replyToEmail is not None: + try: + eml = obj.news.replyToEmail.strip() + validate_email(eml) + ema.extra_headers = { + 'Reply-To' : eml, + } + except ValidationError: + messages.error(request, "Odpovědní email '%s' není ve tvaru emailu. Odesílání zrušeno." % (eml,) ) + messages.info(request, "Chybu může opravit správce tohoto newsletteru." ) + return redirect('nalodeni:news_list_show', lid=obj.news_id) + + ema.attach_alternative(html_message, "text/html") + ema.send() + + messages.info(request, "Zpráva odeslána %s uživatelům." % len(recip_part) ) + # end of partial send if update_sent_ts: obj.sent_ts = datetime.now()