我尝试使用Python 3.3发送基本电子邮件。我在这里跟随第一段代码:
https://docs.python.org/3.3/library/email-examples.html
我的代码如下:
def emailCurrentRankings(recipientEmail):
fp = open('rankings.txt', 'rb')
msg = MIMEText(fp.read())
fp.close()
sender = 'bclayman@gmail.com'
msg['Subject'] = 'CSA Rankings'
msg['From'] = sender
msg['To'] = recipientEmail
s = smtplib.SMTP('localhost')
s.sendmail(sender, [recipientEmail], msg.as_string())
s.quit()
我的main函数调用此方法如下:
emailCurrentRankings('bclayman@gmail.com')
我能说的唯一区别是我使用' rankings.txt'而不是第二行上的文本文件。我已尝试过两者并收到相同的错误消息:
Traceback (most recent call last):
File "helpfulFunctions.py", line 128, in <module>
main()
File "helpfulFunctions.py", line 120, in main
emailCurrentRankings('bclayman@gmail.com')
File "helpfulFunctions.py", line 106, in emailCurrentRankings
msg = MIMEText(fp.read())
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/email/mime/text.py", line 34, in __init__
_text.encode('us-ascii')
AttributeError: 'bytes' object has no attribute 'encode'
当我用Google搜索时,看起来必须进行一些身份验证(因为我能够从给定的电子邮件发送)。但是我对代码进行建模的最基本的例子并没有提到这一点......
我误入歧途的任何想法?
谢谢, bclayman
答案 0 :(得分:2)
尝试在不使用二进制格式的情况下打开文件。
也许像 -
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" width="0" height="0">
<filter id="grayscale">
<feColorMatrix type="matrix" values="0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0 0 0 1 0"/>
</filter>
</svg>