Use send_mass_mail and pass contact to template context
This commit is contained in:
parent
571c07bce7
commit
9baede3a6b
|
@ -1,23 +1,26 @@
|
|||
from django.core.mail import send_mail
|
||||
from django.core.mail import send_mass_mail
|
||||
|
||||
class BaseEmailBackend:
|
||||
def __init__(self, params):
|
||||
pass
|
||||
|
||||
def send_email(subject, from_email, contacts, html):
|
||||
for contact in contacts:
|
||||
# TODO: catch SMTPException and deal with it gracefully
|
||||
status = send_mail(
|
||||
subject,
|
||||
html,
|
||||
from_email,
|
||||
[contact],
|
||||
fail_silently=False,
|
||||
html_message=html
|
||||
)
|
||||
def send_email(request, campaign, subject, contacts):
|
||||
from_email = campaign.get_from_email()
|
||||
messages = ()
|
||||
|
||||
#TODO: Check status' and return a an object of status counts/error
|
||||
return True
|
||||
for contact in contacts:
|
||||
html = render_to_string(campaign.get_template(request), {'self': campaign, 'request': request, 'contact': contact})
|
||||
messages.append((subject, html, from_email, [contact]))
|
||||
|
||||
try:
|
||||
send_mass_mail(messages)
|
||||
success = True
|
||||
except SMTPException as e:
|
||||
success = False
|
||||
print('There was an error sending an email: ', e)
|
||||
|
||||
return success
|
||||
|
||||
def unsubscribe_contact(email):
|
||||
#TODO: Make this work! And expose an unsubscribe url!
|
||||
Contact.objects.get(email=email).delete()
|
||||
|
|
|
@ -7,11 +7,9 @@ from wagtail.contrib.modeladmin.helpers.url import AdminURLHelper
|
|||
|
||||
|
||||
def send_helper(request, campaign, subject, contacts):
|
||||
html = render_to_string(campaign.get_template(request), {'self': campaign, 'request': request})
|
||||
mail_backend = campaign.get_backend()
|
||||
from_email = campaign.get_from_email()
|
||||
|
||||
return mail_backend.send_email(subject, from_email, contacts, html)
|
||||
return mail_backend.send_email(request, campaign, subject, contacts)
|
||||
|
||||
|
||||
def redirect_helper(campaign):
|
||||
|
|
Loading…
Reference in New Issue