SMTPlib inside function, extra tab

Discussion in 'Python' started by Hunter, Oct 7, 2008.

  1. Hunter

    Hunter Guest

    I am writing a script that needs to send some emails. And I've used
    smtplib in the past and it is pretty easy. But I thought, gee it
    would be easier if I could just call it as a function, passing the
    from, to, subject, and message text. So I wrote it up as a function
    and it sort of works, but I get a weird error. When it runs it
    inserts a "\t" tab character before each item during the send portion
    (which I can see when I turn on debug). The end result is that I
    don't get any body or subject in my emails. It works fine when I copy
    the inside of the function and run it directly. It isn't a
    dealbreaker, I can certainly just call it direct, but from a learning
    Python perspective I'm wondering if anyone knows what exactly is
    happening. I'm more interested in the why this is happening than a
    solution (though that would be great too). Oh and if you could
    explain it to me, with no CS background, that would be even better.

    I am working on Windows Vista with Python 2.5.2 (activestate).

    Thanks --Joshua

    Snip of script (more or less a copy/paste from effbot):
    fromaddress = ''
    tolist = ['','']
    msgsubj = "Hello!"
    messagebody = "This message was sent with Python's smtplib."


    def send_mail(fromaddress,tolist,msgsubj,messagebody):
    import smtplib
    SERVER = "mymailserver.mydomain.com"
    message = """\
    From: %s
    To: %s
    Subject: %s
    %s
    """ % (fromaddress, ", ".join(tolist),msgsubj, messagebody)
    print message
    server = smtplib.SMTP(SERVER)
    server.set_debuglevel(1)
    server.sendmail(fromaddress, tolist, message)
    server.quit()

    send_mail(fromaddress, tolist, msgsubj, messagebody)

    Output when called from function:
    send: 'ehlo twaus-mycomputer.mydomain.local\r\n'
    reply: '250-mymailserver.mydomain.com Hello [10.10.10.119]\r\n'
    reply: '250-TURN\r\n'
    reply: '250-SIZE\r\n'
    reply: '250-ETRN\r\n'
    reply: '250-PIPELINING\r\n'
    reply: '250-DSN\r\n'
    reply: '250-ENHANCEDSTATUSCODES\r\n'
    reply: '250-8bitmime\r\n'
    reply: '250-BINARYMIME\r\n'
    reply: '250-CHUNKING\r\n'
    reply: '250-VRFY\r\n'
    reply: '250-X-EXPS GSSAPI NTLM LOGIN\r\n'
    reply: '250-X-EXPS=LOGIN\r\n'
    reply: '250-AUTH GSSAPI NTLM LOGIN\r\n'
    reply: '250-AUTH=LOGIN\r\n'
    reply: '250-X-LINK2STATE\r\n'
    reply: '250-XEXCH50\r\n'
    reply: '250 OK\r\n'
    reply: retcode (250); Msg: mymailserver.mydomain.com Hello
    [10.10.10.119]

    TURN
    SIZE
    ETRN
    PIPELINING
    DSN
    ENHANCEDSTATUSCODES
    8bitmime
    BINARYMIME
    CHUNKING
    VRFY
    X-EXPS GSSAPI NTLM LOGIN
    X-EXPS=LOGIN
    AUTH GSSAPI NTLM LOGIN
    AUTH=LOGIN
    X-LINK2STATE
    XEXCH50
    OK
    send: 'mail FROM:<> size=159\r\n'
    reply: '250 2.1.0 ....Sender OK\r\n'
    reply: retcode (250); Msg: 2.1.0 ....Sender OK
    send: 'rcpt TO:<>\r\n'
    reply: '250 2.1.5 \r\n'
    reply: retcode (250); Msg: 2.1.5
    send: 'rcpt TO:<>\r\n'
    reply: '250 2.1.5 \r\n'
    reply: retcode (250); Msg: 2.1.5
    send: 'data\r\n'
    reply: '354 Start mail input; end with <CRLF>.<CRLF>\r\n'
    reply: retcode (354); Msg: Start mail input; end with <CRLF>.<CRLF>
    data: (354, 'Start mail input; end with <CRLF>.<CRLF>')
    send: "\tFrom: \r\n\tTo: , j
    \r\n\tSubject: Hello!\r\n\tThis message was sent
    with
    Python's smtplib.\r\n\t\r\n.\r\n"
    reply: '250 2.6.0
    <>
    Queued mail for delivery\r\n'
    reply: retcode (250); Msg: 2.6.0
    <mymailservergz1Lz7c0000fb75@mymailserver.
    mydomain.com> Queued mail for delivery
    data: (250, '2.6.0
    <
    > Queued mail for delivery')

    send: 'quit\r\n'
    reply: '221 2.0.0 mymailserver.mydomain.com Service closing
    transmission
    channel\r\n'
    reply: retcode (221); Msg: 2.0.0 mymailserver.mydomain.com Service
    closin
    g transmission channel
    From:
    To: ,
    Subject: Hello!
    This message was sent with Python's smtplib.

    Output if you just run the internal part of the function directly:
    send: 'ehlo twaus-mycomputer.mydomain.local\r\n'
    reply: '250-mymailserver.mydomain.com Hello [10.10.10.119]\r\n'
    reply: '250-TURN\r\n'
    reply: '250-SIZE\r\n'
    reply: '250-ETRN\r\n'
    reply: '250-PIPELINING\r\n'
    reply: '250-DSN\r\n'
    reply: '250-ENHANCEDSTATUSCODES\r\n'
    reply: '250-8bitmime\r\n'
    reply: '250-BINARYMIME\r\n'
    reply: '250-CHUNKING\r\n'
    reply: '250-VRFY\r\n'
    reply: '250-X-EXPS GSSAPI NTLM LOGIN\r\n'
    reply: '250-X-EXPS=LOGIN\r\n'
    reply: '250-AUTH GSSAPI NTLM LOGIN\r\n'
    reply: '250-AUTH=LOGIN\r\n'
    reply: '250-X-LINK2STATE\r\n'
    reply: '250-XEXCH50\r\n'
    reply: '250 OK\r\n'
    reply: retcode (250); Msg: mymailserver.mydomain.com Hello
    [10.10.10.119]

    TURN
    SIZE
    ETRN
    PIPELINING
    DSN
    ENHANCEDSTATUSCODES
    8bitmime
    BINARYMIME
    CHUNKING
    VRFY
    X-EXPS GSSAPI NTLM LOGIN
    X-EXPS=LOGIN
    AUTH GSSAPI NTLM LOGIN
    AUTH=LOGIN
    X-LINK2STATE
    XEXCH50
    OK
    send: 'mail FROM:<> size=154\r\n'
    reply: '250 2.1.0 ....Sender OK\r\n'
    reply: retcode (250); Msg: 2.1.0 ....Sender OK
    send: 'rcpt TO:<>\r\n'
    reply: '250 2.1.5 \r\n'
    reply: retcode (250); Msg: 2.1.5
    send: 'rcpt TO:<>\r\n'
    reply: '250 2.1.5 \r\n'
    reply: retcode (250); Msg: 2.1.5
    send: 'data\r\n'
    reply: '354 Start mail input; end with <CRLF>.<CRLF>\r\n'
    reply: retcode (354); Msg: Start mail input; end with <CRLF>.<CRLF>
    data: (354, 'Start mail input; end with <CRLF>.<CRLF>')
    send: "From: \r\nTo: , jhunt
    \r\nSubject: Hello!\r\nThis message was sent with
    Python's
    smtplib.\r\n.\r\n"
    reply: '250 2.6.0
    <>
    Queued mail for delivery\r\n'
    reply: retcode (250); Msg: 2.6.0
    <mymailservercDoXlFg0000fb76@mymailserver.
    mydomain.com> Queued mail for delivery
    data: (250, '2.6.0
    <
    > Queued mail for delivery')

    send: 'quit\r\n'
    reply: '221 2.0.0 mymailserver.mydomain.com Service closing
    transmission
    channel\r\n'
    reply: retcode (221); Msg: 2.0.0 mymailserver.mydomain.com Service
    closin
    g transmission channel
     
    Hunter, Oct 7, 2008
    #1
    1. Advertising

  2. Hunter

    Hunter Guest

    Thank you Tino. I appreciate the help.

    Duh! Anything inside """ """ is preformatted text. I have tabs
    inside my preformatted text (without even thinking because it looks
    more normal because of the indent). I removed them and voila!

    def send_mail(fromaddress,tolist,msgsubj,messagebody):
    import smtplib
    SERVER = "mymailserver.mydomain.com"
    message = """\
    From: %s
    To: %s
    Subject: %s
    %s
    """ % (fromaddress, ", ".join(tolist),msgsubj, messagebody)
    print message
    server = smtplib.SMTP(SERVER)
    server.set_debuglevel(1)
    server.sendmail(fromaddress, tolist, message)
    server.quit()

    --Joshua
     
    Hunter, Oct 9, 2008
    #2
    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. Hermit Dave
    Replies:
    0
    Views:
    454
    Hermit Dave
    Jan 22, 2004
  2. Steve Jones
    Replies:
    0
    Views:
    579
    Steve Jones
    Feb 18, 2004
  3. Evan
    Replies:
    3
    Views:
    495
  4. mathieu
    Replies:
    3
    Views:
    609
    Bo Persson
    Sep 4, 2009
  5. Sur
    Replies:
    4
    Views:
    208
Loading...

Share This Page