forked from adamlaska/datatracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend_messages.py
More file actions
85 lines (73 loc) · 3.47 KB
/
send_messages.py
File metadata and controls
85 lines (73 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# Copyright The IETF Trust 2020, All Rights Reserved
# -*- coding: utf-8 -*-
import email
import smtplib
from django.core.management.base import BaseCommand
import debug # pyflakes:ignore
from ietf.message.models import Message
from ietf.utils.mail import send_mail_message
class Command(BaseCommand):
help = """
Send (or re-send) messages saved as Message objects as outgoing emails. To
show existing Message objects, use the show_messages management command.
Messages to send can be indicateb by date ranges, a list of primary keys, or
a list of Message-IDs. Unless the --resend switch is given, the inclusion
of already sent messages in the date range or message lists will result in
an error exit, in order to prevent inadvertent re-sending of message.
Alternatively, the --unsent switch can be used to send only messages marked
as not already sent from a date range or message list.
"""
def add_arguments(self, parser):
parser.add_argument(
'--pks', dest='primary_keys',
help="Send the messages with the given primary keys. Accepts a comma-separated list of keys.",
)
parser.add_argument(
'--resend', action="store_true", default=False,
help="Re-send messages (ignoring that they are marked as already sent)."
)
parser.add_argument(
'-t', '--start', '--from', type=str, default=None,
help='Limit the list to messages saved after the given time (default %(default)s).',
)
parser.add_argument(
'--stop', '--to', type=str, default=None,
help='Limit the list to messages saved after the given time.',
)
parser.add_argument(
'--unsent', action="store_true", default=False,
help="Send only the unsent messages from the PKs or date range given",
)
def handle(self, *args, **options):
start = options['start']
stop = options['stop']
pks = options['primary_keys']
resend= options['resend']
unsent= options['unsent']
if pks:
primary_keys = [pk.strip() for pk in pks.split(',')]
else:
primary_keys = []
messages = Message.objects.all()
if primary_keys:
messages = messages.filter(pk__in=primary_keys)
if start:
messages = messages.filter(time__gte=start)
if stop:
messages = messages.filter(sent__lte=stop)
sent = messages.filter(sent__isnull=False)
if sent.exists() and not resend and not unsent:
self.stderr.write("Error: Asked to send one or more already sent messages, and --resend not given")
for m in sent:
to = ','.join( a[1] for a in email.utils.getaddresses([m.to]) )
self.stderr.write(' sent %s: %s %s -> %s "%s"' % (m.sent.strftime('%Y-%m-%d %H:%M'), m.pk, m.frm, to, m.subject.strip()))
else:
if unsent:
messages = messages.filter(sent__isnull=True)
for m in messages:
to = ','.join( a[1] for a in email.utils.getaddresses([m.to]) )
try:
send_mail_message(None, m)
self.stdout.write('%s %s -> %s "%s"' % (m.pk, m.frm, to, m.subject.strip()))
except smtplib.SMTPException as e:
self.stdout.write('Failure %s: %s %s -> %s "%s"' % (e, m.pk, m.frm, to, m.subject.strip()))