-
Notifications
You must be signed in to change notification settings - Fork 49
/
commands.py
270 lines (204 loc) · 8.47 KB
/
commands.py
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
"""
aiosmtplib.commands
===================
Base SMTP protocol commands.
"""
from typing import Iterable, Union
from aiosmtplib.connection import SMTPConnection
from aiosmtplib.email import parse_address, quote_address
from aiosmtplib.errors import (
SMTPDataError, SMTPHeloError, SMTPRecipientRefused, SMTPResponseException,
SMTPSenderRefused,
)
from aiosmtplib.response import SMTPResponse
from aiosmtplib.status import SMTPStatus
from aiosmtplib.typing import OptionalDefaultNumber, _default
__all__ = ('SMTPCommands',)
class SMTPCommands(SMTPConnection):
"""
Base SMTP command implementation.
"""
def __init__(self, *args, **kwargs):
self.last_helo_response = None # type: SMTPResponse
super().__init__(*args, **kwargs)
async def helo(
self, hostname: str = None,
timeout: OptionalDefaultNumber = _default) -> SMTPResponse:
"""
Send the SMTP 'helo' command.
Hostname to send for this command defaults to the FQDN of the local
host.
Returns an SMTPResponse namedtuple.
"""
if hostname is None:
hostname = self.source_address
if timeout is _default:
timeout = self.timeout # type: ignore
self._raise_error_if_disconnected()
response = await self.protocol.execute_command( # type: ignore
'HELO', hostname, timeout=timeout)
self.last_helo_response = response
if response.code != SMTPStatus.completed:
raise SMTPHeloError(response.code, response.message)
return response
async def help(
self, timeout: OptionalDefaultNumber = _default) -> SMTPResponse:
"""
SMTP 'help' command.
Returns help text.
"""
if timeout is _default:
timeout = self.timeout # type: ignore
self._raise_error_if_disconnected()
response = await self.protocol.execute_command( # type: ignore
'HELP', timeout=timeout)
success_codes = (
SMTPStatus.system_status_ok, SMTPStatus.help_message,
SMTPStatus.completed,
)
if response.code not in success_codes:
raise SMTPResponseException(response.code, response.message)
return response.message
async def rset(
self, timeout: OptionalDefaultNumber = _default) -> SMTPResponse:
"""
Sends an SMTP 'rset' command (resets session)
Returns an SMTPResponse namedtuple.
"""
if timeout is _default:
timeout = self.timeout # type: ignore
self._raise_error_if_disconnected()
response = await self.protocol.execute_command( # type: ignore
'RSET', timeout=timeout)
if response.code != SMTPStatus.completed:
raise SMTPResponseException(response.code, response.message)
return response
async def noop(
self, timeout: OptionalDefaultNumber = _default) -> SMTPResponse:
"""
Sends an SMTP 'noop' command (does nothing)
Returns an SMTPResponse namedtuple.
"""
if timeout is _default:
timeout = self.timeout # type: ignore
self._raise_error_if_disconnected()
response = await self.protocol.execute_command( # type: ignore
'NOOP', timeout=timeout)
if response.code != SMTPStatus.completed:
raise SMTPResponseException(response.code, response.message)
return response
async def vrfy(
self, address: str,
timeout: OptionalDefaultNumber = _default) -> SMTPResponse:
"""
Sends an SMTP 'vrfy' command (tests an address for validity)
Returns an SMTPResponse namedtuple.
"""
if timeout is _default:
timeout = self.timeout # type: ignore
parsed_address = parse_address(address)
self._raise_error_if_disconnected()
response = await self.protocol.execute_command( # type: ignore
'VRFY', parsed_address, timeout=timeout)
success_codes = (
SMTPStatus.completed, SMTPStatus.will_forward,
SMTPStatus.cannot_vrfy,
)
if response.code not in success_codes:
raise SMTPResponseException(response.code, response.message)
return response
async def expn(
self, address: str,
timeout: OptionalDefaultNumber = _default) -> SMTPResponse:
"""
Sends an SMTP 'expn' command (expands a mailing list)
Returns an SMTPResponse namedtuple.
"""
if timeout is _default:
timeout = self.timeout # type: ignore
parsed_address = parse_address(address)
self._raise_error_if_disconnected()
response = await self.protocol.execute_command( # type: ignore
'EXPN', parsed_address, timeout=timeout)
if response.code != SMTPStatus.completed:
raise SMTPResponseException(response.code, response.message)
return response
async def quit(
self, timeout: OptionalDefaultNumber = _default) -> SMTPResponse:
"""
Sends the SMTP 'quit' command, and closes the connection.
Returns an SMTPResponse namedtuple.
"""
if timeout is _default:
timeout = self.timeout # type: ignore
self._raise_error_if_disconnected()
response = await self.protocol.execute_command( # type: ignore
'QUIT', timeout=timeout)
if response.code != SMTPStatus.closing:
raise SMTPResponseException(response.code, response.message)
self.close()
return response
async def mail(
self, sender: str, options: Iterable[str] = None,
timeout: OptionalDefaultNumber = _default) -> SMTPResponse:
"""
Sends the SMTP 'mail' command (begins mail transfer session)
Returns an SMTPResponse namedtuple.
"""
if timeout is _default:
timeout = self.timeout # type: ignore
if options is None:
options = []
from_string = 'FROM:{}'.format(quote_address(sender))
self._raise_error_if_disconnected()
response = await self.protocol.execute_command( # type: ignore
'MAIL', from_string, *options, timeout=timeout)
if response.code != SMTPStatus.completed:
raise SMTPSenderRefused(response.code, response.message, sender)
return response
async def rcpt(
self, recipient: str,
options: Iterable[str] = None,
timeout: OptionalDefaultNumber = _default) -> SMTPResponse:
"""
Sends the SMTP 'rcpt' command (specifies a recipient for the message)
Returns an SMTPResponse namedtuple.
"""
if timeout is _default:
timeout = self.timeout # type: ignore
if options is None:
options = []
to = 'TO:{}'.format(quote_address(recipient))
self._raise_error_if_disconnected()
response = await self.protocol.execute_command( # type: ignore
'RCPT', to, *options, timeout=timeout)
success_codes = (SMTPStatus.completed, SMTPStatus.will_forward)
if response.code not in success_codes:
raise SMTPRecipientRefused(
response.code, response.message, recipient)
return response
async def data(
self, message: Union[str, bytes],
timeout: OptionalDefaultNumber = _default) -> SMTPResponse:
"""
Sends the SMTP 'data' command (sends message data to server)
Raises SMTPDataError if there is an unexpected reply to the
DATA command.
Returns an SMTPResponse tuple (the last one, after all data is sent.)
"""
if timeout is _default:
timeout = self.timeout # type: ignore
self._raise_error_if_disconnected()
start_response = await self.protocol.execute_command( # type: ignore
'DATA', timeout=timeout)
if start_response.code != SMTPStatus.start_input:
raise SMTPDataError(start_response.code, start_response.message)
if isinstance(message, str):
message = message.encode('utf-8')
await self.protocol.write_message_data( # type: ignore
message, timeout=timeout)
response = await self.protocol.read_response( # type: ignore
timeout=timeout)
if response.code != SMTPStatus.completed:
raise SMTPDataError(response.code, response.message)
return response