Python电子邮件发件人

时间:2016-08-30 18:45:54

标签: python email

我正在创建一个帐户创建者。我希望我可以创建一个电子邮件发件人,以便人们在填写表格后收到电子邮件, 这是我尝试过的:

# Import smtplib for the actual sending function
import smtplib

# Import the email modules we'll need
from email.mime.text import MIMEText

# Open a plain text file for reading.  For this example, assume that
# the text file contains only ASCII characters.
fp = open('textfile.txt', 'rb')
# Create a text/plain message
msg = MIMEText(fp.read())
fp.close()

# me == the sender's email address
# you == the recipient's email address
msg['Subject'] = 'The contents of %s' % textfile
msg['From'] = me
msg['To'] = you

# Send the message via our own SMTP server, but don't include the
# envelope header.
s = smtplib.SMTP('localhost')
s.sendmail(me, [you], msg.as_string())
s.quit()

我尝试将文件重命名为main.py, 但我得到错误

Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 2218, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/macbook/Movies/GAME/*********/3D/email.py", line 2, in <module>
    import smtplib
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/smtplib.py", line 47, in    <module>
    import email.utils
  File "/Users/macbook/Movies/GAME/*********/3D/email.py", line 5, in <module>
    from email.mime.text import MIMEText
ImportError: No module named 'email.mime'; 'email' is not a package

当我将文件名更改为main.py时 我收到错误

Traceback (most recent call last):
  File "/Users/macbook/Movies/GAME/Wonderland/3D/main.py", line 11, in <module>
    msg = MIMEText(fp.read())
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/email/mime/text.py", line 33, in __init__
    _text.encode('us-ascii')
AttributeError: 'bytes' object has no attribute 'encode'

很多其他python电子邮件发件人问题,但没有一个问题/错误与我一样。

1 个答案:

答案 0 :(得分:1)

更改email.py文件的名称。它掩盖了您要使用的电子邮件包。同时删除email.pyc