Python3 - EC2 - smtplib - OVH : error “[Errno 104] Connection reset by peer”

Utilisation de smtplib en python3 : Je suis confronté à un problème en l'utilisant sur une machine ec2 de AWS

J'utilise la fonction suivante :

def ovh_send_email(sender_name, sender_address, recipient_addresses, subject, body_text, body_html, attachments = ):

mail_username = u'xxx@xxx.xx'
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()

Voici ce que j'obtiens

Exception Type: ConnectionResetError at /contact-email
Exception Value: [[Errno 104] Connection reset by peer

J'ai également essayé avec le port 587 d'envoyer du courrier sans ssl mais le problème reste

Ce qui est étrange, c'est que cela ne se produit pas sur un autre EC2 lorsque je lance la fonction ovh_send_email directement depuis le terminal

Je l'ai de temps en temps :


ovh_send_email(sender_name, sender_address, recipient_addresses, subject, body_text, body_html)
Traceback (most recent call last):
File "", line 1, in
File "", 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

Ce qui me semble étrange, c'est que - parfois, j'obtiens donc l'erreur ci dessus ([Errno 104] Connection reset by peer) - et parfois, je ne l'ai pas … ça marche quoi

exemple :

premier essai –> [Errno 104] Connection reset by peer
deuxième essai – > OK
troisième tentative – > OK
4ème essai –> [Errno 104] Connection reset by peer
5ème essai – > OK

et ainsi de suite !
Quelqu'un aurait il une idée ?