1
# Copyright (C) 2001-2012 by the Free Software Foundation, Inc.
3
# This file is part of GNU Mailman.
5
# GNU Mailman is free software: you can redistribute it and/or modify it under
6
# the terms of the GNU General Public License as published by the Free
7
# Software Foundation, either version 3 of the License, or (at your option)
10
# GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
11
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15
# You should have received a copy of the GNU General Public License along with
16
# GNU Mailman. If not, see <http://www.gnu.org/licenses/>.
18
"""Inject a message into a queue."""
20
from __future__ import absolute_import, print_function, unicode_literals
29
from email import message_from_string
30
from email.utils import formatdate, make_msgid
32
from mailman.config import config
33
from mailman.email.message import Message
34
from mailman.utilities.email import add_message_hash
38
def inject_message(mlist, msg, recipients=None, switchboard=None, **kws):
39
"""Inject a message into a queue.
41
If the message does not have a Message-ID header, one is added. An
42
X-Message-Id-Hash header is also always added.
44
:param mlist: The mailing list this message is destined for.
45
:type mlist: IMailingList
46
:param msg: The Message object to inject.
47
:type msg: a Message object
48
:param recipients: Optional set of recipients to put into the message's
50
:type recipients: sequence of strings
51
:param switchboard: Optional name of switchboard to inject this message
52
into. If not given, the 'in' switchboard is used.
53
:type switchboard: string
54
:param kws: Additional values for the message metadata.
57
if switchboard is None:
59
# Since we're crafting the message from whole cloth, let's make sure this
60
# message has a Message-ID.
61
if 'message-id' not in msg:
62
msg['Message-ID'] = make_msgid()
64
# Ditto for Date: as required by RFC 2822.
66
msg['Date'] = formatdate(localtime=True)
67
msg.original_size = len(msg.as_string())
69
listname=mlist.fqdn_listname,
70
original_size=msg.original_size,
73
if recipients is not None:
74
msgdata['recipients'] = recipients
75
config.switchboards[switchboard].enqueue(msg, **msgdata)
79
def inject_text(mlist, text, recipients=None, switchboard=None, **kws):
80
"""Turn text into a message and inject that into a queue.
82
If the text does not have a Message-ID header, one is added. An
83
X-Message-Id-Hash header is also always added.
85
:param mlist: The mailing list this message is destined for.
86
:type mlist: IMailingList
87
:param text: The text of the message to inject. This will be parsed into
89
:type text: byte string
90
:param recipients: Optional set of recipients to put into the message's
92
:type recipients: sequence of strings
93
:param switchboard: Optional name of switchboard to inject this message
94
into. If not given, the 'in' switchboard is used.
95
:type switchboard: string
96
:param kws: Additional values for the message metadata.
99
message = message_from_string(text, Message)
100
inject_message(mlist, message, recipients, switchboard, **kws)