SMTP Sending Mail Problem

Discussion in 'Python' started by the_ricka, Jan 28, 2008.

  1. the_ricka

    the_ricka Guest

    Hi all,
    I'm fairly new to python, but very excited about it's potential.

    I'm trying to write a simple program that will accept input from a
    command line and send email. The parameters I used on the command
    line are for From address, To addresses, Subject and Body. For the
    body, I thought it would be better to read the input from a file so I
    could allow someone to nicely format the body with newlines in a text
    editor and just read it into a string and send it.

    However, whenever I try to read more than one line from the file, the
    email is not being delivered. The only reason I know this is because
    I tried just reading in the first line of the text file, and the email
    sent fine. Right now I believe this must have something to do with
    new line characters at the end of each line, but it doesn't quite make
    sense to me why this is a problem, as I have also used the same script
    and just created a string in it with multiple lines (\r\n) and sent it
    and the email sends fine. To complicate the issue further, I have
    turned used the set_debuglevel method so I can see the output from the
    SMTP server, and it appears that the message was Queued for mail
    delivery.

    The SMTP server we are using is Exchange if that matters. I have
    listed the code below, along with output from the SMTP server. Any
    help would be much appreciated. (Note, I don't typically read in the
    file with a range(n) iterator, but that is how I have noticed that I
    can send only one line, but not send multiple lines. As soon as I
    change the range value to 1, the email will successfully send the
    first line of the text file).

    import sys,smtplib
    if len(sys.argv) != 5:
    print """
    Usage: sendmail FROM RECIPIENTS SUBJECT BODY
    RECIPIENTS should be a semicolon delimited list of email addresses.
    BODY should be a file where the message to send is stored
    Use double quotes to surround the SUBJECT element if it is more than
    one word
    """
    exit()
    fromaddr = sys.argv[1]
    toaddrs = sys.argv[2].split(";")
    subject = sys.argv[3]
    body = ""
    print toaddrs
    print type(toaddrs)
    try:
    infile = open(sys.argv[4], 'r')
    for i in range(4):
    body = body + infile.readline()
    except IOError:
    print "Can't open the given file, please try again"
    exit()
    headers = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (fromaddr, ",
    ".join(toaddrs), subject)
    msg = headers + body
    server = smtplib.SMTP('##############')
    server.set_debuglevel(1)
    server.sendmail(fromaddr, toaddrs, msg)
    server.quit()

    Output:

    send: 'ehlo hplaptopr.############.com\r\n'
    reply: '250-################.com Hello [10.#.#.#]\r\n'
    reply: '250-SIZE 20971520\r\n'
    reply: '250-DSN\r\n'
    reply: '250-VRFY\r\n'
    reply: '250-AUTH GSSAPI NTLM LOGIN\r\n'
    reply: '250-AUTH=LOGIN\r\n'
    reply: '250 OK\r\n'
    reply: retcode (250); Msg: ################.com Hello [10.#.#.#]
    SIZE 20971520
    DSN
    VRFY
    AUTH GSSAPI NTLM LOGIN
    AUTH=LOGIN
    OK
    send: 'mail FROM:<######@######.com> size=116\r\n'
    reply: '250 2.1.0 #######@######.com....Sender OK\r\n'
    reply: retcode (250); Msg: 2.1.0 ######@######.com....Sender OK
    send: 'rcpt TO:<######@######.com>\r\n'
    reply: '250 2.1.5 ######@######.com \r\n'
    reply: retcode (250); Msg: 2.1.5 #######@######.com
    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: #######@######.com\r\nTo: ######@######.com\r\nSubject:
    Test1\r\n\
    r\nThis is from the file\r\nThis concludes our test\r\n\r\n\r\n.\r\n'
    reply: '250 2.6.0 <############@###############.com> Qu
    eued mail for delivery\r\n'
    reply: retcode (250); Msg: 2.6.0 <##########@n#########.com> Queued
    mail for delivery
    data: (250, '2.6.0 <########@################.com> Q
    ueued mail for delivery')
    send: 'quit\r\n'
    reply: '221 2.0.0 ################.com Service closing transmission
    cha
    nnel\r\n'
    reply: retcode (221); Msg: 2.0.0 #################.com Service closing
    t
    ransmission channel
     
    the_ricka, Jan 28, 2008
    #1
    1. Advertising

  2. On 28 ene, 14:15, the_ricka <> wrote:

    > However, whenever I try to read more than one line from the file, the
    > email is not being delivered.  The only reason I know this is because
    > I tried just reading in the first line of the text file, and the email
    > sent fine.  Right now I believe this must have something to do with
    > new line characters at the end of each line, but it doesn't quite make
    > sense to me why this is a problem, as I have also used the same script
    > and just created a string in it with multiple lines (\r\n) and sent it
    > and the email sends fine.  To complicate the issue further, I have
    > turned used the set_debuglevel method so I can see the output from the
    > SMTP server, and it appears that the message was Queued for mail
    > delivery.


    Your code is fine, and the message was queued as you noticed. Maybe
    the recipient has some sort of black list, didn't like the sender, the
    message was classified as spam, or something like that. I'd look for
    problems elsewhere, not in the code.

    > send: 'From: #######@######.com\r\nTo: ######@######.com\r\nSubject:
    > Test1\r\n\
    > r\nThis is from the file\r\nThis concludes our test\r\n\r\n\r\n.\r\n'
    > reply: '250 2.6.0 <############@###############.com> Qu
    > eued mail for delivery\r\n'
    > reply: retcode (250); Msg: 2.6.0 <##########@n#########.com> Queued
    > mail for delivery
    > data: (250, '2.6.0 <########@################.com> Q
    > ueued mail for delivery')
    > send: 'quit\r\n'
    > reply: '221 2.0.0 ################.com Service closing transmission
    > cha
    > nnel\r\n'
    > reply: retcode (221); Msg: 2.0.0 #################.com Service closing
    > t
    > ransmission channel


    --
    Gabriel Genellina
     
    Gabriel Genellina, Jan 28, 2008
    #2
    1. Advertising

  3. the_ricka

    the_ricka Guest

    I added a longer text file with vocabulary typically used in our
    office and the email sent successfully. The crazy thing is I had
    already checked all the spam filters and queues in Exchange, and the
    older messages seem to have disappeared.

    Sorry for the post, I thought I was going crazy for a bit! Thanks
    again for the sanity check.
     
    the_ricka, Jan 28, 2008
    #3
    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. Scott Allen
    Replies:
    2
    Views:
    2,234
    =?Utf-8?B?c2FtaWtkdXQ=?=
    Feb 17, 2004
  2. Replies:
    3
    Views:
    1,103
    Roedy Green
    Jan 24, 2006
  3. Alex Hunsley
    Replies:
    4
    Views:
    3,578
    Tim Williams (gmail)
    Jun 29, 2004
  4. Raaijmakers, Vincent \(GE Infrastructure\)

    RE: sending of mail (smtp) - connection refused - but smtp serveris running!

    Raaijmakers, Vincent \(GE Infrastructure\), Jun 29, 2004, in forum: Python
    Replies:
    0
    Views:
    896
    Raaijmakers, Vincent \(GE Infrastructure\)
    Jun 29, 2004
  5. Replies:
    0
    Views:
    1,415
Loading...

Share This Page