Skip to content

Commit 34cd9e8

Browse files
05ZYT30gitster
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 a regex to extract status codes as defined in RFC 5321: - For 4yz (temporary errors), return 1 and allow retries. - For 5yz (permanent errors), return 0 as failure. - For unrecognized codes, treat as permanent errors. - If no error occurs, return the authentication result. Signed-off-by: Zheng Yuting <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5b4da71 commit 34cd9e8

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

git-send-email.perl

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,9 +1454,30 @@ 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 $result ? 1 : 0;
1459-
}
1457+
# check if an error was captured
1458+
if ($error) {
1459+
# parse SMTP status code from error message in:
1460+
# https://www.rfc-editor.org/rfc/rfc5321.html
1461+
if ($error =~ /\b(\d{3})\b/) {
1462+
my $status_code = $1;
1463+
if ($status_code =~ /^4/) {
1464+
# 4yz: Transient Negative Completion reply
1465+
warn "SMTP temporary error (status code $status_code): $error";
1466+
return 1;
1467+
} elsif ($status_code =~ /^5/) {
1468+
# 5yz: Permanent Negative Completion reply
1469+
warn "SMTP permanent error (status code $status_code): $error";
1470+
return 0;
1471+
}
1472+
# if no recognized status code is found, treat as permanent error
1473+
warn "SMTP unknown error: $error";
1474+
return 0;
1475+
}
1476+
return $result ? 1 : 0;
1477+
} else {
1478+
return $result ? 1 : 0;
1479+
}
1480+
}
14601481

14611482
sub ssl_verify_params {
14621483
eval {

0 commit comments

Comments
 (0)