def mail_newsletter(recipients, mail):
email_context = {
'title': mail.subject,
'message': mail.text,
'base_url': settings.EMAIL_ABS_URL,
'sponsors': Sponsor.objects.all(),
}
email_html = render_to_string('newsletter/email_newsletter.html', email_context)
email_plaintext = render_to_string('newsletter/email_newsletter.txt', email_context)
to = [x.email for x in recipients]
# Create a map of emails to unsub tokens for the email merge
unsub_tokens = {recipient.email: {
'unsub_url': '{hostname}{path}'.format(hostname=settings.EMAIL_ABS_URL,
path=reverse('unsub_with_id', kwargs={
'token': recipient.unsubscribe_token
}))} for recipient in recipients}
sender = '{name} <{email}>'.format(name=mail.sender_name, email=mail.sender_email)
email = EmailMultiAlternatives(mail.subject, email_plaintext, sender, to)
email.attach_alternative(email_html, 'text/html')
email.merge_data = unsub_tokens
email.send()
# Create a function called "chunks" with two arguments, l and n:
评论列表
文章目录