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>  <font size='4' face='微软雅黑'>Sheet: "+img_dir[-15:-4]+" 内容 ,如下为图片链接,请确认:</font><br>  "
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)
python发送邮件
最新推荐文章于 2024-11-08 13:43:44 发布