mardi 11 décembre 2018

Select a random file from a directory and send it (Python, MIME)

I am working on a python program that randomly selects a file from a directory and then sends it to you using the email.mimemodule. I am having a problem where I can choose the random file but I can't sent it due to this error:

 File "C:\Users\Mihkel\Desktop\dnak.py", line 37, in sendmemeone
    attachment  =open(filename, 'rb')
TypeError: expected str, bytes or os.PathLike object, not list

Here is the code:

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import  MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
import os
import random

path ='C:/Users/Mihkel/Desktop/memes'
files = os.listdir(path)
index = random.randrange(0, len(files))
print(files[index])

def send():
    email_user = 'yeetbotmemes@gmail.com'
    email_send = 'miku.rebane@gmail.com'
    subject = 'Test'
    msg = MIMEMultipart()
    msg['From'] = email_user
    msg['To']   = email_send
    msg['Subject'] = subject
    body = 'Here is your very own dank meme of the day:'
    msg.attach(MIMEText (body, 'plain'))
    filename=files
    attachment  =open(filename, 'rb')
    part = MIMEBase('application','octet-stream')
    part.set_payload((attachment).read())
    encoders.encode_base64(part)
    part.add_header('Content-Disposition',"attachment; 
    filename= "+filename)
    msg.attach(part)
    text = msg.as_string()
    server = smtplib.SMTP('smtp.gmail.com',587)
    server.starttls()
    server.login(email_user,"MY PASSWORD")
    server.sendmail(email_user,email_send,text)
    server.quit()

I believe it is just getting the filename as the selected random choice, how could I get it to select the file itself?




Aucun commentaire:

Enregistrer un commentaire