sending emails using python

S

sridhar

iam having user account on an exchangeserver.
with that can i send an email using python?

if iam using the following code iam getting error


fromAddress = '(e-mail address removed)'
toAddress = '(e-mail address removed)'
msg = "Subject: Hello\n\nThis is the body of the message."
import smtplib
server = smtplib.SMTP("hstmsg002",25)
server.sendmail(fromAddress, toAddress, msg)

error:

Traceback (most recent call last):
File
"C:\sridhar\Beginning_Python\Beginning_Python\Chapter16\tryitout\InitialMailExample.py",
line 5, in ?
server = smtplib.SMTP("hstmsg002",25)
File "C:\Python24\lib\smtplib.py", line 244, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python24\lib\smtplib.py", line 307, in connect
(code, msg) = self.getreply()
File "C:\Python24\lib\smtplib.py", line 351, in getreply
raise SMTPServerDisconnected("Connection unexpectedly closed")
SMTPServerDisconnected: Connection unexpectedly closed
 
S

Sybren Stuvel

sridhar enlightened us with:
iam having user account on an exchangeserver.
with that can i send an email using python?

if iam using the following code iam getting error


fromAddress = '(e-mail address removed)'
toAddress = '(e-mail address removed)'
msg = "Subject: Hello\n\nThis is the body of the message."
^^^^

You need \r\n\r\n there. Line-ends in email messages should be DOS
line-ends.
Traceback (most recent call last):
File
"C:\sridhar\Beginning_Python\Beginning_Python\Chapter16\tryitout\InitialMailExample.py",
line 5, in ?
server = smtplib.SMTP("hstmsg002",25)
File "C:\Python24\lib\smtplib.py", line 244, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python24\lib\smtplib.py", line 307, in connect
(code, msg) = self.getreply()
File "C:\Python24\lib\smtplib.py", line 351, in getreply
raise SMTPServerDisconnected("Connection unexpectedly closed")
SMTPServerDisconnected: Connection unexpectedly closed

