发送邮件

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from com.myconf import conf #获取配置文件中的信息


def send_email(file_path,file_name):

    # 第一步连接到smtp服务器
    smtp=smtplib.SMTP_SSL("smtp.qq.com",465)
    smtp.login(conf.get('email','sender'),conf.get('email','sender_pwd'))

    # 第二步构建邮件
    smg=MIMEMultipart()

    text_smg = MIMEText(open(file_path, 'r', encoding='utf8').read(), "html") # 邮件类型:html格式,plain是文本
    smg.attach(text_smg)

    file_msg = MIMEApplication(open(file_path, "rb").read())
    file_msg.add_header('content-disposition', 'attachment', filename=file_name)
    smg.attach(file_msg)

    smg["Subject"] = conf.get('email','subject')
    smg["From"] = conf.get('email','sender')
    smg["To"] = conf.get('email','receiver')


    # 第三步发送邮件
    smtp.send_message(smg,from_addr=conf.get('email','sender'),to_addrs=conf.get('email','receiver'))



if __name__ == '__main__':
    pass
原文地址:https://www.cnblogs.com/kite123/p/13785505.html