SMTP Sending Mail Problem

the_ricka rick.arnett at gmail.com
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
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



More information about the Python-list mailing list