I am trying to send a messsage with SMTP Lib with Python but as soon as I put my code in a function, what I get is empty. But without the function, the code works. I need to put the code in a method because I will use it in another class.
import smtplib
def notify():
server = smtplib.SMTP()
server.connect('smtp.toto.fr')
fromaddr = 'TOTO <moi@toto.fr>'
toaddrs = ['lui@toto.fr', 'elle@toto.fr']
sujet = "Un Mail avec Python"
message = u"""\
Velit morbi ultrices magna integer.
Metus netus nascetur amet cum viverra ve cum.
Curae fusce condimentum interdum felis sit risus.
Proin class condimentum praesent hendrer
it donec odio facilisi sit.
Etiam massa tempus scelerisque curae habitasse vestibulum arcu metus iaculis hac.
"""
msg = """\
From: %s\r\n\
To: %s\r\n\
Subject: %s\r\n\
\r\n\
%s
""" % (fromaddr, ", ".join(toaddrs), sujet, message)
try:
server.sendmail(fromaddr, toaddrs, msg)
except smtplib.SMTPException as e:
print(e)
server.quit()
notify()