SMTP Sending Mail Problem

the_ricka rick.arnett at
Mon Jan 28 17:15:01 CET 2008

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

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 """
	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
fromaddr = sys.argv[1]
toaddrs = sys.argv[2].split(";")
subject = sys.argv[3]
body = ""
print toaddrs
print type(toaddrs)
	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"
headers = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (fromaddr, ",
".join(toaddrs), subject)
msg = headers + body
server = smtplib.SMTP('##############')
server.sendmail(fromaddr, toaddrs, msg)


send: 'ehlo\r\n'
reply: ' 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: Hello [10.#.#.#]
SIZE 20971520
send: 'mail FROM:<> size=116\r\n'
reply: '250 2.1.0 OK\r\n'
reply: retcode (250); Msg: 2.1.0 OK
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:\r\nSubject:
r\nThis is from the file\r\nThis concludes our test\r\n\r\n\r\n.\r\n'
reply: '250 2.6.0 <> Qu
eued mail for delivery\r\n'
reply: retcode (250); Msg: 2.6.0 <> Queued
mail for delivery
data: (250, '2.6.0 <> Q
ueued mail for delivery')
send: 'quit\r\n'
reply: '221 2.0.0 Service closing transmission
reply: retcode (221); Msg: 2.0.0 Service closing
ransmission channel

More information about the Python-list mailing list