python emailing with several attachments

Asked

Viewed 322 times

-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.

  • 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

  • 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

  • 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

1 answer

2


In doing pdfName = "caminho/202008/*.pdf" you are not searching all PDF files in your directory, but only a file that has the name *.pdf, that probably doesn’t even exist.

To actually search all the PDF files in the directory, I recommend that you read about the class pathlib.Path, in particular the method glob:

from pathlib import Path

date = '202008'
directory = Path('caminho') / date
pdfs = directory.glob('*.pdf')

After that, make a repeat loop that sticks over the list of files by attaching them one by one in your email.

To get the current year/month, I suggest using the package datetime.

  • I figured I could try to change the folder name using datetime. I will try to implement it this way and I will take a look at this pathlib class. Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.