#!/usr/bin/env python3

import smtplib, ssl, os, lorem, random, time
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase

sender_email=os.environ['mail_user']
password=os.environ['mail_pw']


receiver_email = [
  "dejan@mail-ost.ch",
  "roman@mail-ost.ch",
  "roman@mail-ost.ch",
  "mino@mail-ost.ch"
]

files = [
  "file1",
  "file2",
  "file3",
  "file4",
  "file5"
]

mailServer="mail.cvijano.ch"


def getHeader(receiver):
  message = MIMEMultipart("alternative")
  message["Subject"] = lorem.sentence()
  message["From"] = sender_email
  message["To"] = receiver

  return message

def getFile():
  filename = random.choice(files)
  with open(f"./files/{filename}", "rb") as attachment:
    part = MIMEBase("application", "octet-stream")
    part.set_payload(attachment.read())
  
  return part,filename

context = ssl.create_default_context()

while True:
  with smtplib.SMTP_SSL(mailServer, 465, context=context) as server:
    server.login(sender_email, password)
    receiver = random.choice(receiver_email)
    message = getHeader(receiver)
    part2,filename = getFile()

    text = ""
    for i in range(random.choice(range(1,6))):
      text += lorem.paragraph()
      text += "\n\n"
    
    part1 = MIMEText(text, "plain")
    part2.add_header(
      "Content-Disposition",
      f"attachment; filename= {filename}"
    )
    message.attach(part1)

    message.attach(part2)
    

    server.sendmail(sender_email, receiver, message.as_string())
    time.sleep(20)
  
