发送邮件

import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication 
 
if __name__ == "__main__":
        fromaddr = "179xxx@qq.com"
        password = "xxxxxx"
        toaddrs = ["179xxx@qq.com", "925xxx@qq.com"]

         #构建文字内容
        content = "hello, this is email content."
        textApart = MIMEText(content,"plain","utf-8")

         #读取本地图片
        imageFile = "D:计算机二级dog.jpg"
        imageApart = MIMEImage(open(imageFile, "rb").read(), imageFile.split(".")[-1])
        imageApart.add_header("Content-Disposition", "attachment", filename=imageFile)

         #读取本地文档
        pdfFile = "D:计算机二级chicago.csv"
        pdfApart = MIMEApplication(open(pdfFile, "rb").read())
        pdfApart.add_header("Content-Disposition", "attachment", filename=pdfFile)
 
        m = MIMEMultipart()
        m.attach(textApart)
        m.attach(imageApart)
        m.attach(pdfApart)
        m["Subject"] = "title"

         #发送邮件
        try:
            server = smtplib.SMTP("smtp.qq.com",25)
            server.login(fromaddr,password)
            server.sendmail(fromaddr, toaddrs, m.as_string())
            print("success")
            server.quit()
        except smtplib.SMTPException as e:
            print("error:",e) #打印错误
hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » 发送邮件