-2
Good afternoon, all right?
I’m having a doubt in my python code, I’m trying to do an automation of sending emails with specific files, these emails I send daily and are random emails with random attachments, sometimes have 15 files that go to 15 e-different mails and has times that has 20 files that go to different emails, depends on the day. My question is how to validate how many files are in the folder on that specific day and how to parameterize which email it will be sent to. Studying throughout the day I created a code that validates whether the file with the specific name exists or not, if it exists, it changes the name of the file by the email of the person who will be sent and so sends by email, so it works, however, has more 15 e-mails to do this and this email number is random sometimes are 15,16,17,18 files to send on the day. Can someone help me? Obs(each file has a default name, for example teste1, teste2 and teste3, I have a list of the emails set for whom I need to send the teste1, teste2 and teste3).
import os
import win32com.client as win32
import time
from pathlib import Path
import os.path
if(os.path.exists(r'C:\Users\Desktop\convenios\teste1.txt')):
print("O arquivo existe")
else:
print("O arquivo não existe")
os.rename (r'C:\Users\Desktop\convenios\teste1.txt',r'C:\Users\Desktop\convenios2\[email protected]')
#Informações do e-mail
outlook = win32.Dispatch('outlook.application')
folder = Path(r"C:\Users\Desktop\convenios2")
#leitura documentos em anexo
for attachment in folder.iterdir():
#envio de e-mail
mail = outlook.CreateItem(0)
mail.SentOnBehalfOfName = '[email protected]'
mail.HTMLBody = """
<p>Olá,</p>
<p>Segue documentos em anexo!</p>
<p>Atenciosamente.</p>
<p>João</p>"""
mail.Attachments.Add(str(attachment))
# coloca o nome do arquivo, sem a extnsão, como endereço do email antes do "@"
mail.To = f'{attachment.stem}'
#mail.Subject = str(attachment)
mail.subject = ('arquivo teste')
mail.display()
time.sleep(5)
mail.Send()
print ("email enviado com sucesso!")
Thanks for your help, Kelvin!
– Gabriel Terribile
Anything if it’s not clear just tell me around here that I’m trying to help
– Kelvin Loretto
Show bro, I just have one more question, I’m studying the repetition structure while, only I’m not able to imagine how to validate the file. example: today has 15 files, how would I know that there are 15 files and 15 are for the specific email, then tomorrow I have 16 files and he needs to add 1 more email, that automatically, understand?
– Gabriel Terribile
I will edit my answer to illustrate better
– Kelvin Loretto
Kelvin, the amendment has worked, thank you very much!
– Gabriel Terribile
Kelvin, is out there?
– Gabriel Terribile
Good morning Gabriel, I believe if it is any doubt, creates a question that I already answer as soon as you open
– Kelvin Loretto
show, if you can give a boost ;-;
– Gabriel Terribile