Unclear on argument passing to "sendmail'

Discussion in 'Python' started by John Draper, Aug 21, 2006.

  1. John Draper

    John Draper Guest

    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>?

    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.

    John

    I
     
    John Draper, Aug 21, 2006
    #1
    1. Advertising

  2. John Draper

    Tim Roberts Guest

    John Draper <> 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,
    Providenza & Boekelheide, Inc.
     
    Tim Roberts, Aug 22, 2006
    #2
    1. Advertising

  3. John Draper

    Tim Williams Guest

    On 22/08/06, Tim Roberts <> wrote:
    > John Draper <> wrote:


    Hi Tim :)

    > 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.


    actually, I said:
    this will be a server from the recipient's domain's mx records (or
    your ISP server). :)

    However it really depends on the use-case, relaying through another
    server will give you no control over bad addresses, you have to wait
    for bounces from the recipient's server, or conversely the ISP server
    can give fails 4xx & 5xx for valid addresses. The ISP may only
    accept email addressed from their local domains, and you may be
    breaking their TOCs or AUP and get blocked.

    On the flip side, some ISPs block outbound port 25 except through
    their servers, or transparent proxy port 25, so Direct MX is
    unusable.

    Blacklists are hit and miss, even large ISPs can get listed. If you
    are on a "clean" range then it shouldn't matter, you can look up your
    IP on various blacklists to see if it is affected. Also Many fixed IP
    addresses don't have valid PTRs so they can fail as often as dynamic
    IP addresses which usually do have valid PTRs

    John Draper <> wrote:

    > Hmmm - the problem I have is if I knowingly
    > put in a bad recipient, and try to send to a
    > unknown user, I get all appearances that
    > the mail went through.


    Yes this will happen if you use a relay server.

    > Ok, so If I already have a MX hostname
    > of "mail.myhost.com", then I would put
    > into my "to_email"... <myusername>@myhost.com for


    Yes, if you just used username the server wouldn't know which domain
    the email was being sent to and therefore how to route it.

    > By the way, I'm sending this mail to a "sms"
    > gateway to a cellular provider, so the
    > username is their phone number.


    If you only ever send to this gateway then you might as well try MX
    records, you will have more control, but you will need to manage
    queueing yourself for temporary failures, or you may decide that if
    you get a temporary failure (4xx) to just fire the email off to your
    ISP server and let them deal with it.


    > But
    > (sigh), these providers don't appear
    > to tell me if I put in a bogus phone number.


    Unfortunately not all mail servers will fail an invalid address even
    if they aren't relaying the email. But in this case the SMS
    "gateway" is probably relaying to a backend system for the SMSc and
    will accept any address with a valid domain part.

    HTH :)
     
    Tim Williams, Aug 22, 2006
    #3
  4. John Draper

    John Draper Guest

    I will respond to part of this, although it wasn't directed to me.

    Tim Williams wrote:

    > However it really depends on the use-case, relaying through another
    > server will give you no control over bad addresses, you have to wait
    > for bounces from the recipient's server, or conversely the ISP server
    > can give fails 4xx & 5xx for valid addresses. The ISP may only
    > accept email addressed from their local domains, and you may be
    > breaking their TOCs or AUP and get blocked.


    I usually will have no problem with this, and for one reason is because
    before I add a new ISP to my spam reporting queue, I establish a
    direct SMTP connection with the ISP's "abuse" email server to confirm
    the Email is good. I get a lot more then my share of BAD or Bogus Emails
    listed in some of these whois queries I get.

    >
    > On the flip side, some ISPs block outbound port 25 except through
    > their servers, or transparent proxy port 25, so Direct MX is
    > unusable.


    I send all of my spam reports through a commercial T1 link.. I made
    the bad mistake of sending my spam reports directly from my ComCrap
    (err - comcast) account, and my service got hosed for a day or so.

    >> Hmmm - the problem I have is if I knowingly
    >> put in a bad recipient, and try to send to a
    >> unknown user, I get all appearances that
    >> the mail went through.

    >
    >
    > Yes this will happen if you use a relay server.


    yea - I know.... :-(

    >
    >> Ok, so If I already have a MX hostname
    >> of "mail.myhost.com", then I would put
    >> into my "to_email"... <myusername>@myhost.com for

    >
    >
    > Yes, if you just used username the server wouldn't know which domain
    > the email was being sent to and therefore how to route it.
    >
    >> By the way, I'm sending this mail to a "sms"
    >> gateway to a cellular provider, so the
    >> username is their phone number.

    >
    >
    > If you only ever send to this gateway then you might as well try MX
    > records, you will have more control, but you will need to manage
    > queueing yourself for temporary failures, or you may decide that if
    > you get a temporary failure (4xx) to just fire the email off to your
    > ISP server and let them deal with it.


    I'm currently exploring a number of different options at this time,
    including giving the customer an option to go back and re-visit their
    order page and get an update in the event the message bounces.

    I'm setting up a special "reply_to" address which I use to collect
    bounces, and customer's asknowledgement they got the content.
    Once I get the Ack from the customer, I'll KNOW they got the product.

    >
    >
    >> But
    >> (sigh), these providers don't appear
    >> to tell me if I put in a bogus phone number.

    >
    >
    > Unfortunately not all mail servers will fail an invalid address even
    > if they aren't relaying the email.


    I haven't really run into a lot of them, but I've had NO experience with
    SMTP->SMS gateways.

    > But in this case the SMS
    > "gateway" is probably relaying to a backend system for the SMSc and
    > will accept any address with a valid domain part.


    I already know that Sprint does this, but (sigh) I don't even get the
    bounces if
    I re-direct them to a specific Email box.... DARN - Foiled again.

    John
     
    John Draper, Aug 23, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sugapablo
    Replies:
    0
    Views:
    2,198
    sugapablo
    Sep 21, 2007
  2. Andrzej Adam Filip
    Replies:
    0
    Views:
    4,183
    Andrzej Adam Filip
    Mar 31, 2008
  3. Trans
    Replies:
    3
    Views:
    208
    Robert Klemme
    Jan 26, 2005
  4. ±è ÁØ¿µ
    Replies:
    7
    Views:
    266
    Einar Magnús Boson
    Nov 27, 2008
  5. User1014
    Replies:
    1
    Views:
    198
    Richard Cornford
    Nov 30, 2006
Loading...

Share This Page