您的位置:首页 > 编程语言 > Python开发

使用python发送邮件,无中文乱码。

2017-08-02 14:18 387 查看
发件邮箱要先开启stmp 邮箱设置里面

# -*- coding: utf-8 -*-
import smtplib
from email.mime.text import MIMEText

def send_mail(mailto, subject, body):
# 发件人信息 这里使用sohu邮箱发送
host, user, password = 'smtp.sohu.com', '发送邮箱', '密码'
msg = MIMEText(body, 'plain', 'utf-8')
if not isinstance(subject, unicode):
subject = unicode(subject, 'utf-8')
msg['Subject'] = subject
msg['From'] = user
msg['To'] = mailto
msg["Accept-Language"] = "zh-CN"
msg["Accept-Charset"] = "ISO-8859-1,utf-8"
try:
s = smtplib.SMTP()
s.connect(host)
s.login(user, password)
s.sendmail(user, mailto, msg.as_string())
s.close()
return True
except Exception, e:
print e
return False

if '__name__ == __main__':
print send_mail('接收邮箱', '邮件标题', '邮件内容')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Python 邮件 乱码 smtp