重点
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
EMAIL_FROM = 'rpa05@rpa.com'
EMAIL_PASSWORD = '82767630'
SMTP_CONTENT_TYPE_HTML = 'html'
SMTP_CONTENT_TYPE_TEXT = 'text'
mail_host = SMTP_IP
mail_user = EMAIL_FROM
mail_pass = EMAIL_PASSWORD
sender = EMAIL_FROM
to = cc = receivers = ['yu@rpa.com']
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['From'] = 'rpa05@rpa.com'
message['To'] = COMMASPACE.join(to)
message['Cc'] = COMMASPACE.join(cc)
msgAlternative = MIMEMultipart('alternative')
message.attach(msgAlternative)
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 = MIMEBase('application','vnd.ms-excel')
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())
encoders.encode_base64(mime)
message.attach(mime)
subject = subject
message['Subject'] = Header(subject)
message['Date'] = formatdate(localtime=True)
msgAlternative.attach(MIMEText(mail_msg, 'html', 'utf-8'))
server = smtplib.SMTP('10.10.10.10:25')
server.ehlo_or_helo_if_needed()
server.ehlo()
server.starttls()
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['From'] = 'rpa05@rpa.com'
message['To'] = COMMASPACE.join(to)
message['Cc'] = COMMASPACE.join(cc)
msgAlternative = MIMEMultipart('alternative')
message.attach(msgAlternative)
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 = MIMEBase('application','octet-stream')
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())
encoders.encode_base64(mime)
message.attach(mime)
subject = subject
message['Subject'] = Header(subject)
message['Date'] = formatdate(localtime=True)
msgAlternative.attach(MIMEText(mail_msg, 'html', 'utf-8'))
server = smtplib.SMTP('10.10.10.10:25')
server.ehlo_or_helo_if_needed()
server.ehlo()
server.starttls()
print(datetime.now())
print("邮件发送成功---------->",name)
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/
|