Chinaunix
标题:
邮件 标题如果是中文就是乱码该怎么改?
[打印本页]
作者:
zl624867243
时间:
2015-11-04 00:57
标题:
邮件 标题如果是中文就是乱码该怎么改?
邮件 标题如果是中文就是乱码,以下脚本该怎么改?
#!/usr/bin/python26
#coding:utf-8
import smtplib
from email.mime.text import MIMEText
import sys
mail_host = 'smtp.189.cn'
mail_user = 'tom_a'
mail_pass = '123456'
mail_postfix = '189.cn'
def send_mail(to_list,subject,content):
me = mail_user+"<"+mail_user+"@"+mail_postfix+">"
msg = MIMEText(content,_subtype='plain',_charset='utf-8')
msg['Subject'] = subject
msg['From'] = me
msg['to'] = to_list
try:
s = smtplib.SMTP()
s.connect(mail_host)
s.login(mail_user,mail_pass)
s.sendmail(me,to_list,msg.as_string())
s.close()
return True
except Exception,e:
print str(e)
return False
if __name__ == "__main__":
send_mail(sys.argv[1], sys.argv[2], sys.argv[3])
复制代码
作者:
h1x2y3awalm
时间:
2015-11-04 19:34
本帖最后由 h1x2y3awalm 于 2015-11-04 19:35 编辑
我之前做的一个问题是在邮件客户端,不知道你用web还是foxmail之类的。调整一下客户端的编码试试看
作者:
zl624867243
时间:
2015-11-04 23:47
回复
2#
h1x2y3awalm
我用的是短信报警 邮件客户端是正常的。短信报警标题是乱码
作者:
haooooaaa
时间:
2015-11-05 09:35
本帖最后由 haooooaaa 于 2015-11-05 09:35 编辑
我用 gb2312 编码没乱码
MIMEText(open(i, 'rb').read(), 'base64', 'gb2312')
作者:
zl624867243
时间:
2015-11-05 12:45
回复
4#
haooooaaa
你能把完整脚本贴出来吗?
作者:
haooooaaa
时间:
2015-11-05 13:01
回复
5#
zl624867243
#!/usr/bin/env python
#_*_ coding:gbk _*_
import datetime
import sys
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
def send_mail(filename=[], picname=[], content_txt='', content_html=''):
smtpserver = 'smtp.163.com'
username = 'svn@163.com'
password = '123123'
msg = MIMEMultipart()
msg['Subject'] = '中文邮件'
msg['From'] = "svn@163.com"
msg['To'] = "test@163.com"
# attchment
if len(filename) > 0:
for i in filename:
att = MIMEText(open(i, 'rb').read(), 'base64', 'gb2312')
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment; filename="%s"' % i.split('\\')[-1]
msg.attach(att)
# attchment picture
if len(picname) > 0 and content_html != '':
for i in range(0,len(picname)):
#content_html = '<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image%s"><br>good!' % i
msg_content_html = MIMEText(content_html,'html','gb2312')
msg.attach(msg_content_html)
with open(picname[i], 'rb') as f:
msgImage = MIMEImage(f.read())
msgImage.add_header('Content-ID', '<image%s>' % (i + 1))
msg.attach(msgImage)
# content text
if content_txt != '':
msg_content_txt = MIMEText(content_txt,_subtype='plain',_charset='gb2312')
msg.attach(msg_content_txt)
# content html
if content_html != '' and len(picname) == 0:
msg_content_html = MIMEText(content_html,_subtype='html',_charset='gb2312')
msg.attach(msg_content_html)
smtp = smtplib.SMTP()
smtp.connect(smtpserver)
smtp.starttls()
smtp.login(username, password)
smtp.sendmail(msg['From'], msg['To'], msg.as_string())
smtp.quit()
send_mail(content_txt='这是中文测试邮件,仅仅是测试而已!')
复制代码
作者:
haooooaaa
时间:
2015-11-05 13:06
回复
5#
zl624867243
搞错了,你是短信报警啊? 这个没试过。。。
作者:
zl624867243
时间:
2015-11-05 13:45
回复
7#
haooooaaa
我的是邮件报警,然后189邮箱免费下发到我的手机短信上。我的是python2.6.6
怎么运行提示报错了
SyntaxError: 'gbk' codec can't decode bytes in position 24-25: illegal multi
作者:
haooooaaa
时间:
2015-11-05 13:56
回复
8#
zl624867243
别用这个了, 刚才我也手机测试了一下, 也是标题乱码。 内容正常。
win10 moblie 测试
作者:
zl624867243
时间:
2015-11-05 15:55
回复
9#
haooooaaa
好的感谢看来只能改zabbix标题的报警信息了
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2