Python邮件SMTP

2024-04-24 21:32:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我想问你这是怎么发生的。 我用python/tkinter创建了一个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、Orange等中选择)时,即使我在函数中复制/粘贴了一个主页,它也不起作用:

# -*- 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()

对于第二个程序,我有一个错误:

NameError: global name 'monmail' is not defined

谢谢你的帮助,祝你今天愉快。你知道吗


Tags: textcolumngridrowbgblackwhitefg