废话不多数直接上代码
[Python] 纯文本查看 复制代码 from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email import encoders
from email.mime.base import MIMEBase
import smtplib
import os
import datetime
[Python] 纯文本查看 复制代码 '''将Excel作为附件发送邮件'''
def __init__(self, email_info):
self.email_info = email_info
# 使用SMTP_SSL连接端口为465
self.smtp = smtplib.SMTP_SSL(self.email_info['server'], self.email_info['port'])
# 创建两个变量
self._attachements = []
self._from = ''
def login(self):
# 通过邮箱名和smtp授权码登录到邮箱
self._from = self.email_info['user']
self.smtp.login(self.email_info['user'], self.email_info['password'])
print("{0} 邮箱登录完成".format(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
[Python] 纯文本查看 复制代码 def add_attachment(self):
# 添加附件内容
# 注意:添加附件内容是通过读取文件的方式加入
file_path = self.email_info['file_path']
# print(file_path[1])
for num in range(0, len(file_path)):
with open(file_path[num], 'rb') as file:
filename = os.path.split(file_path[num])[1]
mime = MIMEBase('application', 'octet-stream', filename=filename)
mime.add_header('Content-Disposition', 'attachment', filename=('gbk', '', filename))
mime.add_header('Content-ID', '<0>')
mime.add_header('X-Attachment-Id', '0')
mime.set_payload(file.read())
encoders.encode_base64(mime)
# 添加到列表,可以有多个附件内容
self._attachements.append(mime)
print("{0} 邮件内容编写完成".format(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
[Asm] 纯文本查看 复制代码 def sendMail(self):
# 发送邮件,可以实现群发
msg = MIMEMultipart()
contents = MIMEText(self.email_info['content'], 'plain', 'utf-8')
msg['From'] = self.email_info['user']
msg['To'] = self.email_info['to']
msg['Subject'] = self.email_info['subject']
if 'cc' in self.email_info and 'bc' in self.email_info:
msg['Cc'] = self.email_info['cc']
msg['Bcc'] = self.email_info['bc']
rto = self.email_info['to'].split(',') + self.email_info['cc'].split(',') + self.email_info['bc'].split(',')
for att in self._attachements:
# 从列表中提交附件,附件可以有多个
msg.attach(att)
msg.attach(contents)
try:
self.smtp.sendmail(self._from, rto, msg.as_string())
print('{0} 邮件发送成功,请注意查收'.format(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
except Exception as e:
print('Error:', e)
elif 'cc' in self.email_info and 'bc' not in self.email_info:
msg['Cc'] = self.email_info['cc']
rto = self.email_info['to'].split(',') + self.email_info['cc'].split(',')
for att in self._attachements:
# 从列表中提交附件,附件可以有多个
msg.attach(att)
msg.attach(contents)
try:
self.smtp.sendmail(self._from, rto, msg.as_string())
print('{0} 邮件发送成功,请注意查收'.format(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
except Exception as e:
print('Error:', e)
elif 'cc' not in self.email_info and 'bc' in self.email_info:
msg['Bcc'] = self.email_info['bc']
rto = self.email_info['to'].split(',') + self.email_info['bc'].split(',')
for att in self._attachements:
# 从列表中提交附件,附件可以有多个
msg.attach(att)
msg.attach(contents)
try:
self.smtp.sendmail(self._from, rto, msg.as_string())
print('{0} 邮件发送成功,请注意查收'.format(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
except Exception as e:
print('Error:', e)
else:
rto = self.email_info['to'].split(',')
for att in self._attachements:
# 从列表中提交附件,附件可以有多个
msg.attach(att)
msg.attach(contents)
try:
self.smtp.sendmail(self._from, rto, msg.as_string())
print('{0} 邮件发送成功,请注意查收'.format(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
except Exception as e:
print('Error:', e)
[Asm] 纯文本查看 复制代码 def close(self):
# 退出smtp服务
self.smtp.quit()
print('{0} 任务结束,退出登录'.format(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))) |