-1
I’m creating a bot in python, for sending emails to some people with different attachments, the code is like this:
import win32com.client as win32
import time
import os
#Informações do e-mail
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.SentOnBehalfOfName = '[email protected]'
#Percorre o diretório de anexos
for attachment in os.listdir(r"pasta_anexos"):
#Agrengando os arquivos da pasta a uma string
file = (os.path.join(r"pasta_anexos",str(attachment)))
if str(file).endswith('test1.txt'):
#Anexando test1.txt
mail.Attachments.Add(file)
#Percorrendo novamente o diretorio, para nexar o segundo arquivo
for attachment2 in os.listdir(r"pasta_anexo"):
#Agrengando os arquivos da pasta a uma string
file2 = (os.path.join(r"pasta_anexo",str(attachment2)))
if str(attachment2).endswith("test2.txt"):
#Anexando test2.txt
mail.Attachments.Add(file2)
#informações finais do e-mail, para quem e assunto
mail.To = '[email protected]'
mail.Subject = "Empresa X " + attachment
mail.display()
time.sleep(5)
mail.Send()
break
print('------------------ Primeiro e-mail ------------------')
I am currently sending an email with two attachments, but I would need to send one more with two different attachments, in the first email will two files Test1.txt and test2.text, in the second would test3.txt and test4.text, but when I try to put another For the error Spyder that did not find the second test3.txt
About the emails, just to quote, Test1 should be sent to [email protected], test2 to [email protected], I was thinking about putting each email inside if
– Vitor Xavier
No = a[i just extract the recipient’s email name from the file name itself. If the file name was a string, as in your code, just do
.split('.')[0]
- with pathlib just use the attribute "Stem" (the file name without the extension) - I will update above– jsbueno