找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 1932|回复: 0

[求助] python3 调用函数发邮件编码报错

0

主题

0

帖子

0

积分

贫民

积分
0
neilxiaoxing 发表于 2020-8-28 15:07:12 | 显示全部楼层 |阅读模式
1、sendmail.py



import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.header import Header

# 设置smtplib所需的参数
# 下面的发件人,收件人是用于邮件传输的。

def vul_mail_send(receiver_list, subject, text):
    smtpserver = '*********@************'
    username = '*********@************'
    password = '************'
    sender = '*********@************'
    receiver = receiver_list

    subject = subject
    # 通过Header对象编码的文本,包含utf-8编码信息和Base64编码信息。以下中文名测试ok
    # subject = '中文标题'
    # subject=Header(subject, 'utf-8').encode()

    # 构造邮件对象MIMEMultipart对象
    # 下面的主题,发件人,收件人,日期是显示在邮件页面上的。
    msg = MIMEMultipart('mixed')
    msg['Subject'] = subject
    msg['From'] = '*********@************'
    msg['To'] = ";".join(receiver)
    # msg['Date']='2012-3-16'

    # 构造文字内容
    text = text
    text = text.replace("/static/upload", "************/static/upload")

    text_plain = MIMEText(text, 'html', 'utf-8')
    msg.attach(text_plain)

    # 构造图片链接
    # sendimagefile = open(r'D:\pythontest\testimage.png', 'rb').read()
    # image = MIMEImage(sendimagefile)
    # image.add_header('Content-ID', '<image1>')
    # image["Content-Disposition"] = 'attachment; filename="testimage.png"'
    # msg.attach(image)

    # 构造html
    # 发送正文中的图片:由于包含未被许可的信息,网易邮箱定义为垃圾邮件,报554 DT:SPM <p><img src="cid:image1"></p>
    # html = """
    # <html>
    #   <head></head>
    #   <body>
    #     <p>Hi!<br>
    #        How are you?<br>
    #        Here is the <a href="http://www.baidu.com">link</a> you wanted.<br>
    #     </p>
    #   </body>
    # </html>
    # """
    # text_html = MIMEText(html, 'html', 'utf-8')
    # text_html["Content-Disposition"] = 'attachment; filename="texthtml.html"'
    # msg.attach(text_html)

    # 构造附件
    # sendfile = open(r'D:\pythontest\1111.txt', 'rb').read()
    # text_att = MIMEText(sendfile, 'base64', 'utf-8')
    # text_att["Content-Type"] = 'application/octet-stream'
    # # 以下附件可以重命名成aaa.txt
    # # text_att["Content-Disposition"] = 'attachment; filename="aaa.txt"'
    # # 另一种实现方式
    # text_att.add_header('Content-Disposition', 'attachment', filename='aaa.txt')
    # # 以下中文测试不ok
    # # text_att["Content-Disposition"] = u'attachment; filename="中文附件.txt"'.decode('utf-8')
    # msg.attach(text_att)

    # 发送邮件
    smtp = smtplib.SMTP()
    try:
        smtp.connect('*********@************')
        # 我们用set_debuglevel(1)就可以打印出和SMTP服务器交互的所有信息。
        # smtp.set_debuglevel(1)
        smtp.login(username, password)
        smtp.sendmail(sender, receiver, msg.as_string())
        smtp.quit()
    except Exception as e:
        print(e)



直接发送中文没有问题,但是其他文件import 这个函数发中文就报编码错误

'ascii' codec can't encode characters in position 408-412: ordinal not in range(128)



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表