Skip to content

Commit 1ac402c

Browse files
Zheng Yutinggitster
authored andcommitted
send-email: finer-grained SMTP error handling
Code captured errors but did not process them further. This treated all failures the same without distinguishing SMTP status. Add handle-smtp_error to extract SMTP status codes using a regex (as defined in RFC 5321) and handle errors as follows: - No error present: - If a result is provided, return 1 to indicate success. - Otherwise, return 0 to indicate failure. - Error present with a captured three-digit status code: - For 4yz (transient errors), return 1 and allow retries. - For 5yz (permanent errors), return 0 to indicate failure. - For any other recognized status code, return 1, treating it as a transient error. - Error present but no status code found: - Return 1 as a transient error. Signed-off-by: Zheng Yuting <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ce20dec commit 1ac402c

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

git-send-email.perl

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,14 +1454,40 @@ sub smtp_auth_maybe {
14541454
$error = $@ || 'Unknown error';
14551455
};
14561456

1457-
# NOTE: SMTP status code handling will be added in a subsequent commit,
1458-
# return 1 when failed due to non-credential reasons
1459-
return $error ? 1 : ($result ? 1 : 0);
1457+
return ($error
1458+
? handle_smtp_error($error)
1459+
: ($result ? 1 : 0));
14601460
});
14611461

14621462
return $auth;
14631463
}
14641464

1465+
sub handle_smtp_error {
1466+
my ($error) = @_;
1467+
1468+
# Parse SMTP status code from error message in:
1469+
# https://www.rfc-editor.org/rfc/rfc5321.html
1470+
if ($error =~ /\b(\d{3})\b/) {
1471+
my $status_code = $1;
1472+
if ($status_code =~ /^4/) {
1473+
# 4yz: Transient Negative Completion reply
1474+
warn "SMTP transient error (status code $status_code): $error";
1475+
return 1;
1476+
} elsif ($status_code =~ /^5/) {
1477+
# 5yz: Permanent Negative Completion reply
1478+
warn "SMTP permanent error (status code $status_code): $error";
1479+
return 0;
1480+
}
1481+
# If no recognized status code is found, treat as transient error
1482+
warn "SMTP unknown error: $error. Treating as transient failure.";
1483+
return 1;
1484+
}
1485+
1486+
# If no status code is found, treat as transient error
1487+
warn "SMTP generic error: $error";
1488+
return 1;
1489+
}
1490+
14651491
sub ssl_verify_params {
14661492
eval {
14671493
require IO::Socket::SSL;

0 commit comments

Comments
 (0)