我試圖通過電子郵件發送附件的蟒蛇,但我得到這個錯誤:Python:試圖通過電子郵件發送附件〜字符串錯誤?
msg.attach(msgImage) AttributeError的:「海峽」對象有沒有屬性「附加」
下面是代碼:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.mime.image import MIMEImage
def send_email():
fromaddr = '[email protected]'
toaddrs = 'Toemail'
global msg
subject = 'RESPOSTA'
message = 'Subject: %s\n\n%s' % (subject, msg)
username = '[email protected]'
password = 'xxxxxxxx'
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
fp = open ('C:\Python27\Scripts\pares.txt', 'rb')
msgImage = MIMEImage (fp.read(), _subtype='txt')
fp.close()
msg.attach(msgImage)
server.sendmail(fromaddr, toaddrs, message, msg.as_string())
server.quit()
msg = 'Email test, please, see the attachments'
send_email()
任何人有什麼問題的提示?
提示:msg的類型是什麼? – jwodder
'msg'是一個字符串,但我仍然不明白問題,當腳本讀取txt文件時,是否無法將內容「附加」爲字符串? – Milteven