2017-05-18 3 views
0

どうしたらいいのですか? 私は、Python/Tkinterを持つSMTPのメールシステムを作り、私はこのコードを使用する場合、それは完璧に動作します:Python mailing SMTP

-*- coding: cp1252 -*- 
from Tkinter import * 
import smtplib 
from Tkinter import * 
from email.MIMEMultipart import MIMEMultipart 
from email.MIMEText import MIMEText 

Mafenetre = Tk() 
Mafenetre.title('Gmail')   
Mafenetre.geometry('500x200') 
Mafenetre.configure(bg='white') 

monmail= StringVar() 
sonmail= StringVar() 
monmdp= StringVar() 
sujet= StringVar() 
texte= StringVar() 

LabelQuestion= Label(Mafenetre, text= 'Expéditeur',bg='white',fg='black') 
LabelQuestion.grid(row=0,column=1) 

LabelQuestion= Label(Mafenetre, text= 'Mot de passe',bg='white',fg='black') 
LabelQuestion.grid(row=1,column=1) 

LabelQuestion= Label(Mafenetre, text= 'Destinataire',bg='white',fg='black') 
LabelQuestion.grid(row=2,column=1) 

LabelQuestion= Label(Mafenetre, text= 'Sujet',bg='white',fg='black') 
LabelQuestion.grid(row=3,column=1) 

LabelQuestion= Label(Mafenetre, text= 'Texte',bg='white',fg='black') 
LabelQuestion.grid(row=4,column=1) 


monmail = Entry(Mafenetre, textvariable= monmail, width=70,fg='black',bg='#FEBFD2') 
monmail.grid(row=0,column=2) 

monmdp = Entry(Mafenetre, textvariable= monmdp, width=70, show='*',fg='black',bg='#FEBFD2') 
monmdp.grid(row=1,column=2) 

sonmail = Entry(Mafenetre, textvariable= sonmail, width=70,fg='black',bg='#FEBFD2') 
sonmail.grid(row=2,column=2) 

sujet = Entry(Mafenetre, textvariable= sujet, width=70,fg='black',bg='#FEBFD2') 
sujet.grid(row=3,column=2) 

texte = Entry(Mafenetre, textvariable= texte, width=70,fg='black',bg='#FEBFD2') 
texte.grid(row=4,column=2) 

def Validation() : 
    global monmail 
    global monmdp 
    global sujet 
    global texte 
    global sonmail 
    monmail=str(monmail) 
    monmdp=str(monmdp) 
    sujet=str(sujet) 
    texte=str(texte) 
    sonmail=str(sonmail) 
    server = smtplib.SMTP('smtp.gmail.com:25') 
    server.starttls()       
    server.login(monmail,monmdp) 

    server.sendmail(monmail, sonmail, sujet)  
    server.quit()        

    Reponse= Label(Mafenetre, text='Envoyé avec succès !') 
    Reponse.grid(row=5,column=2) 

Envoyer= Button(Mafenetre, text='Envoyer', command = Validation,bg='white',fg='black') 
Envoyer.grid(row=6,column=2) 

Quitter= Button(Mafenetre, text='Quitter', command = Mafenetre.destroy,bg='white',fg='black') 
Quitter.grid(row=6,column=1) 


Mafenetre.mainloop() 

しかし、私はホームページを追加したいとき(Gmailやオレンジなどの間で選択すること。)それはドン」あなたの助けのための

NameError: global name 'monmail' is not defined 

感謝をし、素敵な一日を持っている:2番目のプログラムで

# -*- coding: cp1252 -*- 
from Tkinter import * 
import smtplib 
from Tkinter import * 
from email.MIMEMultipart import MIMEMultipart 
from email.MIMEText import MIMEText 

index = Tk() 
index.title('Page d\'acceuil')   
index.geometry('425x200')    
index.configure(bg='white') 


