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
'''将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")))
def add_attachment(self):
# 添加附件内容
# 注意:添加附件内容是通过读取文件的方式加入
file_path = self.email_info['file_path']
# print(file_path)
for num in range(0, len(file_path)):
with open(file_path, 'rb') as file:
filename = os.path.split(file_path)
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")))
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)
def close(self):
# 退出smtp服务
self.smtp.quit()
print('{0} 任务结束,退出登录'.format(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))) yagmail发邮件好简单 yagmail 一行解决 感谢发布原创作品,吾爱破解论坛因你更精彩! 最简
https://cloud.tencent.com/developer/article/1855997 菱角 发表于 2021-11-9 12:34
yagmail发邮件好简单
感谢提醒 GhostCN_Z 发表于 2021-11-9 16:23
最简
https://cloud.tencent.com/developer/article/1855997
感谢分享,我研究下你这个,我写这个需要配合部分业务场景,已经看下能不能结合 我今天是大佬 发表于 2021-11-9 13:59
yagmail 一行解决
感谢提醒,又学到新的东西,哈哈哈:lol Mozilla/5.0 (Windows NT 6.1; rv:94.0) Gecko/20100101 Firefox/94.0
页:
[1]