title: How can I send random pdf files in email from a folder using Python?
body: <p>I am trying to create an email bot that will send me a random pdf file from a folder. Though my code is not showing any error, I am not getting any mail. It'd be helpful if you can show me where I am going wrong and what should I do. Thank you.</p>
  <pre><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
  def send():
      body = &quot;&quot;
      sender_email = &quot;email&quot;
      password = &quot;my_password&quot;
      receiver_email = &quot;email&quot;
  
      msg = MIMEMultipart()
      msg['Subject'] = '[Email Test]'
      msg['From'] = sender_email
      msg['To'] = receiver_email
      msg.attach(MIMEText(body, 'plain'))
      path = &quot;C:/Users/Asus/PycharmProjects/messenger_bot/files&quot;
      files = os.listdir(path)
      index = random.randrange(0, len(files))
      print(files[index])
      attachment = open(os.path.join(path, random.choice(files)), 'rb')
  
      payload = MIMEBase('application', 'octate-stream')
      # payload = MIMEBase('application', 'pdf', Name=pdfname)
      payload.set_payload(attachment.read())
  
      # enconding the binary into base64
      encoders.encode_base64(payload)
  
      # add header with pdf name
      payload.add_header('Content-Decomposition', 'attachment', filename=files)
      msg.attach(payload)
  
      # use gmail with port
      session = smtplib.SMTP('smtp.gmail.com', 587)
  
      # enable security
      session.starttls()
  
      # login with mail_id and password
      session.login(sender_email, password)
  
      text = msg.as_string()
      session.sendmail(sender_email, receiver_email, text)
      session.quit()
      print('Mail Sent')
  </code></pre>