SMTP help please

Tim Williams listserver at tdw.net
Tue Jun 7 04:55:46 EDT 2005


----- Original Message ----- 
From: "Martin Franklin" <mfranklin1 at gatwick.westerngeco.slb.com>

> server.sendmail(fromaddr, toaddrs, msg)

You can use this if you don't need to worry about whether the email was
successfully sent to all recipients.   Otherwise,  you need something like
this

(untested and partly copied from an earlier post of mine).
------------------------------------------------
import smtplib
try:
   s = smtplib.SMTP(outgoing_server)
   failed = s.sendmail(fromaddr, toaddrs, msg)
except smtplib.SMTPRecipientsRefused, x :  # All failed
   do something  # for recip in x.recipients:  info = x.recipients[recip]
except smtplib.SMTPDataError, x:  # email failed after data stage
   do something  # x[0], x[1]
except smtplib.SMTPSenderRefused, x :  # the sender was refused
   do something  # x.sender, x.smtp_code, x.smtp_error
except:
   do something

try:
    s.close()
except
    pass

for recip in failed:
    do something

--------------------------------------------------

(failed contains a list of failed recipients if *some* but not all failed,
if all failed then use :  except smtplib.SMTPRecipientsRefused, x :)





More information about the Python-list mailing list