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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
===============
MTA connections
===============
Outgoing connections to the outgoing mail transport agent (MTA) are mitigated
through a ``SMTPConnection`` class, which can transparently manage multiple
sessions in a single connection.
>>> from mailman.mta.connection import SMTPConnection, SMTPSConnection
>>> from lazr.config import as_boolean
The number of sessions per connections is specified when the ``SMTPConnection``
object is created, as is the host and port number of the SMTP server. Zero
means there's an unlimited number of sessions per connection.
>>> connection = SMTPConnection(
... config.mta.smtp_host, int(config.mta.smtp_port), 0)
At the start, there have been no connections to the server.
>>> smtpd.get_connection_count()
0
By sending a message to the server, a connection is opened.
::
>>> connection.sendmail('anne@example.com', ['bart@example.com'], """\
... From: anne@example.com
... To: bart@example.com
... Subject: aardvarks
...
... """)
{}
>>> smtpd.get_connection_count()
1
We can reset the connection count back to zero.
::
>>> from smtplib import SMTP
>>> def reset():
... smtpd = SMTP()
... smtpd.connect(config.mta.smtp_host, int(config.mta.smtp_port))
... smtpd.docmd('RSET')
>>> reset()
>>> smtpd.get_connection_count()
0
>>> connection.quit()
By providing an SMTP user name and password in the configuration file, Mailman
will authenticate with the mail server after each new connection.
::
>>> config.push('auth', """
... [mta]
... smtp_user: testuser
... smtp_pass: testpass
... """)
>>> connection = SMTPConnection(
... config.mta.smtp_host, int(config.mta.smtp_port), 0,
... config.mta.smtp_user, config.mta.smtp_pass)
>>> connection.sendmail('anne@example.com', ['bart@example.com'], """\
... From: anne@example.com
... To: bart@example.com
... Subject: aardvarks
...
... """)
{}
>>> print(smtpd.get_authentication_credentials())
AHRlc3R1c2VyAHRlc3RwYXNz
>>> reset()
>>> config.pop('auth')
SMTPS and STARTTLS support works through ``SMTPSConnection``
and ``STARTTLSConnection`` classes. These provide the same interface
as ``SMTPConnection``.
::
>>> connection = SMTPSConnection(
... config.mta.smtp_host, int(config.mta.smtp_port), 0,
... as_boolean(config.mta.smtp_verify_cert),
... as_boolean(config.mta.smtp_verify_hostname),
... config.mta.smtp_user, config.mta.smtp_pass)
Sessions per connection
=======================
Let's say we specify a maximum number of sessions per connection of 2. When
the third message is sent, the connection is torn down and a new one is
created.
The connection count starts at zero.
::
>>> connection = SMTPConnection(
... config.mta.smtp_host, int(config.mta.smtp_port), 2)
>>> smtpd.get_connection_count()
0
We send two messages through the ``SMTPConnection`` object. Only one connection
is opened.
::
>>> connection.sendmail('anne@example.com', ['bart@example.com'], """\
... From: anne@example.com
... To: bart@example.com
... Subject: aardvarks
...
... """)
{}
>>> smtpd.get_connection_count()
1
>>> connection.sendmail('anne@example.com', ['bart@example.com'], """\
... From: anne@example.com
... To: bart@example.com
... Subject: aardvarks
...
... """)
{}
>>> smtpd.get_connection_count()
1
The third message would cause a third session, exceeding the maximum. So the
current connection is closed and a new one opened.
::
>>> connection.sendmail('anne@example.com', ['bart@example.com'], """\
... From: anne@example.com
... To: bart@example.com
... Subject: aardvarks
...
... """)
{}
>>> smtpd.get_connection_count()
2
A fourth message does not cause a new connection to be made.
::
>>> connection.sendmail('anne@example.com', ['bart@example.com'], """\
... From: anne@example.com
... To: bart@example.com
... Subject: aardvarks
...
... """)
{}
>>> smtpd.get_connection_count()
2
But a fifth one does.
::
>>> connection.sendmail('anne@example.com', ['bart@example.com'], """\
... From: anne@example.com
... To: bart@example.com
... Subject: aardvarks
...
... """)
{}
>>> smtpd.get_connection_count()
3
No maximum
==========
A value of zero means that there is an unlimited number of sessions per
connection.
>>> connection = SMTPConnection(
... config.mta.smtp_host, int(config.mta.smtp_port), 0)
>>> reset()
Even after ten messages are sent, there's still been only one connection to
the server.
::
>>> connection.debug = True
>>> for i in range(10):
... # Ignore the results.
... results = connection.sendmail(
... 'anne@example.com', ['bart@example.com'], """\
... From: anne@example.com
... To: bart@example.com
... Subject: aardvarks
...
... """)
>>> smtpd.get_connection_count()
1
Development mode
================
By putting Mailman into development mode, you can force the recipients to a
given hard-coded address. This allows you to test Mailman without worrying
about accidental deliveries to unintended recipients.
::
>>> config.push('devmode', """
... [devmode]
... enabled: yes
... recipient: zperson@example.com
... """)
>>> smtpd.clear()
>>> connection.sendmail(
... 'anne@example.com',
... ['bart@example.com', 'cate@example.com'], """\
... From: anne@example.com
... To: bart@example.com
... Subject: aardvarks
...
... """)
{}
>>> messages = list(smtpd.messages)
>>> len(messages)
1
>>> print(messages[0].as_string())
From: anne@example.com
To: bart@example.com
Subject: aardvarks
X-Peer: ...
X-MailFrom: anne@example.com
X-RcptTo: zperson@example.com, zperson@example.com
<BLANKLINE>
<BLANKLINE>
>>> config.pop('devmode')
|