[ImoLUG] scriptino python per e-mail con account gmail

Luca Lama angelus491@gmail.com
Dom 6 Maggio 2012 09:12:01 CEST


script per mandare e-mail con allegati anche cartelle
penso abbia qualche bug ma tutto sommato gira...
-------------- parte successiva --------------
#!/usr/bin/env python

from Tkinter import *
import tkFileDialog
import os
import smtplib
import mimetypes
import email
import email.mime.application


#DEPENDS ON RAR AND PYTHON LIBRARIES ABOVE!!!



def funzione():
    global sendfile,recipient,subject,body
    if recipient.get()=='':
        error=Tk()
        error.wm_title("ERROR")
        f=Label(error,text="'To' field is missing").pack(side=BOTTOM, anchor=SE)
        return False
            
    da="angelus491@gmail.com" #INSERIRE IL PROPRIO INDIRIZZO!!!
    msg=email.mime.Multipart.MIMEMultipart()
    try:
        msg['Subject']= subject.get()
    except Exception:
        error=Tk()
        error.wm_title("ERROR")
        f=Label(error,text="No special chars!!!").pack(side=BOTTOM, anchor=SE)
        return False
    msg['From']=da
    msg['To']=recipient.get()
    try:
        body=email.mime.Text.MIMEText(body.get())
    except Exception:
        error=Tk()
        error.wm_title("ERROR")
        f=Label(error,text="No special chars!!!").pack(side=BOTTOM, anchor=SE)
        return False

    msg.attach(body)
    if sendfile.get()!="":
        os.system("rar a -m5 "+sendfile.get()+".rar "+sendfile.get()) #comprimo il file in file.rar
        filename=sendfile.get()+".rar"    #filename=file.rar
        try:                        #provo se esiste file.rar
            open(filename,'rb')
        except IOError:
            os.system("rm "+filename)
            error=Tk()
            error.wm_title("ERROR")
            f=Label(error,text="File nonexistent").pack(side=BOTTOM, anchor=SE)
            return False
        fp=open(filename,'rb')
        att = email.mime.application.MIMEApplication(fp.read())
        fp.close()
        os.system("rm "+filename)
        att.add_header('Content-Disposition','attachment',filename=filename)
        msg.attach(att)
    else:
        error=Tk()
        error.wm_title("Warning")
        f=Label(error,text="No file sent").pack(side=BOTTOM, anchor=SE)
        
    
    try:
        x=smtplib.SMTP_SSL("smtp.googlemail.com") #provo la connessione al server
    except smtplib.socket.gaierror:
        error=Tk()
        error.wm_title("ERROR")
        f=Label(error,text="Error connecting to host").pack(side=BOTTOM, anchor=SE)
        return False
    try:
        x.login("","") #INSERIRE I PROPRI DATI DI LOGIN USERNAME E PASSWORD
    except smtplib.SMTPAuthenticationError:
        error=Tk()
        error.wm_title("ERROR")
        f=Label(error,text="Error login to host").pack(side=BOTTOM, anchor=SE)
        x.quit()
        return False
    try:
        x.sendmail(da,recipient.get(),msg.as_string()) #provo a spedire
    except Exception:
        error=Tk()
        error.wm_title("ERROR")
        f=Label(error,text="Error sending mail to host").pack(side=BOTTOM, anchor=SE)
        x.quit()
        return False
    finally:
        x.quit()
        error=Tk()
        error.wm_title("SUCCESS")
        f=Label(error,text="Sending mail to host: SUCCESS").pack(side=BOTTOM, anchor=SE)
        return True
    

def folder():#questa fa scegliere la cartella da inviare
    global sendfile
    sendfile=tkFileDialog.askdirectory()



finestra=Tk()
finestra.wm_title("aggiorna file per e-mail")
finestra.minsize(300,120)
sendfile=StringVar()
recipient=StringVar()
subject=StringVar()
body=StringVar()
sendfile.set("")
recipient.set("")
subject.set("")
body.set("")
Label(text="Cartella da inviare:").grid(row=1,column=1)
Button(text="Select", command=folder).grid(row=1,column=2)
Label(text="To:").grid(row=2,column=1)
Entry(textvariable=recipient).grid(row=2,column=2)
Label(text="Subject").grid(row=3,column=1)
Entry(textvariable=subject).grid(row=3,column=2)
Label(text="Body").grid(row=4,column=1)
Entry(textvariable=body).grid(row=4,column=2)
Button(text="Invia", command=funzione).grid(row=4,column=3)
finestra.mainloop()


Maggiori informazioni sulla lista ImoLUG