Email Error help me with syntax error
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import smtplib from email import encoders from email.message import Message from email.mime.audio import MIMEAudio from email.mime.base import MIMEBase from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText msg = MIMEMultipart() msg.attach(MIMEText(file("P:/Email/test.txt").read())) sender = '[email protected]' reciever = '[email protected]' msg = 'Hello' # Credentials (if needed) username = 'user' password = 'pass' # The actual mail send server = smtplib.SMTP('localhost') server.starttls() server.login(username,password) server.sendmail(sender, reciever, msg) server.quit() |
回溯(最近一次调用最后一次):文件"attach2.py",第27行,在
server.sendmail(sender,reciever,msg)文件"C: Python33 lib smtplib.py",第775行,in
sendmail(code,resp)= self.data(msg)文件"C: Python33 lib smtplib.py",第516行,in
data q = _quote_periods(msg)文件"C: Python33 lib smtplib.py",第167行,in
quote_periods返回re.sub(br'(?m)^。',b'..',bindata)文件"C: Python33 lib re.py",
第170行,在子返回_compile(pattern,flags).sub(repl,string,count)TypeError:
期望字符串或缓冲区
为什么我看到此错误消息。 我的python库文件有问题吗?
前一行缺少右括号。
1 2 3 4 5 6 | ... msg = MIMEMultipart() msg.attach(MIMEText(file("P:/Email/test.txt").read())) # line missing a parenthesis sender = '[email protected]' ... |
您的代码有2个错误。 这是我的纠正
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import smtplib from email import encoders from email.message import Message from email.mime.audio import MIMEAudio from email.mime.base import MIMEBase from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText msg = MIMEMultipart() msg.attach(MIMEText(file("P:/Email/test.txt").read())) sender = '[email protected]' reciever = '[email protected]' # ***here don't overwrite the MIMEMultipart() obj*** # msg = 'Hello' # Credentials (if needed) username = 'user' password = 'pass' # The actual mail send server = smtplib.SMTP('localhost') server.starttls() server.login(username,password) # ***here msg.as_string() to convert the MIMEMultipart() obj to a string*** server.sendmail(sender, reciever, msg.as_string()) server.quit() |