Python错误email.mime没有属性'MIMEMultipart'

时间:2018-04-16 13:49:41

标签: python python-3.x email raspberry-pi raspberry-pi3

我正在编写一个项目,使用Raspberry Pi 3通过Gmail发送电子邮件。该项目在我的笔记本电脑上运行良好,但是当我尝试在Raspberry Pi上运行代码时,以下错误不断报告:

email.mime has no attribute 'MIMEMultipart'

我尝试使用pip-install重新安装电子邮件包,如其他地方所建议的那样,但它没有解决问题。我特别感到困惑,因为这目前在我的笔记本电脑上有效,但不适用于Raspberry Pi 3.

我的代码如下:

import smtplib
import mimetypes
import email
import email.mime.application

FROM = "Sender's address" #This has been removed from this post, but normally contains sender's address
TO = "recipient's address" #This has been removed from this post, but normally contains recipient's address

msg = email.mime.Multipart.MIMEMultipart()
msg['Subject'] = 'Greetings'
msg['From'] = FROM
msg['To'] = TO


body = email.mime.Text.MIMEText("""hi""")
msg.attach(body)


filename= "path+filename" #e.g.(C:/Users/Pictures/pic.jpg)
fp=open(filename,'rb')
att = email.mime.application.MIMEApplication(fp.read(),_subtype="jpg")
fp.close()
att.add_header('Content-Disposition','attachment',filename=filename)
msg.attach(att)

s = smtplib.SMTP('smtp.gmail.com',587)
s.starttls()
s.login('FROM','PASSWORD') #password is actually entered here in my real code, it has been removed from this post
s.sendmail('FROM',['TO'], 
msg.as_string())
s.quit()

提前感谢您提供任何指导。

2 个答案:

答案 0 :(得分:1)

我终于自己弄明白了。在我的代码顶部添加import语句修复了问题:

from email.mime.multipart import MIMEMultipart

答案 1 :(得分:0)

这可能是安装问题。 检查python安装文件夹中是否有Lib / email / mime文件夹中的文件multipart.py。 希望这有帮助!