473,699 Members | 2,664 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

smtplib and TLS

Hi!

After getting a @gmail.com address, I recognized I had to use TLS in my
python scripts using smtplib in order to get mail to the smtp.gmail.com
server.

Things work well so far, apart from an unexpected error. Here's my
sample code:

import smtplib

server = smtplib.SMTP('s mtp.gmail.com', 587)
server.ehlo()
server.starttls ()
server.ehlo()
server.login('m *****@gmail.com ', password)
server.sendmail ("mk****@gmail. com", toaddress, message)
server.quit()

The server accepts and delivers my messages, but the last command
raises

socket.sslerror : (8, 'EOF occurred in violation of protocol')

Did I miss something? Any hint is welcome.

Regards,
Matthias

Jul 19 '05 #1
6 10015
"Matthias Kluwe" <mk****@gmail.c om> writes:
The server accepts and delivers my messages, but the last command
raises

socket.sslerror : (8, 'EOF occurred in violation of protocol')

Did I miss something? Any hint is welcome.


Looks like the module didn't send an TLS Close Notify message before
closing the socket. I don't see anything in the docs about how to
send one from smtplib or socket, though.
Jul 19 '05 #2
> "Matthias Kluwe" <mkl...@gmail.c om> writes:
The server accepts and delivers my messages, but the last command
raises socket.sslerror : (8, 'EOF occurred in violation of protocol') Did I miss something? Any hint is welcome.
Looks like the module didn't send an TLS Close Notify message before
closing the socket. I don't see anything in the docs about how to
send one from smtplib or socket, though.


Hmm. I tried

server.sock.rea lsock.shutdown( 2)

before server.quit() with the result of

SMTPServerDisco nnected('Server not connected')

being raised. Quite an improvement ...

Matthias

Jul 19 '05 #3
"Matthias Kluwe" <mk****@gmail.c om> writes:
Hmm. I tried

server.sock.rea lsock.shutdown( 2)
before server.quit() with the result of


I don't think that's exactly what you want. You need to send a
specific TLS message BEFORE shutting down the socket, to tell the
other end that the TLS connection is ending. That tells the server
that it shouldn't accept a TLS session resumption later. The close
notify message is required because if you don't send it, an attacker
could truncate one of your TLS messages by cutting your connection.

Basically the socket library's SSL implementation is pretty crude.
You might try http://trevp.net/tlslite for a pure-Python
implementation that's also still missing stuff, but is getting there.
Jul 19 '05 #4

----- Original Message -----
From: "Paul Rubin" "http://phr.cx"@NOSPAM. invalid

"Matthias Kluwe" <mk****@gmail.c om> writes:
Hmm. I tried

server.sock.rea lsock.shutdown( 2)
before server.quit() with the result of
I don't think that's exactly what you want. You need to send a
specific TLS message BEFORE shutting down the socket, to tell the
other end that the TLS connection is ending. That tells the server
that it shouldn't accept a TLS session resumption later. The close
notify message is required because if you don't send it, an attacker
could truncate one of your TLS messages by cutting your connection.

Basically the socket library's SSL implementation is pretty crude.
You might try http://trevp.net/tlslite for a pure-Python
implementation that's also still missing stuff, but is getting there.


I have found problems with the TLS built into smtplib when you are doing
something with sock elswhere in your app.
eg for me using [something].sock.settimeou t(x) or setting the default
timeout anywhere broke TLS in smtplib.

Have you verified that its your end that is broken, not gmail's, do other
servers give the same response ? The following servers accept incoming
TLS on port 25

e32.co.us.ibm.c om
mail.donkeyisla nd.com
smtp.myrealbox. com

And for quick tests you don't need to send an email (or authenticate), just
use a NOOP after STARTTLS (and perhaps a RSET) then QUIT eg

server = smtplib.SMTP(ho stname [,port])
server.set_debu glevel(1)
server.ehlo('x' )
server.starttls ()
server.ehlo('x' )
server.noop()
server.rset()
server.quit()
Trevor's http://trevp.net/tlslite did the job nicely, solving my previous
TLS problems

(completely untested)

from tlslite.api import *