Well that's useless. You could install a network sniffer
(http://www.ethereal.com/) and find out what's actually sent over the
network connection, and where things go wrong.

Sybren
 
T

Tim Williams

sridhar enlightened us with:

Well that's useless. You could install a network sniffer
(http://www.ethereal.com/) and find out what's actually sent over the
network connection, and where things go wrong.

Have you verified that you are allowed to use SMTP on this server ?
Can you send email via it using outlook express or a similar POP3/IMAP
mail client?

:)
 
S

Steve Holden

Sybren said:
sridhar enlightened us with:


^^^^

You need \r\n\r\n there. Line-ends in email messages should be DOS
line-ends.
This is untrue for the Python smtplib, though correct according to the
RFCs. The SMTP.data() method uses a locally-declared function called
quotedata() to ensure the correct line endings, so using "\n" will
result in the same message as using "\r\n".
Well that's useless. You could install a network sniffer
(http://www.ethereal.com/) and find out what's actually sent over the
network connection, and where things go wrong.
Useless it might be, but it's a lot more clueful than some questions we
see, so it seems a little dismissive just to say "it's useless" - the OP
has provided the traceback, which is so often missing in questions from
newbies, and has correctly localized the error to a small chunk of code.

True, the error message isn't particularly helpful in these
circumstances, but it probably represents an accurate description (from
the client's point of view) about what's happening, in which case an
Ethereal trace will provide little more data than the traceback. I
suppose it'll tell you whether the server is sending RST or FIN to
terminate the connection, but it won't give much insight into why.

We might hazard a guess that the connection is being closed because
there is no SMTP server running on the named host, or (less likely) it
requires authentication. Unfortunately when I try to connect to a
non-existent SMTP server on an existent host I see

socket.error: (113, 'Software caused connection abort')

under both 2.4.3 and 2.5b2. Alas I don't have ready access to a server
that requires authentication (my servers authenticate senders by POP
access IIRC). So I can't reproduce this error.

Perhaps the SMTP server is strapped down to accepting connections from
specific IP addresses by some firewall? It would probably be a good idea
to have a chat with the server's administrator to see if they can
suggest what might be going wrong.

regards
Steve
 
M

Max M

sridhar said:
iam having user account on an exchangeserver.
with that can i send an email using python?

if iam using the following code iam getting error


fromAddress = '(e-mail address removed)'
toAddress = '(e-mail address removed)'
msg = "Subject: Hello\n\nThis is the body of the message."
import smtplib
server = smtplib.SMTP("hstmsg002",25)
server.sendmail(fromAddress, toAddress, msg)


Do yourself a favor and use the email module to construct your messages
with. Constructing messages by hand can lead you into so many traps.
Especially if you are using international characters in you messages.


--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science

Phone: +45 66 11 84 94
Mobile: +45 29 93 42 96
 
S

Steve Holden

Max said:
Do yourself a favor and use the email module to construct your messages
with. Constructing messages by hand can lead you into so many traps.
Especially if you are using international characters in you messages.
Good advice. However, since the error is occurring in the SMTP() call a
malformed message is clearly not the cause of this particular problem.

regards
Steve
 
S

Sybren Stuvel

Steve Holden enlightened us with:
This is untrue for the Python smtplib, though correct according to
the RFCs. The SMTP.data() method uses a locally-declared function
called quotedata() to ensure the correct line endings, so using "\n"
will result in the same message as using "\r\n".

Ah, wonderful.
Useless it might be, but it's a lot more clueful than some questions
we see, so it seems a little dismissive just to say "it's useless" -
the OP has provided the traceback, which is so often missing in
questions from newbies, and has correctly localized the error to a
small chunk of code.

I agree with you. My remark might indeed have sounded harsher than
intended. It referred to the traceback itself, not the posting of the
traceback. That was rather useful.
Ethereal trace will provide little more data than the traceback.

I don't know until I see it. Perhaps the socket is closed in response
to something the client sends. That can be seen in the Ethereal trace.
I suppose it'll tell you whether the server is sending RST or FIN to
terminate the connection, but it won't give much insight into why.

A quick peek in the SMTP logfiles should tell you that.
Perhaps the SMTP server is strapped down to accepting connections from
specific IP addresses by some firewall?

In that case I'd expect the SMTP server to simply ignore the
connection attempt and cause a timeout in the connecting phase. I
wouldn't expect a connection to be created and then closed again,
which is what I read in the posted traceback.
It would probably be a good idea to have a chat with the server's
administrator to see if they can suggest what might be going wrong.

Good idea indeed. Another idea would be to use something like netcat
or telnet to connect to the port and see what happens if you manually
type an SMTP connection.

Sybren
 
S

Sybren Stuvel

Tim Williams enlightened us with:
Can you send email via it using outlook express or a similar
POP3/IMAP mail client?

Wouldn't you use a SMTP client to send email?

Sybren
 
T

Tim Williams

Tim Williams enlightened us with:

Wouldn't you use a SMTP client to send email?

Outlook Express *is* a mail client that uses SMTP as the outbound protocol.

:)
 
G

Grant Edwards

Tim Williams enlightened us with:

Wouldn't you use a SMTP client to send email?

I would, but I don't use exchange server. :)

The one exchange server I used in the past didn't accept SMTP
mail.
 
T

Tim Williams

Grant Edwards wrote:
On 2006-09-07, Sybren Stuvel


Tim Williams enlightened us with:


Can you send email via it using outlook express or a similar
POP3/IMAP mail client?

Wouldn't you use a SMTP client to send email?

I would, but I don't use exchange server. :)

The one exchange server I used in the past didn't accept SMTP
mail.


errr, I used to admin Exchange, if it does accept SMTP then how could it
function as a live mail server?

Did you mean *doesn't accept* ??

There are a couple of possibilities at least, of the top of my head:

* The IMC / IMS (smtp connector) is located on a different server
within its Exchange organisation, or a DMZ'ed exchange server that is
only used for SMTP.

* The server doesn't accept SMTP from local IP ranges, only from
external (ie inbound email)

Outlook / Exchange clients use MAPI, internal SMTP is only a
requirement if you have non-MAPI clients sending email through the
server.

HTH :)
 
G

Grant Edwards

Did you mean *doesn't accept* ??

One presumes he did.
There are a couple of possibilities at least, of the top of my
head:

* The IMC / IMS (smtp connector) is located on a different
server within its Exchange organisation, or a DMZ'ed
exchange server that is only used for SMTP.

* The server doesn't accept SMTP from local IP ranges, only
from external (ie inbound email)

I believe it was the latter. I'm pretty sure there was only
one server. When I first started at that company it did accept
SMTP connections on it's internal network interface.
Outlook / Exchange clients use MAPI, internal SMTP is only a
requirement if you have non-MAPI clients sending email through
the server.

And BOFH was horrified by non-MS software, so he shut off IMAP
support and SMTP support on the internal network as a way to
force everybody to switch to Outlook.
 
S

sridhar

Tim said:
Have you verified that you are allowed to use SMTP on this server ?
Can you send email via it using outlook express or a similar POP3/IMAP
mail client?

:)


yes , because iam having account on that server
 
T

Tim Williams

yes , because iam having account on that server

Having an account on an Exchange server doesn't automatically mean you
have SMTP access to that server. Some exchange servers don't have
SMTP running at all, and others will not allow SMTP access from local
IP addresses - even with authentication.

Can you telnet to your Exchange server on port 25 and start an SMTP
conversation, using the EHLO or HELO, MAIL and RCPT commands, but
quitting before the DATA command? If you can successfully specify a
recipient, then your script should work, if not you will need your
script to work around the problem based on what you do or don't get
back from the server.

HTH :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top