ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:590) - python

I am sending a few scheduled emails via gmail, with python 2.7 on windows. A couple of days ago, I started getting this error, without having made any changes to the job, the python install or the windows server.
It is the first email of the day being sent. Subsequent emails go through just fine. When running manually it from command line in the morning, it runs just fine.
Any ideas of what could be wrong, or how to fix/workaround it are appreciated.
My method from 'mymethodsfile' look like this
import sys
import ast
from datetime import datetime
import smtplib
import mimetypes
from email.mime.multipart import MIMEMultipart
from email import encoders
from email.message import Message
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
def send(self):
msg = MIMEMultipart('alternative')
msg['From']=self.sender
msg['Subject']=self.subject
msg['To'] = ", ".join(self.recipients) # to must be array of the form ['mailsender135#gmail.com']
#msg.preamble = "preamble goes here"
#check if there are attachments if yes, add them
if self.attachments:
self.attach(msg)
#add html body after attachments
msg.attach(MIMEText(self.htmlbody, 'html'))
#send
s = smtplib.SMTP('smtp.gmail.com:587')
s.starttls()
s.login(self.sender,self.senderpass)
s.sendmail(self.sender, self.recipients, msg.as_string())
s.quit()
Error in the logs(script path and filename anonymised):
Traceback (most recent call last):
File "C:\path\filename.py", line 12, in <module>
mymail.send()
File "C:\path2\mymethodsfile.py", line 49, in send
s.starttls()
File "C:\Python27\lib\smtplib.py", line 649, in starttls
self.sock = ssl.wrap_socket(self.sock, keyfile, certfile)
File "C:\Python27\lib\ssl.py", line 911, in wrap_socket
ciphers=ciphers)
File "C:\Python27\lib\ssl.py", line 579, in __init__
self.do_handshake()
File "C:\Python27\lib\ssl.py", line 808, in do_handshake
self._sslobj.do_handshake()
ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:590)

stoping using system proxy while running the code may be helpful.

Related

Smtplib code working in python3.7 for linux but not for windows 10

I am trying to send automated mail through outlook using python3.7.The smtlib code is working fine for python installed in linux while the same code is returning me error for windows.
import time, os, smtplib
from datetime import datetime
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
print("sam")
s = smtplib.SMTP('smtp-mail.outlook.com', 587)
print("sam1")
s.starttls()
print("sam2")
s.login("samgupta#xyz.com", "Welcome")
message = "Hi"
print("sam3")
s.sendmail("samgupta#xyz.com", "abc#xyz.com", message)
s.quit()
This is the error message i am getting in windows python :
Traceback (most recent call last): File
"C:/Users/sysadmin/Desktop/mail.py", line 10, in
s = smtplib.SMTP('smtp-mail.outlook.com', 587) File "C:\Users\sysadmin\AppData\Local\Programs\Python\Python37-32\lib\smtplib.py",
line 251, in init
(code, msg) = self.connect(host, port) File "C:\Users\sysadmin\AppData\Local\Programs\Python\Python37-32\lib\smtplib.py",
line 336, in connect
self.sock = self._get_socket(host, port, self.timeout) File "C:\Users\sysadmin\AppData\Local\Programs\Python\Python37-32\lib\smtplib.py",
line 307, in _get_socket
self.source_address) File "C:\Users\sysadmin\AppData\Local\Programs\Python\Python37-32\lib\socket.py",
line 727, in create_connection
raise err File "C:\Users\sysadmin\AppData\Local\Programs\Python\Python37-32\lib\socket.py",
line 716, in create_connection
sock.connect(sa) TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond
after a period of time, or established connection failed because
connected host has failed to respond
Can you just try this bit? This should print the exceptions and we can fix the problem once we know the problem nature.
import smtplib
global server
try:
server = smtplib.SMTP_SSL( < host >, < port >)
except Exception as e:
print("{}".format(e))
try:
server.login("your username", "your password")
except Exception as e:
print("{}".format(e))
server.sendmail(
"from#address.com",
"to#address.com",
"this message is from python")
server.quit()

How to send email attachments with Python 3.6

would you mind helping me, please!
I use all code's from this page How to send email attachments with Python
but it didn't work =(
This is last version which i used
import smtplib
from smtplib import SMTP_SSL
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email import encoders
import os
filepath = 'D:/files/1.jpg'
fromaddr = "name#gmail.com"
toaddr = "name2#gmail.com"
password = '********'
mail_adr = 'smtp.gmail.com'
mail_port = 587
# Compose attachment
part = MIMEBase('application', "octet-stream")
part.set_payload(open(filepath, "rb").read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % os.path.basename(filepath))
# Compose message
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg.attach(part)
# Send mail
smtp = SMTP_SSL()
smtp.set_debuglevel(1)
smtp.connect(mail_adr, mail_port)
smtp.login(fromaddr, password)
smtp.sendmail(fromaddr, toaddr, msg.as_string())
smtp.quit()
and here are the errors I fall
connect: ('smtp.gmail.com', 587)
connect: ('smtp.gmail.com', 587)
Traceback (most recent call last):
File "C:/Users/Oleg/Desktop/444.py", line 31, in <module>
smtp.connect(mail_adr, mail_port)
File "C:\Users\Oleg\AppData\Local\Programs\Python\Python36-32\lib\smtplib.py", line 335, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Users\Oleg\AppData\Local\Programs\Python\Python36-32\lib\smtplib.py", line 1037, in _get_socket
server_hostname=self._host)
File "C:\Users\Oleg\AppData\Local\Programs\Python\Python36-32\lib\ssl.py", line 401, in wrap_socket
_context=self, _session=session)
File "C:\Users\Oleg\AppData\Local\Programs\Python\Python36-32\lib\ssl.py", line 808, in __init__
self.do_handshake()
File "C:\Users\Oleg\AppData\Local\Programs\Python\Python36-32\lib\ssl.py", line 1061, in do_handshake
self._sslobj.do_handshake()
File "C:\Users\Oleg\AppData\Local\Programs\Python\Python36-32\lib\ssl.py", line 683, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:749)
Have you tried emailpy? (I am the author)
It supports any attachment format unless your email provider restricts it.
Example:
import emailpy
emailManager = emailpy.EmailManager('yourcooladdress#domain.com', 'youremailpassword') # this may take a few seconds to generate
emailManager.send(['sendsomething#gmail.com', 'anotheremail#hotmail.com'], \
subject = 'Subject here', body = 'body text here', html = 'some html here', \
attachments = ['file1.png', 'file2.txt', 'file3.py'], \
nofileattach = {'file.txt': 'hi, this is some data'})
# send email to sendsomething#gmail.com and anotheremail#hotmail.com with subject "Subject here"
#, body "body text here", html "some html here", and some attachments:
# "file1.png", "file2.txt", "file3.py". Also, it adds a file called file.txt
# that has the contents "hi, this is some data"
The emailpy docs can either be downloaded on its PyPI page, or from here
All email servers have been successfully tested with emailpy, and there will be no errors with emailpy if you use it on python 3.x (emailpy is not supported in python 2, mainly because of the syntax)

