Return consistent tuple on error.
This commit is contained in:
+8
-5
@@ -95,7 +95,10 @@ Received-SPF: %(spf_result)s
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def send_dsn(mailfrom,receiver,msg=None):
|
def send_dsn(mailfrom,receiver,msg=None):
|
||||||
"Send DSN. If msg is None, do callback verification."
|
"""Send DSN. If msg is None, do callback verification.
|
||||||
|
Mailfrom is original sender we are sending DSN or CBV to.
|
||||||
|
Receiver is the MTA sending the DSN.
|
||||||
|
Return None for success or (code,msg) for failure."""
|
||||||
user,domain = mailfrom.split('@')
|
user,domain = mailfrom.split('@')
|
||||||
q = spf.query(None,None,None)
|
q = spf.query(None,None,None)
|
||||||
mxlist = q.dns(domain,'MX')
|
mxlist = q.dns(domain,'MX')
|
||||||
@@ -112,7 +115,7 @@ def send_dsn(mailfrom,receiver,msg=None):
|
|||||||
if resp.split()[0] == receiver:
|
if resp.split()[0] == receiver:
|
||||||
return (553,'Fraudulent MX for %s' % domain)
|
return (553,'Fraudulent MX for %s' % domain)
|
||||||
if not (200 <= code <= 299):
|
if not (200 <= code <= 299):
|
||||||
raise SMTPHeloError(code, resp)
|
raise smtplib.SMTPHeloError(code, resp)
|
||||||
if msg:
|
if msg:
|
||||||
try:
|
try:
|
||||||
smtp.sendmail('<>',mailfrom,msg)
|
smtp.sendmail('<>',mailfrom,msg)
|
||||||
@@ -122,7 +125,7 @@ def send_dsn(mailfrom,receiver,msg=None):
|
|||||||
else: # CBV
|
else: # CBV
|
||||||
code,resp = smtp.docmd('MAIL FROM: <>')
|
code,resp = smtp.docmd('MAIL FROM: <>')
|
||||||
if code != 250:
|
if code != 250:
|
||||||
raise SMTPSenderRefused(code, resp, '<>')
|
raise smtplib.SMTPSenderRefused(code, resp, '<>')
|
||||||
code,resp = smtp.rcpt(mailfrom)
|
code,resp = smtp.rcpt(mailfrom)
|
||||||
if code not in (250,251):
|
if code not in (250,251):
|
||||||
return (code,resp) # permanent error
|
return (code,resp) # permanent error
|
||||||
@@ -131,9 +134,9 @@ def send_dsn(mailfrom,receiver,msg=None):
|
|||||||
except smtplib.SMTPRecipientsRefused,x:
|
except smtplib.SMTPRecipientsRefused,x:
|
||||||
return x.recipients[mailfrom] # permanent error
|
return x.recipients[mailfrom] # permanent error
|
||||||
except smtplib.SMTPSenderRefused,x:
|
except smtplib.SMTPSenderRefused,x:
|
||||||
return x # does not accept DSN
|
return x.args[:2] # does not accept DSN
|
||||||
except smtplib.SMTPDataError,x:
|
except smtplib.SMTPDataError,x:
|
||||||
return x # permanent error
|
return x.args # permanent error
|
||||||
except smtplib.SMTPException:
|
except smtplib.SMTPException:
|
||||||
pass # any other error, try next MX
|
pass # any other error, try next MX
|
||||||
except socket.error:
|
except socket.error:
|
||||||
|
|||||||
Reference in New Issue
Block a user