server = SMTP_TLS('smtp. gmail.com', 587)
server.set_debu glevel(1)
server.ehlo()
settings = HandshakeSettin gs()
server.starttls (settings=setti ngs)
server.ehlo()
server.login('m k****@gmail.com ', password)
server.sendmail ("mk****@gmail. com", toaddress, message)
server.quit()

HTH :)


Jul 19 '05 #5
> From: "Paul Rubin" "http://phr.cx"@NOSPAM. invalid
"Matthias Kluwe" <mkl...@gmail.c om> writes:
After getting a @gmail.com address, I recognized I had to use TLS in my
python scripts using smtplib in order to get mail to the smtp.gmail.com
server. [...] The server accepts and delivers my messages, but the last command
raises socket.sslerror : (8, 'EOF occurred in violation of protocol')
[...] Have you verified that its your end that is broken, not gmail's, do other
servers give the same response ?


No, I have not -- I should have, as I know now: Connecting, starttls,
login and sending mail works fine without the above mentioned error
using my previous mail provider.

Does that mean Gmail is in error here? I don't know...

Regards,
Matthias

Jul 19 '05 #6
----- Original Message -----
From: "Matthias Kluwe" <mk****@gmail.c om>
Have you verified that its your end that is broken, not gmail's, do other servers give the same response ?


No, I have not -- I should have, as I know now: Connecting, starttls,
login and sending mail works fine without the above mentioned error
using my previous mail provider.

Does that mean Gmail is in error here? I don't know...

Looks like it is GMAIL , (though TLS is not required to be able to send via
smtp.gmail.com: 587 )

TLS using TLSlite also fails when connecting to GMAIL, but not to other
servers.

