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:
|
class BaseEmailBackend:
|
||||||
def __init__(self, params):
|
def __init__(self, params):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def send_email(subject, from_email, contacts, html):
|
def send_email(request, campaign, subject, contacts):
|
||||||
for contact in contacts:
|
from_email = campaign.get_from_email()
|
||||||
# TODO: catch SMTPException and deal with it gracefully
|
messages = ()
|
||||||
status = send_mail(
|
|
||||||
subject,
|
|
||||||
html,
|
|
||||||
from_email,
|
|
||||||
[contact],
|
|
||||||
fail_silently=False,
|
|
||||||
html_message=html
|
|
||||||
)
|
|
||||||
|
|
||||||
#TODO: Check status' and return a an object of status counts/error
|
for contact in contacts:
|
||||||
return True
|
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):
|
def unsubscribe_contact(email):
|
||||||
|
#TODO: Make this work! And expose an unsubscribe url!
|
||||||
Contact.objects.get(email=email).delete()
|
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):
|
def send_helper(request, campaign, subject, contacts):
|
||||||
html = render_to_string(campaign.get_template(request), {'self': campaign, 'request': request})
|
|
||||||
mail_backend = campaign.get_backend()
|
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):
|
def redirect_helper(campaign):
|
||||||
|
|
Loading…
Reference in New Issue