2015-05-04 19:52:53 +00:00
|
|
|
import smtplib
|
|
|
|
from socket import error as SocketError
|
|
|
|
|
|
|
|
from django.core.mail import get_connection
|
|
|
|
from django.utils.encoding import smart_str
|
|
|
|
|
|
|
|
from .models import Message
|
|
|
|
|
|
|
|
|
2015-05-05 19:42:55 +00:00
|
|
|
def send_message(message, num=0, connection=None, bulk=100):
|
2015-05-04 19:52:53 +00:00
|
|
|
if num >= bulk:
|
|
|
|
connection.close()
|
|
|
|
connection = None
|
|
|
|
if connection is None:
|
2015-05-04 20:26:01 +00:00
|
|
|
# Reset connection with django
|
2015-05-04 19:52:53 +00:00
|
|
|
connection = get_connection(backend='django.core.mail.backends.smtp.EmailBackend')
|
|
|
|
connection.open()
|
|
|
|
error = None
|
|
|
|
try:
|
|
|
|
connection.connection.sendmail(message.from_address, [message.to_address], smart_str(message.content))
|
|
|
|
except (SocketError, smtplib.SMTPSenderRefused,
|
|
|
|
smtplib.SMTPRecipientsRefused,
|
|
|
|
smtplib.SMTPAuthenticationError) as err:
|
|
|
|
message.defer()
|
|
|
|
error = err
|
|
|
|
else:
|
|
|
|
message.sent()
|
|
|
|
message.log(error)
|
|
|
|
|
|
|
|
|
|
|
|
def send_pending(bulk=100):
|
|
|
|
# TODO aquire lock
|
|
|
|
connection = None
|
|
|
|
num = 0
|
|
|
|
for message in Message.objects.filter(state=Message.QUEUED).order_by('priority'):
|
|
|
|
send_message(message, num, connection, bulk)
|
2015-05-05 19:42:55 +00:00
|
|
|
num += 1
|
2015-05-04 19:52:53 +00:00
|
|
|
from django.utils import timezone
|
|
|
|
from . import settings
|
|
|
|
from datetime import timedelta
|
|
|
|
from django.db.models import Q
|
|
|
|
|
|
|
|
now = timezone.now()
|
|
|
|
qs = Q()
|
|
|
|
for retries, seconds in enumerate(settings.MAILER_DEFERE_SECONDS):
|
|
|
|
delta = timedelta(seconds=seconds)
|
|
|
|
qs = qs | Q(retries=retries, last_retry__lte=now-delta)
|
|
|
|
for message in Message.objects.filter(state=Message.DEFERRED).filter(qs).order_by('priority'):
|
|
|
|
send_message(message, num, connection, bulk)
|
|
|
|
if connection is not None:
|
|
|
|
connection.close()
|