0

I am a newbie to python, I would like to create an application that can send all files from a folder.

anyidea to get all files from a folder and send email to someone

in example, there are four files from my folder, and I would to get all files from a folder Thank you very much

Folder
 -AAA.txt
 -BBB.zip
 -CCC.docx
 -av.mp4

my code

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header


mail_host="smtp.gmail.com"  
mail_user="testing"    
mail_pass="testing"   

sender = 'testing@gmail.com'
receivers = ['testing@gmail.com']

message = MIMEMultipart()
message['From'] = Header("Hello2", 'utf-8')
message['To'] =  Header("World2", 'utf-8')

subject = 'Python SMTP Testing'
message['Subject'] = Header(subject, 'utf-8')

att1 = MIMEText(open('folder/*', 'rb').read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'

att1["Content-Disposition"] = 'attachment; filename="test.txt"'
message.attach(att1)

try:
    smtpObj = smtplib.SMTP()
    smtpObj.connect(mail_host, 25)
    smtpObj.login(mail_user,mail_pass)  
    smtpObj.sendmail(sender, receivers, message.as_string())
    print('Sent...')
except:
    traceback.print_exc()
    print('Error: ')
MaryAnn
  • 95
  • 1
  • 8
  • I suggest that you compress the folder, and then send it as a single attachment, I don't know if folder uploads are allowed, even if they are, they are always sent as different attachments. Compressing the folder has another advantage, if your folder contains another folder, you don't need to worry about it. – tsamridh86 Dec 08 '20 at 06:57
  • Zip like this: https://stackoverflow.com/a/25650295/6091017. Then send that :) – kluvin Dec 08 '20 at 10:04

0 Answers0