0

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()
Omar Aflak
  • 2,918
  • 21
  • 39
MLD
  • 1
  • Is your code indented like it is now after my edits ? – Omar Aflak May 14 '21 at 15:18
  • The duplicate is different in the details, but the fundamental problem is the same: you cannot pass in random text gunk instead of a valid RFC822 email message. – tripleee May 14 '21 at 15:52

0 Answers0