使用 AT00001 删除电子邮件附件文件名
Email attachment file names are removed with AT00001
发送带有多个附件的邮件正在删除附件文件名。附件名称更改为 ATT00001.xlsx。根据以下 link,'body' 部分在附件之前添加,但没有成功。
https://exchange-server-guide.blogspot.com/2015/11/att00001.txt-file-fix-email-attachment-issue.html
供参考,分享以下代码片段。任何建议表示赞赏。
msg = MIMEMultipart()
ctype = content_type
maintype, subtype = ctype.split('/', 1)
msg['Subject'] = subject
msg['To'] = 'abc@sample.com'
msg['From'] = sender
smtp_client = smtplib.SMTP(smtp_host + ':' + smtp_port)
smtp_client.starttls()
smtp_client.login(sender, smtp_login_password)
body_part = MIMEText(body, 'plain')
msg.attach(body_part)
for file_path in file_paths :
temp_arr = file_path.split('/')
file_name = temp_arr[len(temp_arr) - 1]
msg.add_header('Content-Disposition', 'attachment', filename=file_name)
fp = open(file_path, 'rb')
attachment = MIMEBase(maintype, subtype)
attachment.set_payload(fp.read())
fp.close()
encode_base64(attachment)
msg.attach(attachment)
smtp_client.sendmail(sender, 'abc@sample.com', msg.as_string())
smtp_client.quit()
您正在将 Content-Disposition:
添加到多部分容器中。您应该将它添加到每个单独的 body 部分。
改变这个:
for file_path in file_paths :
temp_arr = file_path.split('/')
file_name = temp_arr[len(temp_arr) - 1]
msg.add_header('Content-Disposition', 'attachment', filename=file_name)
fp = open(file_path, 'rb')
attachment = MIMEBase(maintype, subtype)
attachment.set_payload(fp.read())
fp.close()
encode_base64(attachment)
msg.attach(attachment)
类似于
for file_path in file_paths:
file_name = file_path.split('/')[-1]
attachment = MIMEBase(maintype, subtype)
with open(file_path, 'rb') as fp:
attachment.set_payload(fp.read())
attachment.add_header('Content-Disposition', 'attachment', filename=file_name)
encode_base64(attachment)
msg.attach(attachment)
我还冒昧地切换到使用上下文管理器(with
语句)。我还简化了文件名提取。
发送带有多个附件的邮件正在删除附件文件名。附件名称更改为 ATT00001.xlsx。根据以下 link,'body' 部分在附件之前添加,但没有成功。
https://exchange-server-guide.blogspot.com/2015/11/att00001.txt-file-fix-email-attachment-issue.html
供参考,分享以下代码片段。任何建议表示赞赏。
msg = MIMEMultipart()
ctype = content_type
maintype, subtype = ctype.split('/', 1)
msg['Subject'] = subject
msg['To'] = 'abc@sample.com'
msg['From'] = sender
smtp_client = smtplib.SMTP(smtp_host + ':' + smtp_port)
smtp_client.starttls()
smtp_client.login(sender, smtp_login_password)
body_part = MIMEText(body, 'plain')
msg.attach(body_part)
for file_path in file_paths :
temp_arr = file_path.split('/')
file_name = temp_arr[len(temp_arr) - 1]
msg.add_header('Content-Disposition', 'attachment', filename=file_name)
fp = open(file_path, 'rb')
attachment = MIMEBase(maintype, subtype)
attachment.set_payload(fp.read())
fp.close()
encode_base64(attachment)
msg.attach(attachment)
smtp_client.sendmail(sender, 'abc@sample.com', msg.as_string())
smtp_client.quit()
您正在将 Content-Disposition:
添加到多部分容器中。您应该将它添加到每个单独的 body 部分。
改变这个:
for file_path in file_paths :
temp_arr = file_path.split('/')
file_name = temp_arr[len(temp_arr) - 1]
msg.add_header('Content-Disposition', 'attachment', filename=file_name)
fp = open(file_path, 'rb')
attachment = MIMEBase(maintype, subtype)
attachment.set_payload(fp.read())
fp.close()
encode_base64(attachment)
msg.attach(attachment)
类似于
for file_path in file_paths:
file_name = file_path.split('/')[-1]
attachment = MIMEBase(maintype, subtype)
with open(file_path, 'rb') as fp:
attachment.set_payload(fp.read())
attachment.add_header('Content-Disposition', 'attachment', filename=file_name)
encode_base64(attachment)
msg.attach(attachment)
我还冒昧地切换到使用上下文管理器(with
语句)。我还简化了文件名提取。