import smtplib
import paramiko

import mimetypes
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import os

def copy_partial_result(filenames):
    for filename in filenames:
        os.system("scp "+filename+" pedino@linux.cs.ox.ac.uk:/fs/website/people/pedro.antonino/")
    

def send_result(filenames, title):
    emailfrom = "pedro.antonino@cs.ox.ac.uk"
    emailto = "prgantonino@gmail.com"
    
    msg = MIMEMultipart()
    msg["From"] = emailfrom
    msg["To"] = emailto
    msg["Subject"] = title
    msg.preamble = title
    for filename in filenames:
        ctype, encoding = mimetypes.guess_type(filename)
        if ctype is None or encoding is not None:
            ctype = "application/octet-stream"
        
        maintype, subtype = ctype.split("/", 1)
        
        if maintype == "text":
            fp = open(filename)
            # Note: we should handle calculating the charset
            attachment = MIMEText(fp.read(), _subtype=subtype)
            fp.close()
        attachment.add_header("Content-Disposition", "attachment", filename=filename)
        msg.attach(attachment)
    
    server = smtplib.SMTP("mail.cs.ox.ac.uk",25)
    server.sendmail(emailfrom, emailto, msg.as_string())
    server.quit()
    
if __name__ == '__main__':
    send_result("Test2/results_table.txt", "Results", ["prgantonino@gmail.com"])