重点
mime.add_header('Content-Disposition', 'attachment', filename=( make_header([(file_name, 'UTF-8')]).encode('UTF-8')))
完整代码可以发送多个附件
import smtplib
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email.header import Header, make_header
from email.utils import COMMASPACE, formatdate
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.mime.application import MIMEApplication
from email import encoders
from datetime import datetime
from config import mail_tail
import time, os
SMTP_IP = 'sftp.gmail.com'
SMTP_PORT = 25 # 587
EMAIL_FROM = 'rpa05@rpa.com'
EMAIL_PASSWORD = '82767630'
SMTP_CONTENT_TYPE_HTML = 'html'
SMTP_CONTENT_TYPE_TEXT = 'text'
# 第三方 SMTP 服务
mail_host = SMTP_IP # 设置服务器
mail_user = EMAIL_FROM # 用户名
mail_pass = EMAIL_PASSWORD # 口令
sender = EMAIL_FROM
to = cc = receivers = ['yu@rpa.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
week = time.strftime("%W")
year = time.strftime("%Y")
day = datetime.now().strftime('%Y%m%d')
mail_msg = mail_tail
def send_mail(mail_msg, subject, to, cc, bcc, file_name=None, file_path=None, name=None):
message = MIMEMultipart('related')
# message = MIMEText(mail_msg, 'html', 'utf-8')
message['From'] = 'rpa05@rpa.com'
message['To'] = COMMASPACE.join(to)
message['Cc'] = COMMASPACE.join(cc)
msgAlternative = MIMEMultipart('alternative')
message.attach(msgAlternative)
#添加附件
# att1 = MIMEBase(open(file_path, 'rb').read(), 'base64')
# att1["Content-Type"] = 'application/octet-stream'
# # 这里的filename可以任意写,写什么名字,邮件中显示什么名字
# att1["Content-Disposition"] = f'attachment; filename={file_name}'
# message.attach(att1)
if file_path:
print('os path', os.path.exists(file_path))
a = os.path.exists(file_path)
while not a:
print('while os path', os.path.exists(file_path))
time.sleep(0.5)
a = os.path.exists(file_path)
if file_path:
with open(file_path, 'rb') as f:
# 设置附件的MIME和文件名,这里是png类型:
mime = MIMEBase('application','vnd.ms-excel') #, filename=file_name
# 加上必要的头信息:
mime.add_header('Content-Disposition', 'attachment', filename=file_name)
mime.add_header('Content-ID', '<0>')
mime.add_header('X-Attachment-Id', '0')
# 把附件的内容读进来:
mime.set_payload(f.read())
# 用Base64编码:
encoders.encode_base64(mime)
# 添加到MIMEMultipart:
message.attach(mime)
subject = subject
message['Subject'] = Header(subject)
message['Date'] = formatdate(localtime=True)
msgAlternative.attach(MIMEText(mail_msg, 'html', 'utf-8'))
# 指定图片为当前目录
# fp = open(img, 'rb')
# msgImage = MIMEImage(fp.read())
# fp.close()
# 定义图片 ID,在 HTML 文本中引用
# msgImage.add_header('Content-ID', '<image1>')
# message.attach(msgImage)
server = smtplib.SMTP('10.10.10.10:25')
server.ehlo_or_helo_if_needed()
server.ehlo() # 向Gamil发送SMTP 'ehlo' 命令
server.starttls()
# server.login(sender, mail_pass)
print(datetime.now())
print("邮件发送成功---------->",name)
if file_path:
os.remove(file_path)
server.sendmail('rpa@rpa.com', to + cc + bcc , message.as_string())
server.quit()
def send_mail_multi_attch(mail_msg, subject, to, cc, bcc, file_list=None, name=None):
message = MIMEMultipart('related')
# message = MIMEText(mail_msg, 'html', 'utf-8')
message['From'] = 'rpa05@rpa.com'
message['To'] = COMMASPACE.join(to)
message['Cc'] = COMMASPACE.join(cc)
msgAlternative = MIMEMultipart('alternative')
message.attach(msgAlternative)
#添加附件
# att1 = MIMEBase(open(file_path, 'rb').read(), 'base64')
# att1["Content-Type"] = 'application/octet-stream'
# # 这里的filename可以任意写,写什么名字,邮件中显示什么名字
# att1["Content-Disposition"] = f'attachment; filename={file_name}'
# message.attach(att1)
if file_list:
for index, file in enumerate(file_list):
file_path = file.get('path')
file_name = file.get('name')
with open(file_path, 'rb') as f:
# 设置附件的MIME和文件名,这里是png类型:
# mime = MIMEBase('application','vnd.ms-excel') #, filename=file_name
mime = MIMEBase('application','octet-stream') #, filename=file_name
# 加上必要的头信息:
print('file_name', file_name)
mime.add_header('Content-Disposition', 'attachment', filename=( make_header([(file_name, 'UTF-8')]).encode('UTF-8')))
mime.add_header('Content-ID', f'<{index}>')
mime.add_header('X-Attachment-Id', f'{index}')
# 把附件的内容读进来:
mime.set_payload(f.read())
# 用Base64编码:
encoders.encode_base64(mime)
# 添加到MIMEMultipart:
message.attach(mime)
subject = subject
message['Subject'] = Header(subject)
message['Date'] = formatdate(localtime=True)
msgAlternative.attach(MIMEText(mail_msg, 'html', 'utf-8'))
# 指定图片为当前目录
# fp = open(img, 'rb')
# msgImage = MIMEImage(fp.read())
# fp.close()
# 定义图片 ID,在 HTML 文本中引用
# msgImage.add_header('Content-ID', '<image1>')
# message.attach(msgImage)
server = smtplib.SMTP('10.10.10.10:25')
server.ehlo_or_helo_if_needed()
server.ehlo() # 向Gamil发送SMTP 'ehlo' 命令
server.starttls()
# server.login(sender, mail_pass)
print(datetime.now())
print("邮件发送成功---------->",name)
# if file_path:
# os.remove(file_path)
server.sendmail('rpa@rpa.com', to + cc + bcc , message.as_string())
server.quit()
if __name__ == '__main__':
pass
参考
https://blog.csdn.net/u013948083/article/details/95313594
https://segmentfault.com/q/1010000016296204/