On 21/08/06, John Draper <[EMAIL PROTECTED]> wrote: > In "smtplib" module, the "sendmail" method of function is to be passed > host, but it is the Domain name > for the SMTP Server as gotten from the "dig" command? IE: dig -tMX > would give me the SMTP > server. In my code I have: > > try: > print "Sending message to host: %s" % mailHost > server=smtplib.SMTP(mailHost) > server.sendmail(reply_email,email,body) > server.quit() > except: > print "Uunable to send" > > Is mailHost like "mail.t-mobile.com" which I get from the MX record for > a given domain? > But also, is the "email" just the mail account, ie: the username? > without the @<domain>? > > I need to be able to query the mail server? Also, I want to be able > to handle > the "SMTPRecipientsRefused" exception. What is the proper syntax for > handling > this? > > Do I do it like this? > > try: > print "Sending message to host: %s" % mailHost > server=smtplib.SMTP(mailHost) > server.sendmail(reply_email,email,body) > server.quit() > except SMTPRecipientsRefused: > print "Recipient refused" > > Is that the right syntax? I have severe problems with not enough > example code. >
mailHost is the name of the mail server server you are sending the email to/via, for internet mail this will be a server from the recipient's domain's mx records (or your ISP server). reply_email is the full email address of the sender, or the email address you wish to appear as the sender. It does not have to be the same address as used in the body headers email is a bad choice for a variable name, try something like to_email, in your case it should contain the full email address of the recipeint or a list of recipients. The address(es) do not have to be the same address as used in the body headers server.sendmail returns a list of failed recipient email addresses if only some failed, if all the recipients failed you get an exception. Apologies for the bad formatting below, its untested but should show you the structure for managing an SMTP msg send. You could tidy it up without too much effort import sys for mailHost in MX_records: try: print "Sending message to host: %s" % mailHost server=smtplib.SMTP(mailHost) failed = server.sendmail(reply_email,email,body) server.quit() break except smtplib.SMTPRecipientsRefused, x : #all recips failed for recip in x.recipients: print recip server.quit() break except smtplib.SMTPDataError, x: # an error at the end of the # message body = MSG Failed # all recips failed print x[0], x[1] server.quit() break except smtplib.SMTPSenderRefused, x : # the sender was refused = #MSG failed # all recips failed print x[0], x[1] server.quit() break except: #can't connect so continue to next MX server - don't fail !!! e_error = str(sys.exc_info()[0]) print e_error server.quit() continue for recip in failed: # some failed, some didn't print recip -- Tim Williams -- http://mail.python.org/mailman/listinfo/python-list