469,645 Members | 1,653 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,645 developers. It's quick & easy.

SMTP Sending Mail Problem

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:<######@######.comsize=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 <############@###############.comQu
eued mail for delivery\r\n'
reply: retcode (250); Msg: 2.6.0 <##########@n#########.comQueued
mail for delivery
data: (250, '2.6.0 <########@################.comQ
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
Jan 28 '08 #1
1 2656

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.
Jan 28 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by dale zhang | last post: by
4 posts views Thread by z f | last post: by
11 posts views Thread by JD | last post: by
34 posts views Thread by antonyliu2002 | last post: by
11 posts views Thread by ibiza | last post: by
4 posts views Thread by =?Utf-8?B?dHBhcmtzNjk=?= | last post: by
7 posts views Thread by oopsbabies | last post: by
2 posts views Thread by =?Utf-8?B?QWRl?= | last post: by
9 posts views Thread by SREEDEV | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.