1

I'm trying to send an email via SMTP also using pythons email module. Since I want to send a file I'm using the MIME module to get this working. Unfortunately there is some problem importing these email.mime modules which I can not get fixed.

#Imports
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText  
from email.mime.base import MIMEBase
from email import encoders
import smtplib
import datetime

mail = 'email@address.net'

#E-Mail Content
msg = MIMEMultipart()
msg['From'] = mail
msg['To'] = mail
msg['Subject'] = 'MesseMahlzeiten Backup Nr.{}'.format('1')

body = datetime.datetime.strftime('%Y-&m-%d %H:%M')
msg.attach(MIMEText(body, 'plain'))

filename = 'WinIcon.jpg'
attachment = open(filenmae, 'rb')
p = MIMEBase('application', 'octet-stream')
p.set_payload(attachment.read())
encoders.encode_base64(p)
p.add_header('Content-Disposition', "attachment; filename= 
{}".format(filename))
msg.attach(p)

content = msg.as_string()

#E-Mail via SMTP
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttsl()
server.login(mail, 'password')
server.sendmail(mail, mail, content)
server.quit()

I'm getting following Error Message:

Traceback (most recent call last):
  File "D:/Python/101testprojects/email/email.py", line 2, in <module>
    from email.mime.multipart import MIMEMultipart
  File "D:\Python\101testprojects\email\email.py", line 2, in <module>
    from email.mime.multipart import MIMEMultipart
ModuleNotFoundError: No module named 'email.mime'; 'email' is not a 
package

How can I get this import work?

FinnK
  • 210
  • 4
  • 10
  • 1
    Did you try `pip install email` or `easy_install email` – wcarhart Aug 30 '19 at 18:24
  • 1
    https://stackoverflow.com/questions/33313858/importerror-no-module-named-email-mime-email-is-not-a-package – wcarhart Aug 30 '19 at 18:24
  • 1
    Thanks, problem was that the file was called email.py which caused this problem. – FinnK Aug 30 '19 at 18:51
  • 1
    Cool, formalized my comments into an answer for you. If you see value in my answer, you can accept it by clicking the check mark next to it. – wcarhart Aug 30 '19 at 18:54

1 Answers1

1

Change the name of the file from email.py to something else. With the original name, Python tries to import from email.py, rather than the email module.

Further reading: ImportError: No module named 'email.mime'; email is not a package

wcarhart
  • 2,685
  • 1
  • 23
  • 44