Unclear on argument passing to "sendmail'

Tim Roberts timr at probo.com
Tue Aug 22 04:24:41 EDT 2006


John Draper <lists at webcrunchers.com> wrote:
>
>In "smtplib" module, the "sendmail" method of function is to be passed a 
>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>?

Tim William's answer is not exactly correct.  The host you specify in the
smtplib.SMTP constructor should NOT be the MX record for any of the
recipients.  You should never have to look up MX records yourself.

Instead, you should specify your ISP's outgoing mail host.  In your
particular case, you should specify "mail.webcrunchers.com".  That server
will do the MX record lookups, and distribute the message to all of the
places it needs to go, using as many SMTP conversations as are required.

Remember that a single call to smtplib.SMTP.sendmail can send to multiple
recipients.  Each of them has to have its own SMTP connection.  That's the
job of mail.webcrunchers.com: it queues your message, and sends it to each
recipient individually.

If your message is being sent to exactly one person, then you CAN look up
the MX host and send it directly, but there are more and more cases where
that won't work.  Many corporate SMTP servers are now rejecting mail that
comes from DSL and cable modem IP addresses, as an anti-spam measure.
-- 
- Tim Roberts, timr at probo.com
  Providenza & Boekelheide, Inc.



More information about the Python-list mailing list