summaryrefslogtreecommitdiff
path: root/src/mailman/commands/docs/inject.rst
blob: 3db22013accfdf11350be9071aa432018c81f69f (plain)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
==============================
Command line message injection
==============================

You can inject a message directly into a queue directory via the command
line.

    >>> command = cli('mailman.commands.cli_inject.inject')

It's easy to find out which queues are available.

    >>> command('mailman inject --show')
    Available queues:
        archive
        bad
        bounces
        command
        digest
        in
        nntp
        out
        pipeline
        retry
        shunt
        virgin

Usually, the text of the message to inject is in a file.

    >>> from tempfile import NamedTemporaryFile
    >>> filename = cleanups.enter_context(NamedTemporaryFile()).name
    >>> with open(filename, 'w', encoding='utf-8') as fp:
    ...     print("""\
    ... From: aperson@example.com
    ... To: ant@example.com
    ... Subject: testing
    ... Message-ID: <aardvark>
    ...
    ... This is a test message.
    ... """, file=fp)

Create a mailing list to inject this message into.

    >>> mlist = create_list('ant@example.com')
    >>> transaction.commit()

The mailing list's incoming queue is empty.

    >>> from mailman.testing.helpers import get_queue_messages
    >>> get_queue_messages('in')
    []

By default, messages are injected into the incoming queue.

    >>> command('mailman inject --filename ' + filename + ' ant@example.com')
    >>> items = get_queue_messages('in')
    >>> len(items)
    1
    >>> print(items[0].msg.as_string())
    From: aperson@example.com
    To: ant@example.com
    Subject: testing
    Message-ID: ...
    Date: ...
    <BLANKLINE>
    This is a test message.
    <BLANKLINE>
    <BLANKLINE>

And the message is destined for ant@example.com.

    >>> dump_msgdata(items[0].msgdata)
    _parsemsg    : False
    listid       : ant.example.com
    original_size: 252
    version      : 3

But a different queue can be specified on the command line.
::

    >>> command('mailman inject --queue virgin --filename ' +
    ...         filename + ' ant@example.com')

    >>> get_queue_messages('in')
    []
    >>> items = get_queue_messages('virgin')
    >>> len(items)
    1
    >>> print(items[0].msg.as_string())
    From: aperson@example.com
    To: ant@example.com
    Subject: testing
    Message-ID: ...
    Date: ...
    <BLANKLINE>
    This is a test message.
    <BLANKLINE>
    <BLANKLINE>

    >>> dump_msgdata(items[0].msgdata)
    _parsemsg    : False
    listid       : ant.example.com
    original_size: 252
    version      : 3


Standard input
==============

The message text can also be provided on standard input.
::

    >>> stdin = """\
    ... From: bperson@example.com
    ... To: ant@example.com
    ... Subject: another test
    ... Message-ID: <badger>
    ...
    ... This is another test message.
    ... """

    >>> command('mailman inject --filename - ant@example.com', input=stdin)
    >>> items = get_queue_messages('in')
    >>> len(items)
    1
    >>> print(items[0].msg.as_string())
    From: bperson@example.com
    To: ant@example.com
    Subject: another test
    Message-ID: ...
    Date: ...
    <BLANKLINE>
    This is another test message.
    <BLANKLINE>
    <BLANKLINE>

    >>> dump_msgdata(items[0].msgdata)
    _parsemsg    : False
    listid       : ant.example.com
    original_size: 260
    version      : 3


Metadata
========

Additional metadata keys can be provided on the command line.  These key/value
pairs get added to the message metadata dictionary when the message is
injected.
::

    >>> command('mailman inject --filename ' + filename +
    ...         ' -m foo=one -m bar=two ant@example.com')

    >>> items = get_queue_messages('in')
    >>> dump_msgdata(items[0].msgdata)
    _parsemsg    : False
    bar          : two
    foo          : one
    listid       : ant.example.com
    original_size: 252
    version      : 3