Envoi d'email avec Python
Ce script fonctionnant sous Python3 utilise des paramêtres de configuration SMTP d'ovh pour envoyer un message par email.
import smtplib
from email.message import EmailMessage
_username = '**@xpres.fr'
_password = '*****'
_host = 'ssl0.ovh.net'
_port = 587
_subject = f'Text : {textfile}'
_from = _username
_reply_to = "*****@****"
_to = "*****@****"
_content = "Contenu du message"
print('Initiating email sending...')
print( f'From : {_from}\n'
f'To : {_to}\n'
f'Subject : {_subject}\n'
f'Host : {_host}\n'
f'Port : {_port}\n'
f'Username : {_username}\n'
f'Reply-To : {_reply_to}\n' )
msg = EmailMessage()
msg.set_content(_content)
msg['Subject'] = _subject
msg['From'] = _from
msg['Reply-To'] = _reply_to
msg['To'] = _to
try:
# Send the message via our own SMTP server.
s = smtplib.SMTP(_host, _port)
s.login(_username, _password)
s.send_message(msg)
s.quit()
print("Successfully sent email")
except Exception as e:
print(e)