-1
Good afternoon,
I am new to programming especially in python and I am trying to send all the files of a certain folder as an attachment in an email. And digging on the Internet I managed to get to this code I’m using:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
smtp_ssl_host = "smtp.exemplo.com"
smtp_ssl_port = 465
from_addr = "Meu Email"
to_addrs = ["Destinatario1", "Destinatario2"]
username = "Meu Email"
password = "minha Senha"
pdfName = "caminho/202008/*.pdf"
message = MIMEMultipart()
message["From"] = from_addr
message["To"] = ", ".join(to_addrs)
message["Subject"] = "Testando o envio de anexos pdf pelo python"
raw = message.as_string()
body = MIMEText("Exemplo de envio com anexo PDF")
message.attach(body)
fp = open(pdfName, 'rb')
anexo = MIMEApplication(fp.read(), _subtype="pdf")
fp.close()
anexo.add_header('Content-Disposition', 'attachment', filename=pdfName)
message.attach(anexo)
server = smtplib.SMTP_SSL(smtp_ssl_host, smtp_ssl_port)
server.login(username, password)
server.sendmail(from_addr, to_addrs, raw)
server.quit()
I can send the email, but the attachment does not send, plus I have the problem that every month the folder name changes following this pattern "path/202008", "path/202009" and so on. Then he needed the path of the folder to change according to the year and month and to send all the contents of the folder as an attachment. But I was only able to send the email without attachment, I wanted to know how to attach all the content and change the folder according to the date of the computer.
Thank you very much to anyone who can shed light on this issue.
Can you do a table test that describes the execution of this code? It seems to me that you are trying to use it without fully understanding it. Maybe the table test will help you with that.
– Woss
In fact I still consider myself a layman, I am studying about it so I would say I understand part of the code above, I think I understand. But I don’t know if I could run a test like that, but I’ll try it here. Actually I’m still right at the beginning when it comes to python and programming
– sarge88
The question of the folder name, if it is a sequential number is only you encapsulate the upload within a for, for example, and use a variable that increments by 1 unit each repetition
– Evilmaax
I understood, I had thought of creating a date object to take the current month and year and convert it into string to use in the folder name, because the folder name would only change when at each end of month. Would you solve it like that? I thought about it but I don’t know if it would generate a problem because I haven’t tested it yet
– sarge88