2017-04-23 20:04:58 +00:00
|
|
|
import logging
|
2019-06-16 12:32:24 +00:00
|
|
|
|
2020-02-12 12:10:41 +00:00
|
|
|
from .email import _send_email
|
|
|
|
from .models import OutgoingEmail
|
|
|
|
|
2017-04-23 20:04:58 +00:00
|
|
|
logging.basicConfig(level=logging.INFO)
|
2019-06-16 12:32:24 +00:00
|
|
|
logger = logging.getLogger("bornhack.%s" % __name__)
|
2017-04-23 20:04:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
def do_work():
|
|
|
|
"""
|
|
|
|
The outgoing email worker sends emails added to the OutgoingEmail
|
|
|
|
queue.
|
|
|
|
"""
|
|
|
|
not_processed_email = OutgoingEmail.objects.filter(processed=False)
|
|
|
|
|
2017-05-21 14:48:06 +00:00
|
|
|
if len(not_processed_email) > 0:
|
2019-06-16 12:32:24 +00:00
|
|
|
logger.debug("about to process {} emails".format(len(not_processed_email)))
|
2017-05-21 14:48:06 +00:00
|
|
|
|
2017-04-23 20:04:58 +00:00
|
|
|
for email in not_processed_email:
|
|
|
|
|
2017-04-30 09:32:49 +00:00
|
|
|
attachment = None
|
2019-06-16 12:32:24 +00:00
|
|
|
attachment_filename = ""
|
2017-04-30 09:32:49 +00:00
|
|
|
if email.attachment:
|
|
|
|
attachment = email.attachment.read()
|
|
|
|
attachment_filename = email.attachment.name
|
|
|
|
|
|
|
|
mail_send_success = _send_email(
|
2017-04-23 20:04:58 +00:00
|
|
|
text_template=email.text_template,
|
2017-05-21 18:13:49 +00:00
|
|
|
to_recipients=email.to_recipients,
|
2017-04-23 20:04:58 +00:00
|
|
|
subject=email.subject,
|
2017-05-21 18:13:49 +00:00
|
|
|
cc_recipients=email.cc_recipients,
|
|
|
|
bcc_recipients=email.bcc_recipients,
|
2017-04-23 20:04:58 +00:00
|
|
|
html_template=email.html_template,
|
2017-04-30 09:32:49 +00:00
|
|
|
attachment=attachment,
|
2019-06-16 12:32:24 +00:00
|
|
|
attachment_filename=attachment_filename,
|
2017-04-23 20:04:58 +00:00
|
|
|
)
|
2017-04-30 09:32:49 +00:00
|
|
|
if mail_send_success:
|
|
|
|
email.processed = True
|
|
|
|
email.save()
|
2019-06-16 12:32:24 +00:00
|
|
|
logger.debug("successfully sent {}".format(email))
|
2017-04-30 09:32:49 +00:00
|
|
|
else:
|
2019-06-16 12:32:24 +00:00
|
|
|
logger.error("unable to sent {}".format(email))
|