smtplib 重命名多封电子邮件的最佳方式
smtplib best way to rename multiple emails
我有一个 smtplib 函数,它循环遍历 2 个 excel 文件,然后打开它们并将它们添加为附件。现在它们有通用名称,但我认为从数据中获取信息并将其用作文档名称会很酷。例如,如果我已被用户过滤,我想根据在数据框中找到的“位置”列来获取他们的位置。
目标: 将 2 个当前名为“File1.xlsx”和“File2.xlsx”的文档重命名为
“location_email_reminder1_3-4-2022”和“location_email_reminder2_3-4-2022”
到目前为止,这是我的代码:
#imports
import datetime as dt
from datetime import date
import smtplib
import pandas as pd
from email.message import EmailMessage
#data
today = date.today()
today_clean = today.strftime("%m-%d-%Y")
#these are saved to my computer
files = ['file1.xlsx', 'file2.xlsx']
#real code- location is a string that comes from a dataframe
location = df.loc[df['Login Key'] == email, 'Location Name'][0]
#test code for Whosebug
location = 'USA'
#for renaming the file, these arent real files, only names
new_attach = [f"{location}_email_reminder1_{today_clean}.xlsx",f"{territory}_email_reminder2_{today_clean}.xlsx"]
#loop for the file names
msg = EmailMessage()
for filename in files:
with open(filename, 'rb') as file:
file_data = file.read()
msg.add_attachment(file_data, maintype='application', subtype='octet-stream', filename=file.name)
通常情况下,如果是单个文件,我可以使用“文件名”方法更改名称,但我不确定如何对超过 1 个文件进行更改。
您可以使用 zip
函数创建一个字典,并将旧名称引用为键,以将所需的新名称传递给 filename
参数。
...
new_attach = [f"{location}_email_reminder1_{today_clean}.xlsx",
f"{territory}_email_reminder2_{today_clean}.xlsx"]
new_ref = { orig_file : new_file for orig_file, new_file in zip(files, new_attach) }
msg = EmailMessage()
for filename in files:
with open(filename, 'rb') as file:
file_data = file.read()
new_name = new_ref[file.name]
msg.add_attachment(file_data, maintype='application', subtype='octet-stream', filename=new_name)
我有一个 smtplib 函数,它循环遍历 2 个 excel 文件,然后打开它们并将它们添加为附件。现在它们有通用名称,但我认为从数据中获取信息并将其用作文档名称会很酷。例如,如果我已被用户过滤,我想根据在数据框中找到的“位置”列来获取他们的位置。
目标: 将 2 个当前名为“File1.xlsx”和“File2.xlsx”的文档重命名为 “location_email_reminder1_3-4-2022”和“location_email_reminder2_3-4-2022”
到目前为止,这是我的代码:
#imports
import datetime as dt
from datetime import date
import smtplib
import pandas as pd
from email.message import EmailMessage
#data
today = date.today()
today_clean = today.strftime("%m-%d-%Y")
#these are saved to my computer
files = ['file1.xlsx', 'file2.xlsx']
#real code- location is a string that comes from a dataframe
location = df.loc[df['Login Key'] == email, 'Location Name'][0]
#test code for Whosebug
location = 'USA'
#for renaming the file, these arent real files, only names
new_attach = [f"{location}_email_reminder1_{today_clean}.xlsx",f"{territory}_email_reminder2_{today_clean}.xlsx"]
#loop for the file names
msg = EmailMessage()
for filename in files:
with open(filename, 'rb') as file:
file_data = file.read()
msg.add_attachment(file_data, maintype='application', subtype='octet-stream', filename=file.name)
通常情况下,如果是单个文件,我可以使用“文件名”方法更改名称,但我不确定如何对超过 1 个文件进行更改。
您可以使用 zip
函数创建一个字典,并将旧名称引用为键,以将所需的新名称传递给 filename
参数。
...
new_attach = [f"{location}_email_reminder1_{today_clean}.xlsx",
f"{territory}_email_reminder2_{today_clean}.xlsx"]
new_ref = { orig_file : new_file for orig_file, new_file in zip(files, new_attach) }
msg = EmailMessage()
for filename in files:
with open(filename, 'rb') as file:
file_data = file.read()
new_name = new_ref[file.name]
msg.add_attachment(file_data, maintype='application', subtype='octet-stream', filename=new_name)