0
点赞
收藏
分享

微信扫一扫

python发送带附件的163邮件

M4Y 2022-02-22 阅读 76
python

代码如下:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText


class Mail_163():
    def __init__(self,data):
        self.mailserver = "smtp.163.com"
        self.mailPort = 25
        self.username = "123456789@163.com"  # 发送邮件的163账号
        self.passwd = "******"  # 163邮箱授权码
        self.to_mail = "1231541521@qq.com"   # 接收邮件的邮箱
        self.data_info = data['data_info']  # 邮件的主题内容
        self.data_title = data['data_title']  # 邮件的标题

    def mail_send(self):
        # to_mail = "vat_registration@163.com"
        smtpServer = smtplib.SMTP(self.mailserver,self.mailPort)
        smtpServer.login(self.username,self.passwd)
        # 设置主题内容
        msg = MIMEMultipart()
        msg.attach(MIMEText(self.data_info, 'plain', 'utf-8'))
        msg["Subject"] = self.data_title
        msg["From"] = self.username
        msg["To"] = self.to_mail
        filename = 'a.txt'
        att1 = MIMEText(open(filename, 'rb').read(), 'base64', 'utf-8')
        att1["Content-Type"] = 'application/octet-stream'
        att1["Content-Disposition"] = 'attachment; filename="' + filename + '"'
        msg.attach(att1)
        smtpServer.sendmail(self.username,self.to_mail,msg.as_string())
        smtpServer.close()
举报

相关推荐

0 条评论