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?
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