TimeoutError when using python to send email

I'm trying to write a python script to send an email. And here is the code:
def main():
import smtplib
import email.mime.text
import email.mime.application
from email.mime.multipart import MIMEMultipart
from_email = 'xxx#gmail.com'
from_email_pwd = '123456'
to_email = 'yyy#gmail.com'
msg = MIMEMultipart()
msg['Subject'] = 'Subject'
msg['From'] = from_email
msg['To'] = to_email
body = email.mime.text.MIMEText("Body")
msg.attach(body)
s = smtplib.SMTP('smtp.gmail.com')
s.starttls()
s.login(from_email, from_email_pwd)
s.sendmail(from_email, to_email, msg.as_string())
s.quit()
return
main()
While I can successfully run this code on my personal laptop, I came across problems when I try to run it on the company's desktop (working on PyCharm IDE with interpreter Python 3.5.1). Here is the error message:
Traceback (most recent call last):
File "C:/PATH/stack.py", line 29, in <module>
main()
File "C:/PATH/stack.py", line 21, in main
s = smtplib.SMTP('smtp.gmail.com')
File "C:\Username\AppData\Local\Programs\Python\Python35-32\lib\smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "C:\Username\AppData\Local\Programs\Python\Python35-32\lib\smtplib.py", line 335, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Username\AppData\Local\Programs\Python\Python35-32\lib\smtplib.py", line 306, in _get_socket
self.source_address)
File "C:\Username\AppData\Local\Programs\Python\Python35-32\lib\socket.py", line 711, in create_connection
raise err
File "C:\Username\AppData\Local\Programs\Python\Python35-32\lib\socket.py", line 702, in create_connection
sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
Process finished with exit code 1
Any thoughts what's going on?

Smtplib doesn't Work

I got the script from the first example here:
https://docs.python.org/2/library/email-examples.html#email-examples
I made sure there is no file name similar to email.py.
import smtplib
# Import the email modules we'll need
from email.mime.text import MIMEText
fp = open(textfile, 'rb')
# Create a text/plain message
msg = MIMEText(fp.read())
fp.close()
# me == the sender's email address
# you == the recipient's email address
msg['Subject'] = 'The contents of %s' % textfile
msg['From'] = torontocoinowl#gmail.com
msg['To'] = torontocoinowl#gmail.com
# Send the message via our own SMTP server, but don't include the
# envelope header.
s = smtplib.SMTP('localhost')
s.sendmail(me, [you], msg.as_string())
s.quit()
The error is
C:\Users\donald\AppData\Local\Programs\Python\Python35-32\python.exe C:/Users/donald/PycharmProjects/untitled3/testingemail/ff.py
Traceback (most recent call last):
File "C:/Users/donald/PycharmProjects/untitled3/testingemail/ff.py", line 1, in <module>
import smtplib
File "C:\Users\donald\AppData\Local\Programs\Python\Python35-32\lib\smtplib.py", line 47, in <module>
import email.utils
File "C:\Users\donald\AppData\Local\Programs\Python\Python35-32\lib\email\utils.py", line 28, in <module>
import random
File "C:\Users\donald\PycharmProjects\untitled3\random.py", line 1
From random import randint
^
SyntaxError: invalid syntax
Process finished with exit code 1
You are having a file random.py in your folder. It clashes with the random modul in Python. Remove of rename that file!

Python SMTP error: "(10013, 'Permission denied')"

So I have this Python send-email file that sends an email (the text from a file) to a recipient specified inside the code:
import smtplib
from email.mime.text import MIMEText
msgContent = open(Sourcefilelocation, "rb")
msg = MIMEText(spam.read())
msgContent.close()
msg['Subject'] = SUBJECT
msg['From'] = FROM_CLIENT_EMAIL
msg['To'] = TO_CLIENT_EMAIL
#THE ERROR BELOW
s = smtplib.SMTP('localhost')
I understand that there is no mail server on localhost, but how do I make a mail server on that and if I can't then why does s = smtplib.SMTP('gmail.com') take so long if it even works? What do I need to make it work? Any help is appreciated.
output: >> Traceback (most recent call last):
File "<pyshell#24>", line 1, in <module>
s = smtplib.SMTP('gmail.com')
File "C:\Python25\lib\smtplib.py", line 244, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python25\lib\smtplib.py", line 310, in connect
raise socket.error, msg
error: (10013, 'Permission denied')
And the gmail smtp code takes too long.

Categories

Resources