python发送邮件

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


# 添加正文图片
def addIMG(msg,src_org, imgname):
    img = open(src_org, 'rb')
    txtImage = MIMEText(img.read(), "base64" , "utf-8" )
    img.close()
    txtImage.add_header( "Content-Disposition" , "attachment" , filename = imgname)
    msg.attach(txtImage)
    img1 = open(src_org, 'rb')
    msgImage = MIMEImage(img1.read())
    img1.close()
    msgImage.add_header('Content-ID', imgname)  # 指定图片文件的Content-ID,<img>标签src用到
    msg.attach(msgImage)
    
    
def sendMail(img_dir,file_name,num,result_dir):
    try:
        smtpserver = "" #邮箱服务器IP
        sender = "" #发送者
        receiver = [] #接收者,可以有多个
        #ccer = [] # 抄送
        bser = []
        subject = "邮件主题"
        msg = MIMEMultipart('related')
        msg[ 'From' ] = sender
        msg[ 'To' ] = ";".join( receiver )
        msg[ "Cc" ] = ";".join( ccer )
        msg[ "Subject" ] = Header(subject, "utf-8" )
        path = "" 
        # 添加正文
        text="<font size='4' face='微软雅黑'>Dear Sirs:</font><br>&nbsp&nbsp<font size='4' face='微软雅黑'>Sheet: "+img_dir[-15:-4]+" 内容 ,如下为图片链接,请确认:</font><br>&nbsp&nbsp"
        msg.attach(MIMEText(text,"html" , "utf-8"))
        #addIMG(msg,os.path.join(temp_dir,'result_'+filename),filename)
        print('send em')
        smtp = smtplib.SMTP()
        smtp.connect(smtpserver, 25 )
        if len(receiver)>0:
            print('send em2')
            smtp.sendmail(sender, receiver, msg.as_string())
        if len(ccer)>0:
            smtp.sendmail(sender, ccer, msg.as_string())
        if len(bser)>0:
            smtp.sendmail(sender, bser, msg.as_string())
        smtp.quit()
    except Exception as e:
        LOG.logger.error(e.args)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值