Python发邮件, 附件路径有中文提示错误...

hello_2012222 2012-01-29 10:42:45
#!/usr/bin/env python
#-*- encoding: utf-8 -*-
import smtplib,mimetypes
from email.mime.multipart import MIMEMultipart #多个MIME对象的集合
from email.mime.text import MIMEText #MIME文本对象
from email.mime.image import MIMEImage #MIME二进制文件对象
from email import utils
from email import encoders
from email.header import Header
import os.path
import datetime
import time

sender = '123456@163.com' #发件人邮箱
smtpserver = 'smtp.163.com' #邮件服务器
username = '123456' #发件人邮箱用户名
password = '******' #发件人邮箱密码
mailHead = "邮件标题"
mailBody = "邮件正文"

#收件人列表
tolist = ('123456@qq.com', '123456@163.com')

msgRoot = MIMEMultipart()
attText = MIMEText(mailBody, 'plain','utf-8')
msgRoot.attach(attText) #添加邮件正文

msgRoot['from'] = sender #发件人
msgRoot['Subject'] = Header(mailHead, 'utf-8') #邮件标题
msgRoot['Date'] = time.ctime(time.time()) #设置时间

#构造附件(附件路径出现中文,会提示编码错误!!!!!!)
rarFilePath = "测试附件.rar"
att = MIMEText(open(rarFilePath, 'rb').read(), 'base64', 'gb2312')
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment;filename= %s' % Header(rarFilePath, 'gb2312')
msgRoot.attach(att)

#群发邮件
smtp = smtplib.SMTP(smtpserver, 25)
smtp.login(username, password)
msgRoot['to'] = 'friend@163.com'
smtp.sendmail(sender, tolist, msgRoot.as_string())
time.sleep(5) #休眠5秒
smtp.quit() #断开与服务器的连接
...全文
409 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
test2223344 2012-02-07
  • 打赏
  • 举报
回复
我也对这个问题感兴趣.
angel_su 2012-02-07
  • 打赏
  • 举报
回复
附件嘛,以前学过抄下的代码类似下面,你试试...
...
from email import Encoders
...
filename = 'jdk15.tar' #附件名
fp = open(filename,'rb')
ctype, encoding = mimetypes.guess_type(filename)
if ctype is None or encoding is not None:
ctype = 'application/octet-stream'
maintype, subtype = ctype.split('/',1)
m = MIMEBase(maintype, subtype)
m.set_payload(fp.read())
fp.close()
Encoders.encode_base64(m) #把附件编码
m.add_header('Content-disposition','attachment',filename=filename) #修改邮件头
msg.attach(m)
...
javacode007 2012-02-05
  • 打赏
  • 举报
回复
mark下,这个问题确实不知如何解决,楼上的几种方式都不行,期待高人解答啊
JacksonLv 2012-01-30
  • 打赏
  • 举报
回复
我认为你应该先把带有中文的路径编码成gbk或gb2312再交给smtp.sendmail去处理。
hello_2012222 2012-01-30
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 jacksonlv 的回复:]
我认为你应该先把带有中文的路径编码成gbk或gb2312再交给smtp.sendmail去处理。
[/Quote]
===========
rarFilePath = u"测试.rar"
我这边一运行就提示语法错误
hello_2012222 2012-01-30
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 iambic 的回复:]
试下Header(rarFilePath.encode('gbk'), 'gb2312')
[/Quote]
=============
刚刚试了下,这样还不可以...
hello_2012222 2012-01-30
  • 打赏
  • 举报
回复
rarFilePath = u"测试.rar"

我这边一运行就提示语法错误
iambic 2012-01-30
  • 打赏
  • 举报
回复
试下Header(rarFilePath.encode('gbk'), 'gb2312')
hello_2012222 2012-01-29
  • 打赏
  • 举报
回复
#构造附件(附件路径出现中文,会提示编码错误!!!!!!)
rarFilePath = "测试附件.rar"
att = MIMEText(open(rarFilePath, 'rb').read(), 'base64', 'gb2312')
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment;filename= %s' % Header(rarFilePath, 'gb2312')
msgRoot.attach(att)

#============================

如果邮件标题,或正文,或附件名字, 出现中文,
就会提示类似下面的错误:
Traceback (most recent call last):
File "D:\test.py", line 54, in <module>
smtp.sendmail(sender, tolist, msgRoot.as_string())
File "C:\Python32\lib\smtplib.py", line 733, in sendmail
msg = _fix_eols(msg).encode('ascii')
UnicodeEncodeError: 'ascii' codec can't encode characters in position 292-293: ordinal not in range(128)

37,741

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