('5 send:', '(16:39:23) ehlo x\r\n')
('6 reply:', '(16:39:23) 250-mx.gmail.com at your service\r\n')
('6 reply:', '(16:39:23) 250-SIZE 20971520\r\n')
('6 reply:', '(16:39:23) 250-8BITMIME\r\n')
('6 reply:', '(16:39:23) 250-STARTTLS\r\n')
('6 reply:', '(16:39:23) 250 ENHANCEDSTATUSC ODES\r\n')
('5 send:', '(16:39:23) STARTTLS\r\n')
('6 reply:', '(16:39:23) 220 2.0.0 Ready to start TLS\r\n')
('Status:', '(16:39:24) 2202.0.0 Ready to start TLS')
('5 send:', '(16:39:24) ehlo x\r\n')
('6 reply:', '(16:39:24) 250-mx.gmail.com at your service\r\n')
('6 reply:', '(16:39:24) 250-SIZE 20971520\r\n')
('6 reply:', '(16:39:24) 250-8BITMIME\r\n')
('6 reply:', '(16:39:24) 250-AUTH LOGIN PLAIN\r\n')
('6 reply:', '(16:39:24) 250 ENHANCEDSTATUSC ODES\r\n')
('5 send:', '(16:39:24) noop\r\n')
('6 reply:', '(16:39:24) 250 2.0.0 OK\r\n')
('5 send:', '(16:39:24) rset\r\n')
('6 reply:', '(16:39:24) 250 2.1.0 Flushed d61sm2700367wra \r\n')
('5 send:', '(16:39:24) noop\r\n')
('6 reply:', '(16:39:24) 250 2.0.0 OK\r\n')
('5 send:', '(16:39:24) quit\r\n')
Traceback (most recent call last):
File "C:\test\tls.py ", line 103, in ?
s.quit()
File "C:\test\smtpli b.py", line 737, in quit
self.docmd("qui t")
File "C:\test\smtpli b.py", line 395, in docmd
return self.getreply()
File "C:\test\smtpli b.py", line 367, in getreply
line = self.file.readl ine()
File "C:\Python23\Li b\site-packages\tlslit e\FileObject.py ", line 152, in
readline
data = self._sock.recv (self._rbufsize )
File "C:\Python23\Li b\site-packages\tlslit e\TLSRecordLaye r.py", line 393,
in recv
return self.read(bufsi ze)
File "C:\Python23\Li b\site-packages\tlslit e\TLSRecordLaye r.py", line 182,
in read
for result in self.readAsync( max, min):
File "C:\Python23\Li b\site-packages\tlslit e\TLSRecordLaye r.py", line 201,
in readAsync
for result in self._getMsg(Co ntentType.appli cation_data):
File "C:\Python23\Li b\site-packages\tlslit e\TLSRecordLaye r.py", line 564,
in _getMsg
for result in self._getNextRe cord():
File "C:\Python23\Li b\site-packages\tlslit e\TLSRecordLaye r.py", line 737,
in _getNextRecord
raise TLSAbruptCloseE rror()
tlslite.errors. TLSAbruptCloseE rror
Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
10955
by: Bill | last post by:
I am trying to have the capability to email attachments. Specifically I want to be able to email a specific attachment that I name that may be a PDF document, text doc, etc. I already have a working piece of code that emails jpg attachments but does not work with any other types of attachments. Could someone tell me how to modify this code to send other types of attachments like the one's stated above(especially PDF's)? Also how do I...
1
2907
by: David Hughes | last post by:
I wonder if anyone can help me resolve this problem. Although my ISP (One and One) provides the facility to run Python 2.2 CGI programs, their technical support people don't seem to have any Python expertise. I am trying to run Gypsymail.py as a CGI program from a web form. It is nearly all working, except for the part that I have reduced to the following code: #!/usr/bin/python # the line above must contain the path to Python on your...
0
1743
by: Frank Zheng | last post by:
I wrote some code to test "smtplib", but i met a problem when i call the "login(user,pass)" of the "SMTP" object. here are the codes: >>> s = smtplib.SMTP() >>> s.set_debuglevel(1) >>> s.connect('smtp.263.net') connect: ('smtp.263.net', 25) connect: ('smtp.263.net', 25) reply: '220 Welcome to coremail System(With Anti-Spam) 2.1 for
3
4672
by: yazzoo | last post by:
Does anyone know what causes an SMTPLIB DataError? The only thing I can find in the manual is exception SMTPDataError The SMTP server refused to accept the message data. My problem is a previously functioning script suddenly failed last week. My ISP thinks it maybe something to do with upgrading CPanel on their server -- maybe a library was missed, but when I try and execute a script I get...
6
2319
by: stewart.midwinter | last post by:
I'm having problem with a script that used to work under Win2k but is now broken after an install of WinXP Pro. I can no longer connect to a local mail server. Has anyone else seen this? If so, were you able to work around it? Here's the traceback (below). Interestingly, if I change ports to the POP port 110, I get a different error, but one that lets me know that I can reach the server. trying to connect on the SMTP port 25: >>>...
0
3041
by: Tim Williams | last post by:
I have a working SMTP client that I need to add TLS capability to, I absolutely need the client to timeout within a specified time, but when I use the sock.timeout() line it freezes the reading of chars from SSLFakeFile used during TLS instead of timing out the client connection. I am working from my own hacked version of smtplib, but can demonstrate the problem using the standard module (Python 2.3.4). Can anyone suggest a...
3
4393
by: Van_Gogh | last post by:
Hi, I am learning how to use the smtplib module, but am having some very early problems, maybe because I don't understand it. So, am I correct that by following the example in the Python: >>> import smtplib >>> server = smtplib.SMTP('localhost') >>> server.sendmail('soothsayer@example.org', 'jcaesar@example.org', """To: jcaesar@example.org
0
1864
by: Roger | last post by:
I am having a problem sending email through smtp.gmail.com using smtplib. Everything works and the mail is sent and received, except quit. The following shows the problem (without bothering to login or do the sendmail): I have not had this problem with other mail servers (but only tried two). A solution for gmail seems to be to replace the server.quit() with server.close(). The difference between the two commands is quit() sends a...
5
4684
by: zxo102 | last post by:
Hi, I am trying to use python module smtplib to send my email out on window xp (localhost). import smtplib server = smtplib.SMTP('localhost') but I got the error information as follows: Traceback (most recent call last):
1
2386
by: Hunter | last post by:
I am writing a script that needs to send some emails. And I've used smtplib in the past and it is pretty easy. But I thought, gee it would be easier if I could just call it as a function, passing the from, to, subject, and message text. So I wrote it up as a function and it sort of works, but I get a weird error. When it runs it inserts a "\t" tab character before each item during the send portion (which I can see when I turn on...
0
8618
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9178
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9035
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8916
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5875
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4376
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2348
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2010
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.