为了方面使用直接复制出来了,可以直接使用
# coding = utf-8
import requests
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
import time
from jinja2 import Environment, FileSystemLoader
import shutil,os
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.header import Header
from email.utils import parseaddr, formataddr
import hashlib
import sys
import codecs
#sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detac)
requests.packages.urllib3.disable_warnings()
def send_email():
#设置发件人和收件人邮箱
msg_from = '发送人邮箱'
passwd = '发送人邮箱授权码'
msg_to = ['收件邮箱','收件邮箱1']
msg_to_cc=['抄送邮箱','抄送邮箱2']
#设置邮件信息
message = MIMEMultipart('related')
subject = '标题test'
message['Subject'] = Header(subject, 'utf-8')
message['Date'] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
#添加文字(也可以是html格式)这里直接读取当前路径下的result.html文件
with open('result.html', 'rb') as file:
mail_msg=file.read()
message.attach(MIMEText(mail_msg, 'html', 'utf-8'))
#添加图片,这里直接读取当前路径下的picture文件夹中的图片
files = os.listdir('./picture')
files.sort(key=lambda x: int(x.split('.')[0]))
for f in files:
with open('./picture/{}'.format(f), 'rb') as file:
msgImage = MIMEImage(file.read())
msgImage.add_header('Conteng-ID', f)
message.attach(msgImage)
# 自定义发件人和收件人信息(可以在收到邮件的时候看到发件人和收件人信息)
def _format_addr(s):
addr = parseaddr(s)
return formataddr(addr)
#自定义发件人名称
message['From'] = _format_addr(u'殊二 <%s>' % msg_from)
#将收件和抄送邮箱分别加上
for to_addr in msg_to:
message['to'] = _format_addr(to_addr)
for cc in msg_to_cc:
message['Cc'] = _format_addr(cc)
#发送邮件
try:
smtpObj = smtplib.SMTP_SSL("smtp.qq.com", 465)
smtpObj.login(msg_from, passwd)
smtpObj.sendmail(msg_from, msg_to+msg_to_cc, message.as_string())
print("邮件发送成功")
smtpObj.quit()
except Exception as msg:
print("Error: 无法发送邮件",msg)
if __name__ == "__main__":
send_email()