Reputation: 15
Using smtplib in python3 I am facing an issue using it on an ec2 machine of AWS
I am using the folowing function :
def ovh_send_email(sender_name, sender_address, recipient_addresses, subject, body_text, body_html, attachments = []):
mail_username = u'[email protected]'
mail_password = u'XXXXXXXXXXXXXXXXXX'
mail_smtp_server = "ssl0.ovh.net"
mail_smtp_port = 465
session = smtplib.SMTP_SSL(mail_smtp_server, mail_smtp_port) #Initiate connection to the server
session.set_debuglevel(1)
session.ehlo() #Start encrypting everything you're sending to the server
session.login(mail_username, mail_password) #Define the recipient of the email
msg = MIMEMultipart('mixed')
msg['Subject'] = subject
msg['From'] = sender_name + " <" + sender_address + ">"
msg['To'] = ", ".join(recipient_addresses)
html = body_html
part2 = MIMEText(body_html, 'html', 'utf-8')
for attachment_name in attachments:
attachment = MIMEApplication(attachments[attachment_name])
attachment.add_header("Content-Disposition", "attachment", filename=attachment_name)
msg.attach(attachment)
msg.attach(part2)
session.sendmail(mail_username, recipient_addresses, msg.as_string())#Close the connection to the SMTP server
session.quit()
What I get is
Exception Type: ConnectionResetError at /contact-email
Exception Value: [[Errno 104] Connection reset by peer
I have also tryed with the 587 port to send mail with no ssh but the problem remains
What is strange is that it doss not occure on another EC2 when I run the ovh_send_email function directly from the terminal
once I get this in terminal but I never saw it again whet I tryed some more time ...
ovh_send_email(sender_name, sender_address, recipient_addresses, subject, body_text, body_html)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 8, in ovh_send_email
File "/usr/lib/python3.5/smtplib.py", line 1021, in __init__
source_address)
File "/usr/lib/python3.5/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python3.5/smtplib.py", line 335, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python3.5/smtplib.py", line 1029, in _get_socket
server_hostname=self._host)
File "/usr/lib/python3.5/ssl.py", line 385, in wrap_socket
_context=self)
File "/usr/lib/python3.5/ssl.py", line 760, in __init__
self.do_handshake()
File "/usr/lib/python3.5/ssl.py", line 996, in do_handshake
self._sslobj.do_handshake()
File "/usr/lib/python3.5/ssl.py", line 641, in do_handshake
self._sslobj.do_handshake()
ConnectionResetError: [Errno 104] Connection reset by peer
Any idea to help ? THX !!!
Upvotes: 0
Views: 2930
Reputation: 436
Encoding special characters was causing the error in my case. I got rid of them and of: .encode('utf-8') on my sendmail command
Upvotes: 0
Reputation: 15
I thought about that and will dig into this idea
What seems strange to me is that • sometime i get the [Errno 104] Connection reset by peer • and sometime I dont ... I mean it works
example :
first try --> [Errno 104] Connection reset by peer
second try -- > OK
third try -- > OK
4th try --> [Errno 104] Connection reset by peer
5th try -- > OK
and so on !
Upvotes: 0
Reputation: 249
You are getting blocked by your mail provider because smtplib doesn't have any concrete super secure certificate which is necessary to send email via gmail or other mail engines. The problem is not on your code or the port...
You can do the following:
And you should be good to go!
Upvotes: 0