Odesilani newsletteru po castech kvuli limitu API.
This commit is contained in:
parent
e0a735391b
commit
b0945a10d9
|
@ -391,47 +391,58 @@ def msg_send(request, id, realSend=False):
|
||||||
}
|
}
|
||||||
|
|
||||||
if realSend:
|
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
|
from django.core.mail import get_connection, EmailMultiAlternatives
|
||||||
import quopri
|
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>' % (
|
mailgun_backend = get_connection('anymail.backends.mailgun.EmailBackend')
|
||||||
appSettings.ANYMAIL['FROM_NAME'],
|
|
||||||
appSettings.ANYMAIL['FROM_EMAIL'],
|
|
||||||
)
|
|
||||||
html_message = render_to_string(template, context)
|
|
||||||
plain_message = html2text.html2text(html_message)
|
|
||||||
|
|
||||||
ema = EmailMultiAlternatives(
|
header_from = '%s <%s>' % (
|
||||||
"%s - %s" % (obj.news.name, obj.title),
|
appSettings.ANYMAIL['FROM_NAME'],
|
||||||
plain_message, header_from )
|
appSettings.ANYMAIL['FROM_EMAIL'],
|
||||||
ema.connection = mailgun_backend
|
)
|
||||||
|
html_message = render_to_string(template, context)
|
||||||
|
plain_message = html2text.html2text(html_message)
|
||||||
|
|
||||||
if use_public_to_email:
|
ema = EmailMultiAlternatives(
|
||||||
ema.to = [ appSettings.ANYMAIL['PUBLIC_TO_EMAIL'] ]
|
"%s - %s" % (obj.news.name, obj.title),
|
||||||
else:
|
plain_message, header_from )
|
||||||
# pouzit kontaktni nebo registracni email uzivatele
|
ema.connection = mailgun_backend
|
||||||
eml = rcp.email_contact_active
|
|
||||||
if eml is None or len(eml.strip()) == 0:
|
|
||||||
eml = rcp.email
|
|
||||||
ema.to = [ eml ]
|
|
||||||
|
|
||||||
ema.bcc = recipients_email
|
if use_public_to_email:
|
||||||
if obj.news.replyToEmail is not None:
|
ema.to = [ appSettings.ANYMAIL['PUBLIC_TO_EMAIL'] ]
|
||||||
try:
|
else:
|
||||||
eml = obj.news.replyToEmail.strip()
|
# pouzit kontaktni nebo registracni email uzivatele
|
||||||
validate_email(eml)
|
eml = rcp.email_contact_active
|
||||||
ema.extra_headers = {
|
if eml is None or len(eml.strip()) == 0:
|
||||||
'Reply-To' : eml,
|
eml = rcp.email
|
||||||
}
|
ema.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.bcc = recip_part
|
||||||
ema.send()
|
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:
|
if update_sent_ts:
|
||||||
obj.sent_ts = datetime.now()
|
obj.sent_ts = datetime.now()
|
||||||
|
|
Loading…
Reference in New Issue