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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
//! Error types for SMTP operations.
//!
//! Distinguishes permanent (5xx), transient (4xx), I/O, auth, parse, and timeout errors.
//! Reply code classes are defined in RFC 5321 Section 4.2.1.
use crate::types::SmtpResponse;
/// Error type for SMTP client operations.
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// Underlying I/O error (includes TLS transport errors).
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
/// Authentication was rejected by the server (RFC 4954 Section 4).
///
/// The `response` field preserves the full server reply so callers can
/// distinguish transient failures (454) from permanent ones (535).
#[error("authentication failed: {message}")]
Auth {
message: String,
response: SmtpResponse,
},
/// Permanent failure — 5xx response (RFC 5321 Section 4.2.1). Do not retry.
#[error("permanent failure ({code}): {message}")]
Permanent {
code: u16,
message: String,
response: SmtpResponse,
},
/// Transient failure — 4xx response (RFC 5321 Section 4.2.1). May succeed on retry.
#[error("transient failure ({code}): {message}")]
Transient {
code: u16,
message: String,
response: SmtpResponse,
},
/// SMTP protocol violation by the server.
#[error("protocol error: {0}")]
Protocol(String),
/// Failed to parse a server response.
#[error("parse error: {0}")]
Parse(String),
/// Operation exceeded the caller-supplied timeout.
#[error("operation timed out")]
Timeout,
/// The connection has been closed.
#[error("connection closed")]
Closed,
/// STARTTLS was requested but the server does not advertise it (RFC 3207).
#[error("STARTTLS not supported by server")]
StartTlsUnavailable,
/// All recipients were rejected (RFC 5321 Section 3.3 / RFC 1854 Section 3).
#[error("all {count} recipients were rejected")]
AllRecipientsFailed {
count: usize,
responses: Vec<SmtpResponse>,
},
}
impl Error {
/// Returns `true` if the error is transient and the operation may succeed on retry.
///
/// Transient errors include 4xx SMTP responses (RFC 5321 Section 4.2.1),
/// I/O errors, timeouts, and transient auth failures (454, RFC 4954 Section 4).
pub fn is_transient(&self) -> bool {
match self {
Self::Transient { .. } | Self::Io(_) | Self::Timeout => true,
// RFC 4954 Section 4: 454 is a transient auth failure.
Self::Auth { response, .. } => response.is_transient_error(),
_ => false,
}
}
/// Returns `true` if the error is permanent and the operation should not be retried.
///
/// Permanent errors include 5xx SMTP responses (RFC 5321 Section 4.2.1)
/// and permanent authentication failures (535, RFC 4954 Section 4).
pub fn is_permanent(&self) -> bool {
match self {
Self::Permanent { .. } => true,
// RFC 4954 Section 4: 535 is a permanent auth failure.
Self::Auth { response, .. } => response.is_permanent_error(),
_ => false,
}
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::*;
/// Helper to build an `SmtpResponse` with a given code.
fn response(code: u16) -> SmtpResponse {
SmtpResponse {
code,
enhanced_code: None,
lines: vec!["test".into()],
}
}
// ── is_transient ────────────────────────────────────────────────────
#[test]
fn transient_error_is_transient() {
// RFC 5321 Section 4.2.1: 4xx responses are transient failures.
let err = Error::Transient {
code: 421,
message: "try again".into(),
response: response(421),
};
assert!(
err.is_transient(),
"Transient error must return true for is_transient()"
);
}
#[test]
fn io_error_is_transient() {
// I/O errors are transient — network issues may resolve on retry.
let err = Error::Io(std::io::Error::new(
std::io::ErrorKind::ConnectionReset,
"connection reset",
));
assert!(
err.is_transient(),
"Io error must return true for is_transient()"
);
}
#[test]
fn timeout_error_is_transient() {
// Timeouts are transient — the server may respond faster on retry.
let err = Error::Timeout;
assert!(
err.is_transient(),
"Timeout error must return true for is_transient()"
);
}
#[test]
fn permanent_error_is_not_transient() {
// RFC 5321 Section 4.2.1: 5xx responses are permanent failures.
let err = Error::Permanent {
code: 550,
message: "mailbox not found".into(),
response: response(550),
};
assert!(
!err.is_transient(),
"Permanent error must return false for is_transient()"
);
}
#[test]
fn auth_transient_is_transient() {
// RFC 4954 Section 4: 454 is a transient auth failure.
let err = Error::Auth {
message: "temporary auth failure".into(),
response: response(454),
};
assert!(
err.is_transient(),
"Auth error with 4xx response must return true for is_transient() \
(RFC 4954 Section 4)"
);
}
#[test]
fn auth_permanent_is_not_transient() {
// RFC 4954 Section 4: 535 is a permanent auth failure, not transient.
let err = Error::Auth {
message: "bad credentials".into(),
response: response(535),
};
assert!(
!err.is_transient(),
"Auth error with 5xx response must return false for is_transient()"
);
}
#[test]
fn parse_error_is_not_transient() {
let err = Error::Parse("bad response".into());
assert!(
!err.is_transient(),
"Parse error must return false for is_transient()"
);
}
#[test]
fn protocol_error_is_not_transient() {
let err = Error::Protocol("violation".into());
assert!(
!err.is_transient(),
"Protocol error must return false for is_transient()"
);
}
#[test]
fn closed_error_is_not_transient() {
let err = Error::Closed;
assert!(
!err.is_transient(),
"Closed error must return false for is_transient()"
);
}
// ── is_permanent ────────────────────────────────────────────────────
#[test]
fn permanent_error_is_permanent() {
// RFC 5321 Section 4.2.1: 5xx responses are permanent failures.
let err = Error::Permanent {
code: 550,
message: "mailbox not found".into(),
response: response(550),
};
assert!(
err.is_permanent(),
"Permanent error must return true for is_permanent()"
);
}
#[test]
fn transient_error_is_not_permanent() {
// RFC 5321 Section 4.2.1: 4xx responses are transient, not permanent.
let err = Error::Transient {
code: 421,
message: "try again".into(),
response: response(421),
};
assert!(
!err.is_permanent(),
"Transient error must return false for is_permanent()"
);
}
#[test]
fn io_error_is_not_permanent() {
let err = Error::Io(std::io::Error::new(
std::io::ErrorKind::ConnectionRefused,
"refused",
));
assert!(
!err.is_permanent(),
"Io error must return false for is_permanent()"
);
}
#[test]
fn auth_permanent_is_permanent() {
// RFC 4954 Section 4: 535 is a permanent auth failure.
let err = Error::Auth {
message: "bad credentials".into(),
response: response(535),
};
assert!(
err.is_permanent(),
"Auth error with 5xx response must return true for is_permanent() \
(RFC 4954 Section 4)"
);
}
#[test]
fn auth_transient_is_not_permanent() {
// RFC 4954 Section 4: 454 is a transient auth failure, not permanent.
let err = Error::Auth {
message: "temporary failure".into(),
response: response(454),
};
assert!(
!err.is_permanent(),
"Auth error with 4xx response must return false for is_permanent()"
);
}
#[test]
fn timeout_is_not_permanent() {
let err = Error::Timeout;
assert!(
!err.is_permanent(),
"Timeout error must return false for is_permanent()"
);
}
#[test]
fn parse_error_is_not_permanent() {
let err = Error::Parse("bad response".into());
assert!(
!err.is_permanent(),
"Parse error must return false for is_permanent()"
);
}
#[test]
fn starttls_unavailable_is_not_permanent() {
// RFC 3207: STARTTLS unavailability is not inherently permanent
// (server config may change).
let err = Error::StartTlsUnavailable;
assert!(
!err.is_permanent(),
"StartTlsUnavailable must return false for is_permanent()"
);
}
#[test]
fn all_recipients_failed_is_not_permanent() {
// AllRecipientsFailed is an aggregate error, not classified as
// permanent or transient at the top level.
let err = Error::AllRecipientsFailed {
count: 2,
responses: vec![response(550), response(550)],
};
assert!(
!err.is_permanent(),
"AllRecipientsFailed must return false for is_permanent()"
);
}
}