Search code examples
flasksmtplib

An email sent using Python "smtplib" module arrives empty


Problem: When I send an email it does get sent from email1@hotmail.com to email2@hotmail.com but the email is empty. However, when I send the email using Gmail the email arrives with the content.

@app.route('/')
        def email():
            msg= 'Hello'
            server = smtplib.SMTP("smtp-mail.outlook.com", 587)
            server.starttls()
            server.login("email1@hotmail.com", "Password1")
            server.sendmail("email1@hotmail.com", "email2@hotmail.com", msg)
            return 'Message Sent!'

Desirable result: I want to see the content when I open the email in email2@hotmail.com


Solution

  • I can't test it, but it might be necessary to add

    "From: email1@hotmail.com\r\nTo: email2@hotmail.com\r\n\r\n"
    

    in the beginning of msg.

    So I would try with

    msg = 'From: email1@hotmail.com\r\nTo: email2@hotmail.com\r\n\r\nHello'
    

    and see if that works better.

    Gmail might be more forgiving over such things than Hotmail.

    Note also the blank line (\r\n\r\n) before the "Hello".