def Gmail() : 
    global monmail,sonmail,sujet,texte,monmdp 
    index.destroy() 
    Mafenetre = Tk() 
    Mafenetre.title('Gmail')   
    Mafenetre.geometry('500x200') 
    Mafenetre.configure(bg='white') 

    monmail= StringVar() 
    sonmail= StringVar() 
    monmdp= StringVar() 
    sujet= StringVar() 
    texte= StringVar() 

    LabelQuestion= Label(Mafenetre, text= 'Expéditeur',bg='white',fg='black') 
    LabelQuestion.grid(row=0,column=1) 

    LabelQuestion= Label(Mafenetre, text= 'Mot de passe',bg='white',fg='black') 
    LabelQuestion.grid(row=1,column=1) 

    LabelQuestion= Label(Mafenetre, text= 'Destinataire',bg='white',fg='black') 
    LabelQuestion.grid(row=2,column=1) 

    LabelQuestion= Label(Mafenetre, text= 'Sujet',bg='white',fg='black') 
    LabelQuestion.grid(row=3,column=1) 

    LabelQuestion= Label(Mafenetre, text= 'Texte',bg='white',fg='black') 
    LabelQuestion.grid(row=4,column=1) 

    monmail = Entry(Mafenetre, textvariable= monmail, width=70,fg='black',bg='#FEBFD2') 
    monmail.grid(row=0,column=2) 

    monmdp = Entry(Mafenetre, textvariable= monmdp, width=70, show='*',fg='black',bg='#FEBFD2') 
    monmdp.grid(row=1,column=2) 

    sonmail = Entry(Mafenetre, textvariable= sonmail, width=70,fg='black',bg='#FEBFD2') 
    sonmail.grid(row=2,column=2) 

    sujet = Entry(Mafenetre, textvariable= sujet, width=70,fg='black',bg='#FEBFD2') 
    sujet.grid(row=3,column=2) 

    texte = Entry(Mafenetre, textvariable= texte, width=70,fg='black',bg='#FEBFD2') 
    texte.grid(row=4,column=2) 

    def Validation() : 
     global monmail,monmdp,sonmail,sujet,texte 
     monmail=str(monmail) 
     monmdp=str(monmdp) 
     sujet=str(sujet) 
     texte=str(texte) 
     sonmail=str(sonmail) 
     server = smtplib.SMTP('smtp.gmail.com:25') #Connexion au serveur smtp 
     server.starttls()       #Vérification de l'authentification 
     server.login(monmail,monmdp)    #Connexion avec les identifiants renseignés 

     server.sendmail(monmail, sonmail, sujet) 
     server.quit()        

     Reponse= Label(Mafenetre, text='Envoyé avec succès !') 
     Reponse.grid(row=5,column=2) 

    Envoyer= Button(Mafenetre, text='Envoyer', command = Validation,bg='white',fg='black') 
    Envoyer.grid(row=6,column=2) 

    Quitter= Button(Mafenetre, text='Quitter', command = Mafenetre.destroy,bg='white',fg='black') 
    Quitter.grid(row=6,column=1) 


LabelIndex= Label(index, text= 'Bienvenue sur votre envoi de mails personalisé.\n Pour continuer, merci de choisir votre nom de domaine en cliquant ci-dessous',bg='white',fg='black') 
LabelIndex.grid(row=0,column=1) 

BoutonGmail = Button(index, text='Gmail', command = Gmail,bg='blue',fg='white') 

BoutonGmail.grid(row=1,column=1) 

index.mainloop() 

私はこのエラーを持っている:tは、私は関数内のコピー/貼り付けをした場合でも動作します。

+0

のように外にそれを定義するだけ起こるのを待っている災害です。特にあなたがこのように多くの再交渉をしているとき。しかし、少なくとも完全なスタックトレースを投稿することはできます – e4c5

+0

私はPythonの世界では初心者ですがご意見ありがとうございます –

答えて

0
global monmail 
def Validation() : 
    # declare it inside function 

ほど多くのグローバルを使うmonmail=''

+0

私が正しい場合、関数 ? –

+0

はい、通常はグローバルに使用しないで関数内に宣言することができます – Exprator

+0

あなたは大歓迎ですが、upvoteを忘れてansを確認しないでください – Exprator

関連する問題