Python邮件附件数量可扩展

huangapple go评论54阅读模式
英文:

Python number of email attachements scalable

问题

我正在发送有关交付的电子邮件。电子邮件将附带所有文件,具体取决于卡车上有多少东西。在下面的代码中,我将展示我有的内容:

if filSize > 1:
        for u in range(filSize-1):
    
            att2 = ("C:\\Users\\hancke\\Desktop\\TEst mail\\" + filNameArr[z][2] + ".pdf", "application", "pdf", filNameArr[z][2] + ".pdf")
            att3 = (att3, att2)

        print("What to expect for att3:")
        print(att3.__str__())
        for attachment, main, sub, filename in (
            att3):
            with open(attachment, "rb") as fp: msg.add_attachment(fp.read(), maintype=main, subtype=sub,
                                                                      filename=filename)
        z = z + 1
    else:
        with open(attach1, "rb") as fp:
            msg.add_attachment(fp.read(), maintype="application", subtype="pdf", filename="filename.pdf")

这是我尝试的最后一种方法。当只有一个交付时,一切都正常。对于具有静态值的多个交付,它将如下所示:

for attachment, main, sub, filename in (
            ('C:\\Users\\hancke\\Desktop\\TEst mail\1515.pdf', "application", "pdf", "PDFTest.pdf"),
            ('C:\\Users\\hancke\\Desktop\\TEst mail\2020.pdf', "application", "pdf", "PDFTest.pdf"), 
            ('C:\\Users\\hancke\\Desktop\\TEst mail\2525.pdf', "application", "pdf", "PDFTest.pdf")):
            with open(attachment, "rb") as fp: msg.add_attachment(fp.read(), maintype=main, subtype=sub, filename=filename) 

我不确定如何更改这个静态值,以便它可以根据交付物品的数量进行扩展。我尝试了一些不同的方法,但似乎找不到正确的解决方法。目前,我有att2 = ("C:\\Users\\hancke\\Desktop\\TEst mail\\" + filNameArr[z][2] + ".pdf", "application", "pdf", filNameArr[z][2] + ".pdf")att3 = (att3, att2)att3是我的第一个项目。

英文:

I am sending an email for deliveries. The email will have attached all documents depending on how many things are on the truck. In the below code I will show what I have:

if filSize > 1:
        for u in range(filSize-1):
    
            att2 = ("C:\\Users\\hancke\\Desktop\\TEst mail\\" + filNameArr[z][2] + ".pdf", "application", "pdf", filNameArr[z][2] + ".pdf")
            att3 = (att3, att2)


        print("What to expect for att3:")
        print(att3.__str__())
        for attachment, main, sub, filename in (
            att3):
            with open(attachment, "rb") as fp: msg.add_attachment(fp.read(), maintype=main, subtype=sub,
                                                                      filename=filename)
        z = z + 1
    else:
        with open(attach1, "rb") as fp:
            msg.add_attachment(fp.read(), maintype="application", subtype="pdf", filename="filename.pdf")

This is the last thing that I have tried. When there is only one delivery everything is fine. With multiple deliveries having it static it will look like this:

for attachment, main, sub, filename in (
            ('C:\\Users\\hancke\\Desktop\\TEst mail\1515.pdf", "application", "pdf", "PDFTest.pdf"'),
            ('C:\\Users\\hancke\\Desktop\\TEst mail\2020.pdf", "application", "pdf", "PDFTest.pdf"'), 
            ('C:\\Users\\hancke\\Desktop\\TEst mail\2525.pdf", "application", "pdf", "PDFTest.pdf"')):
            with open(attachment, "rb") as fp: msg.add_attachment(fp.read(), maintype=main, subtype=sub, filename=filename) 

I am not sure how to change that static value so that it can scale on the amount of items out for delivery. I have tried a few different things, But cant seem to find the right way to solve this. Currently I have the att2 = ("C:\\Users\\hancke\\Desktop\\TEst mail\\" + filNameArr[z][2] + ".pdf", "application", "pdf", filNameArr[z][2] + ".pdf")

att3 = (att3, att2)

With att3 being my first item.

答案1

得分: 1

不需要在for循环中使用圆括号。如果这样做,解包的结果将只包含一个项。您想要使用的动态列表应该与您希望解包的方式具有相同的格式。有关打包/解包列表和元组的更多信息,请参阅此链接

my_dynamic_list = [(attachment, main, sub, filename)]
for attachment, main, sub, filename in my_dynamic_list:
    with open(attachment, "rb") as fp:
        msg.add_attachment(fp.read(), maintype=main, subtype=sub, filename=filename)

此外,if .. else .. 的缩进不一致。

英文:

You do not need the round brackets in a for loop. If you do that the unpacking is only one item long. The dynamic list you want to use should be in the same format as how you want to unpack it. See this for more info on packing/unpacking lists and tuples

my_dynamic_list = [(attachment, main, sub, filename)]
for attachment, main, sub, filename in my_dynamic_list:
        with open(attachment, "rb") as fp: msg.add_attachment(fp.read(), maintype=main, subtype=sub, filename=filename) 

Furter, the if .. else .. is not on the same indent.

huangapple
  • 本文由 发表于 2023年2月10日 16:41:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75408690.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定