gpt4 book ai didi

python - 'str' 对象没有属性 'mime' 错误?

转载 作者:行者123 更新时间:2023-12-04 22:17:47 26 4
gpt4 key购买 nike

下面是我的代码。 Email.xlsx 具有列名称和电子邮件。 Name 列的值与同一文件夹中的某些 Excel 文件名匹配。想法是将带有附件(Excel 工作簿)的电子邮件发送到 Email.xlsx 中匹配的电子邮件地址。例如:-
电子邮件.xlsx

Name Email 
1001 xxx@gmail.com
1002 yyy@gmail.com
1003 xxx@xxx.com
文件夹中的文件:
1001.xlsx
1002.xlsx
1004.xlsx
1005.xlsx
期望是分别以 1001.xlsx 和 1002.xlsx 作为附件向 xxx@gmail.com 和 yyy@gmail.com 发送电子邮件。它们是唯一匹配的名称。我的代码如下,它给了我这个错误: -
AttributeError: 'str' object has no attribute 'mime'
有什么指向我做错的地方吗?
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

email_list = pd.read_excel(r'Email.xlsx')
folder_path="." #Same folder as above
my_files=[{each_file.split(".")[0]:each_file} for each_file in os.listdir(folder_path) if each_file.endswith(".xlsx")]
my_files_dict = dict(ChainMap(*my_files))

names = email_list['Name']
emails = email_list['Email']

for i in range(len(emails)): # iterate through the records
# for every record get the name and the email addresses

name = str(names[i])
email = emails[i]

if my_files_dict.get(name):
smtp_ssl_host = 'smtp.gmail.com'
smtp_ssl_port = 465
email_from = "xxxxx@gmail.com"
email_pass = "xxxx"
email_to = email
msg2 = MIMEMultipart()
msg2['Subject'] = "Record(s)"
msg2['From'] = email_from
msg2['To'] = email
filename = my_files_dict.get(name)
print(filename)
attach = email.mime.application.MIMEApplication(filename.read(),_subtype="xlsx")
msg.attach(attach)
s2 = smtplib.SMTP_SSL(smtp_ssl_host, smtp_ssl_port)
s2.login(email_from, email_pass)
s2.send_message(msg)
s2.quit()

最佳答案

那是因为你重命名了 email包在这里:

email = emails[i]
您正在尝试引用 from email.mime.text import MIMEText但您指的是字符串 email反而。只需更改字符串的名称就可以了

关于python - 'str' 对象没有属性 'mime' 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67264837/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com