0

I tried to send email using smtplib using smtplib in python. I created a object of MIMEMultipart and tried to send email but email is send with subject,to,from but there is no text sent with it. I tried following code

import smtplib as smtp
from email.mime.multipart import MIMEMultipart
msg = MIMEMultipart()
connection = smtp.SMTP_SSL('smtp.gmail.com', 465)
email_addr = 'example@gmail.com'
email_passwd = 'GoogleAppPassword'
connection.login(email_addr, email_passwd)
msg['Subject'] = "IMPORTANT"
msg['From'] = "example@gmail.com"
msg['To'] = "person@gmail.com"
msg['text'] = "hi"
connection.sendmail(email_addr,"person@gmail.com", msg.as_string())
connection.close()

Can you spot where i went wrong.

  • `msg["text"]` adds an invalid nonstandard header, not actual body text. Multipart is the wrong choice for a message with only one part - or, as here, none at all. – tripleee Nov 20 '22 at 09:46
  • 1
    As an aside, it looks like your `email` code was written for an older Python version. The `email` module in the standard library was overhauled in Python 3.6 to be more logical, versatile, and succinct; new code should target the (no longer very) new `EmailMessage` API. Probably throw away this code and start over with modern code from [the Python `email` examples documentation.](https://docs.python.org/3/library/email.examples.html) – tripleee Nov 20 '22 at 09:46

0 Answers0