得到错误`没有名为mime.text`的模块[Python]

时间:2016-11-05 02:59:05

标签: python python-2.7 email sendmail

此代码是"假设"发送电子邮件。

import smtplib
#SERVER = "localhost"

FROM = 'myEmail@email.com'

TO = ["toEmail@email.com"] # must be a list

SUBJECT = "Hello!"

TEXT = "This message was sent with Python's smtplib."

# Prepare actual message

message = """\
From: %s
To: %s
Subject: %s

%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)

# Send the mail

server = smtplib.SMTP('myserver')
server.sendmail(FROM, TO, message)
server.quit()

我收到此错误消息。

Traceback (most recent call last):
  File "C:\Users\myCpu\Documents\myFiles\python\test wy.py", line 1, in <module>
    import smtplib
  File "C:\Python27\lib\smtplib.py", line 46, in <module>
    import email.utils
  File "C:/Users/myCpu/Documents/myFiles/python\email.py", line 5, in <module>
ImportError: No module named mime.text

使用:

Python 2.7

Windows 7 Professionnal

Gmail(@ gmail.com)

有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:6)

这就是我为gmail所做的。 希望这能解决你的问题

 from email.mime.text import MIMEText

    def construct_mesage():
      message = MIMEText(message_text)
      message['to'] = to
      message['from'] = sender
      message['subject'] = subject
      return {'raw': base64.urlsafe_b64encode(message.as_string())}

答案 1 :(得分:6)

将您正在处理名为email.py的文件重命名为其他内容。它可能会破坏您图书馆的进口。