gpt4 book ai didi

python - 发送带有 3 个不同附件的一封电子邮件 python 3

转载 作者:行者123 更新时间:2023-12-02 03:29:21 26 4
gpt4 key购买 nike

我只是想发送一封包含 3 个 csv 文件附件的电子邮件。当我让它运行时,尝试我在这里找到的解决方案,它给了我一个 BIN 文件而不是 3 个文件。或者,尝试另一种解决方案,它只会发送 3 个文件中的最后一个。当我运行下面的代码时,它给出了 TypeError: add.header() 需要 3 个位置参数,但给出了 4 个。

我知道它可以通过一个函数来完成,但我不确定如何让它将所有三个文件拉入其中。我花了很多时间试图弄清楚。

将其发布在这里是我最后的手段。我感谢您在寻找解决方案方面提供的任何帮助。

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import time
import os

msg = MIMEMultipart()
msg['From'] = EMAIL_FROM
msg['To'] = ", ".join(RECIPIENT_LIST)
msg['Subject'] = 'Louisiana Contractors List'

#email content
message = """<html>
<body>
Attached is the Louisiana Contractors Spreadsheet.
<br><br>

Let me know if you have any questions

</body>
</html>
"""

msg.attach(MIMEText(message, 'html'))

files = [
'C:/Users/rkrouse/Downloads/search-results.csv',
'C:/Users/rkrouse/Downloads/search-results(1).csv',
'C:/Users/rkrouse/Downloads/search-results(2).csv']

for a_file in files:
attachment = open(a_file, 'rb')
part = MIMEBase('application','octet-stream')
part.set_payload((attachment).read())
part.add_header('Content-Disposition', 'attachment', a_file = os.path.basename('C:/Users/rkrouse/Downloads/search-results.csv'))
encoders.encode_base64(part)
msg.attach(part)

for a_file in files:
attachment = open(a_file, 'rb')
part = MIMEBase('application','octet-stream')
part.set_payload((attachment).read())
part.add_header('Content-Disposition', 'attachment', a_file = os.path.basename('C:/Users/rkrouse/Downloads/search-results(1).csv'))
encoders.encode_base64(part)
msg.attach(part)

for a_file in files:
attachment = open(a_file, 'rb')
part = MIMEBase('application','octet-stream')
part.set_payload(attachment.read())
part.add_header('Content-Disposition', 'attachment', a_file = os.path.basename('C:/Users/rkrouse/Downloads/search-results(2).csv'))
encoders.encode_base64(part)
msg.attach(part)

#sends email
smtpserver = smtplib.SMTP(EMAIL_SERVER, EMAIL_PORT)
smtpserver.sendmail(EMAIL_FROM, RECIPIENT_LIST, msg.as_string())
smtpserver.quit()

最佳答案

更新的测试解决方案:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import time
import os

msg = MIMEMultipart()
msg['From'] = EMAIL_FROM
msg['To'] = ", ".join(RECIPIENT_LIST)
msg['Subject'] = 'Louisiana Contractors List'

#email content
message = """<html>
<body>
Attached is the Louisiana Contractors Spreadsheet.
<br><br>

Let me know if you have any questions

</body>
</html>
"""

msg.attach(MIMEText(message, 'html'))

files = [
'C:/Users/rkrouse/Downloads/search-results.csv',
'C:/Users/rkrouse/Downloads/search-results(1).csv',
'C:/Users/rkrouse/Downloads/search-results(2).csv']

for a_file in files:
attachment = open(a_file, 'rb')
file_name = os.path.basename(a_file)
part = MIMEBase('application','octet-stream')
part.set_payload(attachment.read())
part.add_header('Content-Disposition',
'attachment',
filename=file_name)
encoders.encode_base64(part)
msg.attach(part)

#sends email

smtpserver = smtplib.SMTP(EMAIL_SERVER, EMAIL_PORT)
smtpserver.sendmail(EMAIL_FROM, RECIPIENT_LIST, msg.as_string())
smtpserver.quit()

如您所见,您需要以 Base64 编码指定文件名。此外,您还将文件迭代三次。

for 循环用于遍历列表、集合、数组等。一个 for 循环(因为所有附件都在一个列表中)就足够了。

关于python - 发送带有 3 个不同附件的一封电子邮件 python 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52292971/

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