# -*- coding: utf-8 -*-
"""
Created on Thu Apr 2 09:40:33 2020
@author: 1010
"""
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.mime.application import MIMEApplication
import os
#设置邮箱登陆信息
username = ''
password = ''
#发件人和收件人信息
sender = '@com.cn'
receiver = [''] #收件人,多人逗号分开
cc = [''] #抄送人,多人逗号分开
##邮件内容编辑
##邮件主题
subject = 'Process_Monitor'
###########email内容,html格式###############
msg = MIMEMultipart()
msg.add_header('From',username)
msg.add_header('To',','.join(receiver))
msg.add_header('Cc',','.join(cc))
msg.add_header('Subject',subject)
mail_content = '''<html>
<!-- 网页的标题、图标... -->
<head>
<mate charset="utf-8">
<title>Process_Monitor</title>
</head>
<!-- 网页的具体内容 -->
<body>
各位领导同事,大家好!以下为
<script>
Date.prototype.format = function (fmt) {
var o = {
"y+": this.getFullYear, //年
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds() //秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
setInterval("document.getElementById('dateTime').innerHTML = (new Date()).format('yyyy-MM-dd');", 1000);
</script>
<div id="dateTime"></div>Monitor情况:
<h2>1.预测</h2>
<table border="2">
<tr><th>Max</th>
<th coslanb="5"> <img src="cid:image1" alt="image1" width="600" height="300" /><img src="cid:image2" alt="image2" width="500" height="300" /></th></tr>
<tr><th>Ti</th>
<th coslanb="5"> <img src="cid:image3" alt="image3" width="600" height="300" /><img src="cid:image4" alt="image4" width="500" height="300" /></th></tr>
<tr><th>19</th>
<th coslanb="5"> <img src="cid:image5" alt="image5" width="600" height="300" /><img src="cid:image6" alt="image6" width="500" height="300" /></th></tr>
<tr><th>Go</th>
<th coslanb="5"> <img src="cid:image7" alt="image7" width="600" height="300" /><img src="cid:image8" alt="image8" width="500" height="300" /></th></tr>
<tr><th>Abnormal</th>
<th coslanb="5"><img src="cid:image9" alt="image9" width="300" height="400" /> <img src="cid:image10" alt="image10" width="800" height="400" /></th></tr>
<tr><th>Ab</th>
<th coslanb="5"><img src="cid:image11" alt="image11" width="300" height="400" /> <img src="cid:image12" alt="image12" width="800" height="400" /></th></tr>
</table>
<p>附件为Glass预测详细信息</p>
<p>以上,谢谢!</p>
<p>来自automation_service</p>
具体情况请查询:
<a href="http://10.141.70.248:81/spotfire/wp/analysis?file=/CONTENT&waid=FK-qCS7Q-USPDKfqATbZo-27194274c2oHkx&wavid=0" target="_blank">Spotfire_Web</a>
</body>
</html>'''
msg.attach(MIMEText(mail_content,'html','utf-8'))
##########################################添加图片############################################
Files = []
i = 1
for filepath in Files:
f = open(filepath,'rb')
msgImage = MIMEImage(f.read())
f.close()
msgImage.add_header('Content-ID','<image%i>'%i)
msg.attach(msgImage)
i = i +1
'''f = open('C:/Users/Ab.png','rb')
msgImage = MIMEImage(f.read())
f.close()
msgImage.add_header('Content-ID','image10')
msg.attach(msgImage) '''
##########################################添加附件############################################
Files_1 = ['C:/Users/Abnormal.txt']
for filepath in Files_1:
baseName = os.path.basename(filepath)
att = MIMEApplication(open(filepath,'rb').read())
att.add_header('Content-Disposition', 'attachment', filename=baseName)
msg.attach(att)
print(filepath, 'added')
#############
##所有收信人以及抄送和暗抄对象都一样,
##放在server.sendmail(sender, toaddrs, msg.as_string()) 第二个参数toaddrs里面
##具体区别收信人以及抄送和暗抄对象,
##都由server.sendmail(sender, toaddrs, msg.as_string()) 第三个参数里面msg里面的关键词决定
#############
##所有收信人
to_addrs = receiver + cc
#############发送####################################
mail_server = 'smtp.xxx.com.cn'
mail_server_port = 25
try:
server = smtplib.SMTP(mail_server, mail_server_port)
# server.set_debuglevel(1) # 调试模式
server.ehlo()
server.starttls()
server.login(username, password)
server.sendmail(sender, to_addrs, msg.as_string())
server.quit()
print('邮件发送成功')
except smtplib.SMTPException:
print('Error: 无法发送邮件')
后记:
这过程中遇到的一个非常奇怪的问题,就是公司的邮箱软件图片超过9张就显示不出来了,但是网页版的显示就没有问题。
- 研究了好久,开始猜想是图片大小的原因,所以改了小的图片格式,发现还是不行
- 想到可能是软件的问题,但是软件问题感觉不好弄啊。
- 后来无意之间看到公司的软件在超过9张图片后编码就出现了问题
例如:Attachment11.png 变成了Attachment1.png1
file:///AppData/Roaming/DM%20Pro6/94C66C94C0054D52A7B5D6B32FA9B24A/7NPQJqopoW/Attachment1.png1
先做了测试,手动粘了15张图片,发现没问题,再去看url发现手动方式命名是图片本身的名字!
那这样可能是用图片的命名用数字方式不行了,那我换一种呗:
for filepath in Files:
baseName = os.path.basename(filepath)
f = open(filepath,'rb')
msgImage = MIMEImage(f.read())
f.close()
msgImage.add_header('Content-ID','%s'%baseName)
msg.attach(msgImage)
然后在测试,终于。需要注意的是html里面<img src=“cid:image11” alt=“image11” 地方image11需要改成读取的图片名字。