Subject: [GSoC PATCH v4 2/2] Error handling for SMTP status codes

From: Zheng Yuting <[email-masked]>
Date: Sun, 16 Mar 2025 13:09:20 +0800

This change further parses and processes the captured exception information
based on the previous patch's unified error capture. Specifically, a
three-digit status code is extracted from the error information through a
regular expression, and judged according to the definition of RFC 5321:

- If the status code starts with "4" (temporary error), only a warning is
printed and success (1) is returned to allow subsequent retries;

- If the status code starts with "5" (permanent error), a warning is
printed and failure (0) is returned;

- If the status code is not recognized in the error, it is considered a
permanent error, and an unknown error message is printed and failure is
returned.

In the absence of an error, the authentication result is still returned
according to the original logic. This change makes SMTP authentication
error handling more refined,

Signed-off-by: Zheng Yuting <[email-masked]>
---
 git-send-email.perl | 27 ++++++++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)

diff --git a/git-send-email.perl b/git-send-email.perl
index 8feb43e9f7..69ba328653 100755
--- a/git-send-email.perl
+++ b/git-send-email.perl
@@ -1454,9 +1454,30 @@ sub smtp_auth_maybe {
 			$error = $@ || 'Unknown error';
 		};

-		# NOTE: SMTP status code handling will be added in a subsequent commit
-		return $result ? 1 : 0;
-	}
+		# check if an error was captured
+		if ($error) {
+			# parse SMTP status code from error message in:
+			# https://www.rfc-editor.org/rfc/rfc5321.html
+			if ($error =~ /\b(\d{3})\b/) {
+				my $status_code = $1;
+				if ($status_code =~ /^4/) {
+					# 4yz: Transient Negative Completion reply
+					warn "SMTP temporary error (status code $status_code): $error";
+					return 1;
+				} elsif ($status_code =~ /^5/) {
+					# 5yz: Permanent Negative Completion reply
+					warn "SMTP permanent error (status code $status_code): $error";
+					return 0;
+				}
+				# if no recognized status code is found, treat as permanent error
+				warn "SMTP unknown error: $error";
+				return 0;
+			}
+			return $result ? 1 : 0;
+		} else {
+			return $result ? 1 : 0;
+		}
+}

 sub ssl_verify_params {
 	eval {
--
2.48.1