Return different SMTP status code depending on HTTP status received
1 files changed, 23 insertions(+), 3 deletions(-)

M fakemail.py
M fakemail.py +23 -3
@@ 125,9 125,26 @@ class HTTPRelay:
     - mail_to: the envelope recipient address
     - message: the full email mime encoded body
 
-    A 2xx response from the server will be returned as a '250 OK' status.
-    Anything else will result in a '421 Service Unavailable' status.
+    A 2xx response from the upstream server causes a "250 OK" response. For
+    other HTTP codes, refer to the ``error_status_code_map`` dict.
     """
+
+    error_status_code_map = {
+        400: "541 The recipient address rejected your message",
+        401: "530 Authentication problem",
+        402: "530 Authentication problem",
+        404: "550 Non-existent email address",
+        410: "550 Non-existent email address",
+        413: "523 Size of your mail exceeds the server limits",
+        429: "541 Destination system misconfigured (received HTTP XXX)",
+        500: "421 Service unavailable",
+        502: "421 Service unavailable",
+        503: "421 Service unavailable",
+        504: "420 Timeout connection problem",
+    }
+
+    default_error_response = "441 Recipient's server not responding"
+
     def __init__(self, url):
         self.url = url
 

          
@@ 139,7 156,10 @@ class HTTPRelay:
                 await response.read()
                 if 200 <= response.status < 299:
                     return '250 OK'
-                return '421 Service Unavailable'
+                return self.error_status_code_map.get(
+                    response.status,
+                    self.default_error_response
+                )
 
 
 class StdoutRelay: