Member Avatar for sravan953

Hey guys,

I have made a small program called 'Py-mailer' which allows you to login with your Gmail credentials and send text-only messages to anyone. I uploaded it on SourceForge( http://www.tinyurl.com/m4ans4).

The next step I had to take was to make a GUI for it. So, I started with wxPython:

import wx

window=wx.App()

class pymailer(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None,-1,"Pymailer",size=(500,500))

        panel=wx.Panel(self,-1)

        menu=wx.MenuBar()
        items=wx.Menu()
        
        items.Append(201,"Quit")
        self.Bind(wx.EVT_LEFT_DOWN,self.Quit,id=201)

        menu.Append(items,"File")
        self.SetMenuBar(menu)

        wx.StaticText(panel,-1,"Please enter your Gmail login ID: ",pos=(10,10))
        wx.StaticText(panel,-1,"Please enter your Gmail login password:\n(will not be stored)",pos=(10,40))

        username=wx.TextCtrl(panel,101,"Login ID",pos=(220,10))
        password=wx.TextCtrl(panel,102,"Password",pos=(220,40))

        self.Centre()
        self.Show()

    def Quit(self,event):
        self.Close()    

pymailer()
window.MainLoop()

But, on clicking 'Quit' in the 'File' menu, nothing happens! I think it is because in line 29, I have not passed the ID of the event, so how do I pass the ID? Or, is because of some thing else?

Thanks

You need to bind the event to the menu, not left_click. Try self.Bind(wx.EVT_MENU,self.Quit,id=201) instead of
self.Bind(wx.EVT_LEFT_DOWN,self.Quit,id=201)

Change to this.

items.Append(201,"Quit")
self.Bind(wx.EVT_MENU,self.Quit,id=201)
Member Avatar for sravan953

You need to bind the event to the menu, not left_click. Try self.Bind(wx.EVT_MENU,self.Quit,id=201) instead of
self.Bind(wx.EVT_LEFT_DOWN,self.Quit,id=201)

Thanks mate, it worked! ;)

Member Avatar for sravan953

Thanks snippsat, it worked! ;)

Member Avatar for sravan953

I have advanced till here:

import smtplib
import time
import os
import wx

window=wx.App()

class pymailer(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None,-1,"Pymailer",size=(500,500))

        panel=wx.Panel(self,-1)

        menu=wx.MenuBar()
        items=wx.Menu()
        
        items.Append(201,"Quit")
        self.Bind(wx.EVT_MENU,self.Quit)

        menu.Append(items,"File")
        self.SetMenuBar(menu)

        wx.StaticText(panel,-1,"Welcome to Py-mailer.\nLogin with your Gmail credentials and you can send a text-only email to anyone quickly and easily.",pos=(10,10))
        wx.StaticText(panel,-1,"Please enter your Gmail login ID: ",pos=(10,100))
        wx.StaticText(panel,-1,"Please enter your Gmail login password:\n(will not be stored)",pos=(10,130))

        username=wx.TextCtrl(panel,101,"Login ID",pos=(220,100))
        password=wx.TextCtrl(panel,102,"Password",pos=(220,130))

        login=wx.Button(panel,103,label="Login",pos=(10,170))
        self.Bind(wx.EVT_LEFT_DOWN,self.login)

        self.Centre()
        self.Show()

    def Quit(self,event):
        self.Close()

    def login(self,event):
        s=smtplib.SMTP("smtp.gmail.com",587)
        s.starttls()

        try:
            wx.StaticText(panel,-1,"Logging in...",pos=(10,200))
            s.login(user,passw)
        except:
            wx.StaticText(panel,-1,"Failed",pos=(10,220))

pymailer()
window.MainLoop()

But, after entering the login details and clicking on 'Login', nothing happens. Any help?

Thank you

use self.Bind(wx.EVT_BUTTON, self.login) instead of self.Bind(wx.EVT_LEFT_DOWN,self.login)

also everywhere you put 'panel' put 'self.panel' instead so it can be accessed during login

Member Avatar for sravan953

What's the difference between EVT_BUTTON and EVT_LEFT_DOWN ?

Also, can you explain to me why I should use self.panel ?

EVT_LEFT_DOWN doesn't work because wx.Button doesn't propagate EVT_LEFT_DOWN, it will only propagate wx.EVT_BUTTON. The thing is that objects like Button and ListCtrl will only propagate certain events, not all events. Take a look at the wxpython docs to see what events are used. http://www.wxpython.org/docs/api/wx.Button-class.html

As for using self.panel, in the constructor the variable panel is created, but when the constructor is done, panel is also destroyed. So in the login function, when 'panel' is used it errors because panel was destroyed. Using self.panel will allow access to the variable throughout the class until the class is destroyed. I'm not sure if I'm explaining it well enough, but if there are lots of tutorials, ect. on class structure floating about the web that can help.

Good luck, and I'm looking forward to seeing this in action!

Member Avatar for sravan953

winmic, if I use panel , it gives this error:

Traceback (most recent call last):
File "C:\Documents and Settings\Sravan\My Documents\Sravan\PYTHON\GUI.py", line 52, in Login
wx.StaticText(panel,-1,"Failed",pos=(10,220))
NameError: global name 'panel' is not defined

If I use self.panel , it gives this error:

Traceback (most recent call last):
File "C:\Documents and Settings\Sravan\My Documents\Sravan\PYTHON\GUI.py", line 64, in <module>
pymailer()
File "C:\Documents and Settings\Sravan\My Documents\Sravan\PYTHON\GUI.py", line 36, in __init__
login=wx.Button(self.panel,103,label="Login",pos=(10,170))
AttributeError: 'pymailer' object has no attribute 'panel

Anyways, this is the code till now:

import smtplib
import time
import os
import wx

window=wx.App()

class pymailer(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None,-1,"Pymailer",size=(500,500))

        panel=wx.Panel(self,-1)
        
        menubar=wx.MenuBar()

        filem=wx.Menu()
        filem.Append(201,"Quit")
        self.Bind(wx.EVT_MENU,self.Quit)

        viewm=wx.Menu()
        viewm.Append(202,"About")
        self.Bind(wx.EVT_MENU,self.About)

        menubar.Append(filem,"File")
        menubar.Append(viewm,"Help")

        self.SetMenuBar(menubar)

        wx.StaticText(panel,-1,"Welcome to Py-mailer.\nLogin with your Gmail credentials and you can send a text-only email to anyone quickly and easily.",pos=(10,10))
        wx.StaticText(panel,-1,"Please enter your Gmail login ID: ",pos=(10,100))
        wx.StaticText(panel,-1,"Please enter your Gmail login password:\n(will not be stored)",pos=(10,130))

        username=wx.TextCtrl(panel,101,"Login ID",pos=(220,100))
        password=wx.TextCtrl(panel,102,"Password",pos=(220,130))

        login=wx.Button(self.panel,103,label="Login",pos=(10,170))
        self.Bind(wx.EVT_BUTTON,self.Login)

        self.Centre()
        self.Show()

    def Quit(self,event):
        self.Close()

    def Login(self,event):
        s=smtplib.SMTP("smtp.gmail.com",587)
        s.starttls()
        try:
            wx.StaticText(panel,-1,"Logging in...",pos=(10,200))
            s.login(user,passw)
        except:
            wx.StaticText(panel,-1,"Failed",pos=(10,220))

    def About(self,event):
        about=wx.AboutDialogInfo()

        about.SetName("Py-Mailer")
        about.SetCopyright("(c) 2009 Sravan")
        about.SetWebSite("http://www.uberpix.wordpress.com")
        about.AddDeveloper("Sravan\nDan")

        wx.AboutBox(about)

pymailer()
window.MainLoop()

Also, when I used self.panel , I replaced panel with self.panel everywhere, just thought I'd let you know!

Any other bugs in the code?

Thanks

This is the code that I have working, not exactly sure why self.panel wouldn't work for you.

import smtplib
import time
import os
import wx

window=wx.App()

class pymailer(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None,-1,"Pymailer",size=(500,500))

        self.panel=wx.Panel(self,-1)
        
        menubar=wx.MenuBar()

        filem=wx.Menu()
        filem.Append(201,"Quit")
        self.Bind(wx.EVT_MENU,self.Quit)

        viewm=wx.Menu()
        viewm.Append(202,"About")
        self.Bind(wx.EVT_MENU,self.About)

        menubar.Append(filem,"File")
        menubar.Append(viewm,"Help")

        self.SetMenuBar(menubar)

        wx.StaticText(self.panel,-1,"Welcome to Py-mailer.\nLogin with your Gmail credentials and you can send a text-only email to anyone quickly and easily.",pos=(10,10))
        wx.StaticText(self.panel,-1,"Please enter your Gmail login ID: ",pos=(10,100))
        wx.StaticText(self.panel,-1,"Please enter your Gmail login password:\n(will not be stored)",pos=(10,130))

        username=wx.TextCtrl(self.panel,101,"Login ID",pos=(220,100))
        password=wx.TextCtrl(self.panel,102,"Password",pos=(220,130))

        login=wx.Button(self.panel,103,label="Login",pos=(10,170))
        self.Bind(wx.EVT_BUTTON,self.Login)

        self.Centre()
        self.Show()

    def Quit(self,event):
        self.Close()

    def Login(self,event):
        s=smtplib.SMTP("smtp.gmail.com",587)
        s.starttls()
        try:
            wx.StaticText(self.panel,-1,"Logging in...",pos=(10,200))
            s.login(user,passw)
        except:
            wx.StaticText(self.panel,-1,"Failed",pos=(10,220))

    def About(self,event):
        about=wx.AboutDialogInfo()

        about.SetName("Py-Mailer")
        about.SetCopyright("(c) 2009 Sravan")
        about.SetWebSite("http://www.uberpix.wordpress.com")
        about.AddDeveloper("Sravan\nDan")

        wx.AboutBox(about)

pymailer()
window.MainLoop()
Member Avatar for sravan953

Hmm...I too don't know why self.panel isn't working. I will get back to you about that.

Now, what should I do to make a button(called 'Next>'), which on being clicked will 'refresh' the screen and display some new contents? I hope you understand me!

So I forgot something with events, you can make them respond to a certain id. If you have tried to File->Quit recently you'll see that it will make the about:dialog appear. If you have objects that propagate the same event then you'll want them id specific. so self.Bind(wx.EVT_MENU,self.Quit) becomes self.Bind(wx.EVT_MENU,self.Quit, id=201). The same thing will need to be done for the about menu event, and if you add another button the button event will need to be id specific.

How to add new content really depends on specifically what you want to do. I'm guessing that you want to have completely new content where all old content is destroyed, then you would probably want to create a new wx.Panel and content. I could help a little more if you were a little more specific.

BTW, have you thought about how your going to get the username and password out of the TextCtrl?

BTW, have you thought about how your going to get the username and password out of the TextCtrl?

It shouldn't be to hard, have a look at the API for the wx.TextCTRL
http://www.wxpython.org/docs/api/wx.TextCtrl-class.html
One of the functon is GetValue() that returns a string of the value in the textctrl, simple :)

Member Avatar for sravan953

I don't think getting the values will be a problem, because I've heard of GetValues() and Google it for help.

The problem is: the self.panel doesn't at all work...

One more question:

viewm=wx.Menu()
viewm.Append(202,"About")        self.Bind(wx.EVT_MENU,self.About,id=302)

def About(self,event,id=302):
    about=wx.AboutDialogInfo()

-but it doesn't work. How do I specify the ID when defining the function?

Thanks

I don't think getting the values will be a problem, because I've heard of GetValues() and Google it for help.

The problem is: the self.panel doesn't at all work...

One more question:

viewm=wx.Menu()
viewm.Append(202,"About")        self.Bind(wx.EVT_MENU,self.About,id=302)

def About(self,event,id=302):
    about=wx.AboutDialogInfo()

-but it doesn't work. How do I specify the ID when defining the function?

Thanks

You don't need the id as a method parameter, but at least try to match the id in the lines above, 202 is not the same as 302.

self.panel will work if you use it throughout the class! Search for 'panel' with your editor.

Member Avatar for sravan953

You don't need the id as a method parameter, but at least try to match the id in the lines above, 202 is not the same as 302.

self.panel will work if you use it throughout the class! Search for 'panel' with your editor.

I did what you told:

viewm=wx.Menu()
viewm.Append(202,"About")     self.Bind(wx.EVT_MENU,self.About,id=202)

def About(self,event):
about=wx.AboutDialogInfo()

But, only "Quit" works, and "About" doesn't.

Member Avatar for sravan953

Also, if I use self.panel throughout, it says:

Traceback (most recent call last):
  File "C:\Documents and Settings\Sravan\My Documents\Sravan\PYTHON\GUI.py", line 72, in <module>
    pymailer()
  File "C:\Documents and Settings\Sravan\My Documents\Sravan\PYTHON\GUI.py", line 29, in __init__
    wx.StaticText(self.panel,-1,"Welcome to Py-mailer.\nLogin with your Gmail credentials and you can send a text-only email to anyone quickly and easily.",pos=(10,10))
AttributeError: 'pymailer' object has no attribute 'panel'

Here's the modified code including [icode]self.panel[icode]everywhere

import smtplib
import time
import os
import wx

window=wx.App()

class pymailer(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None,-1,"Py-mailer",size=(500,500))

        panel=wx.Panel(self,-1)

        menubar=wx.MenuBar()

        filem=wx.Menu()
        filem.Append(201,"Quit")
        self.Bind(wx.EVT_MENU,self.Quit,id=201)

        viewm=wx.Menu()
        viewm.Append(202,"About")
        self.Bind(wx.EVT_MENU,self.About,id=202)

        menubar.Append(filem,"File")
        menubar.Append(viewm,"Help")

        self.SetMenuBar(menubar)

        wx.StaticText(self.panel,-1,"Welcome to Py-mailer.\nLogin with your Gmail credentials and you can send a text-only email to anyone quickly and easily.",pos=(10,10))
        wx.StaticText(self.panel,-1,"Please enter your Gmail login ID: ",pos=(10,100))
        wx.StaticText(self.panel,-1,"Please enter your Gmail login password:\n(will not be stored)",pos=(10,130))

        username=wx.TextCtrl(self.panel,101,"Login ID",pos=(220,100))
        password=wx.TextCtrl(self.panel,102,"Password",pos=(220,130))

        login=wx.Button(self.panel,103,label="Login",pos=(10,170))
        self.Bind(wx.EVT_BUTTON,self.Login,id=103)

        wx.StaticText(self.panel,-1,"To:",pos=(10,220))
        wx.StaticText(self.panel,-1,"Subject:",pos=(10,250))
        wx.StaticText(self.panel,-1,"Message:",pos=(10,280))

        to=wx.TextCtrl(self.panel,103,"<username@domain.server>",pos=(80,220),size=(240,20))
        subject=wx.TextCtrl(self.panel,104,pos=(80,250),size=(240,20))
        message=wx.TextCtrl(self.panel,105,pos=(80,280),size=(240,150))

        self.Centre()
        self.Show()

    def Quit(self,event):
        self.Close()

    def About(self,event):
        about=wx.AboutDialogInfo()

    def Login(self,event,):
        wx.StaticText(panel,-1,"Logging in...",pos=(10,200))
        s=smtplib.SMTP("smtp.gmail.com",587)
        s.starttls()
        try:
            s.login(user,passw)
        except:
            wx.StaticText(self.panel,-1,"Failed",pos=(10,200))

        about.SetName("Py-Mailer")
        about.SetCopyright("(c) 2009 Sravan")
        about.SetWebSite("http://www.uberpix.wordpress.com")
        about.AddDeveloper("Sravan\nDan")

        wx.AboutBox(about)

pymailer()
window.MainLoop()

self.panel everywhere except the most important line 12

panel=wx.Panel(self,-1)
needs to be
self.panel=wx.Panel(self,-1)

Member Avatar for sravan953

Till now:

import smtplib
import time
import os
import wx

window=wx.App()

s=smtplib.SMTP("smtp.gmail.com",587)
s.starttls()

class pymailer(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None,-1,"Py-mailer",size=(500,700))

        self.panel=wx.Panel(self,-1)
        
        menubar=wx.MenuBar()

        filem=wx.Menu()
        filem.Append(201,"Quit")
        self.Bind(wx.EVT_MENU,self.Quit,id=201)

        viewm=wx.Menu()
        viewm.Append(202,"About")
        self.Bind(wx.EVT_MENU,self.About,id=202)

        menubar.Append(filem,"File")
        menubar.Append(viewm,"Help")

        self.SetMenuBar(menubar)

        wx.StaticText(self.panel,-1,"Welcome to Py-mailer.\nLogin with your Gmail credentials and you can send a text-only email to anyone quickly and easily.",pos=(10,10))
        wx.StaticText(self.panel,-1,"Please enter your Gmail login ID: ",pos=(10,100))
        wx.StaticText(self.panel,-1,"Please enter your Gmail login password:\n(will not be stored)",pos=(10,130))

        username=wx.TextCtrl(self.panel,101,"Login ID",pos=(220,100))
        user=username.GetValue()
        password=wx.TextCtrl(self.panel,102,"Password",pos=(220,130))
        passw=password.GetValue()

        login=wx.Button(self.panel,103,label="Login",pos=(10,170))
        self.Bind(wx.EVT_BUTTON,self.Login,id=103)
        
        wx.StaticText(self.panel,-1,"To:",pos=(10,250))
        wx.StaticText(self.panel,-1,"Subject:",pos=(10,280))
        wx.StaticText(self.panel,-1,"Message:",pos=(10,310))

        toadd=wx.TextCtrl(self.panel,104,"<username@domain.server>",pos=(80,250),size=(240,20))
        to=toadd.GetValue()
        subj=wx.TextCtrl(self.panel,105,pos=(80,280),size=(240,20))
        subject=subj.GetValue()
        mess=wx.TextCtrl(self.panel,106,pos=(80,310),size=(240,150))
        message=mess.GetValue()

        send=wx.Button(self.panel,107,"Send",pos=(245,470))
        self.Bind(wx.EVT_BUTTON,self.Send,id=107)

        self.Centre()
        self.Show()

    def Quit(self,event):
        self.Close()
    
    def About(self,event):
        about=wx.AboutDialogInfo()
        
        about.SetName("Py-Mailer")
        about.SetCopyright("(c) 2009 Sravan")
        about.SetWebSite("http://www.uberpix.wordpress.com")
        about.AddDeveloper("Sravan\nDan")

        wx.AboutBox(about)

    def Login(self,event):
        try:
            s.login(user,passw)
            wx.StaticText(self.panel,-1,"Logged in",pos=(10,200))
        except:
            wx.StaticText(self.panel,-1,"Failed",pos=(10,200))

    def Send(self,event):
        msg="To: "+to+"\nSubject: "+subject+"\n"+message
        s.sendmail(us,to,msg)
        s.quit()

pymailer()
window.MainLoop()

No matter what I enter, it doesn't login and says "Failed". Why?
Also, how do I mask the password input?

Member Avatar for sravan953

Also, on clicking "Send" after filling up everything correctly, it shows this error:

Traceback (most recent call last):
File "C:\Documents and Settings\sravan953\My Documents\Sravan\PYTHON\GUI.py", line 85, in Send
msg="To: "+to+"\nSubject: "+subject+"\n"+message
NameError: global name 'to' is not defined

What should I do to solve this? Can anyone please explain to me about the scope of the variables which are causing the error?

msg="To: "+to+"\nSubject: "+subject+"\n"+message

There a are variable names to and subject that need some kind of string value. Those things do not come out of thin air!

Member Avatar for sravan953
wx.StaticText(self.panel,-1,"To:",pos=(10,250))
        wx.StaticText(self.panel,-1,"Subject:",pos=(10,280))
        wx.StaticText(self.panel,-1,"Message:",pos=(10,310))        toadd=wx.TextCtrl(self.panel,104,"<username@domain.server>",pos=(80,250),size=(240,20))
        to=toadd.GetValue()
        subj=wx.TextCtrl(self.panel,105,pos=(80,280),size=(240,20))
        subject=subj.GetValue()
        mess=wx.TextCtrl(self.panel,106,pos=(80,310),size=(240,150))
        message=mess.GetValue()
    def Send(self,event):
        msg="To: "+to+"\nSubject: "+subject+"\n"+message
        s.sendmail(us,to,msg)
        s.quit()

I have to in line 4. Even then it says that the global variable isn't defined!

Member Avatar for sravan953

I defined user,passw,to,subject,message in the class to make it available to all functions, but it says variable not defined, so I had to define everything outside the class like this:

import smtplib
import time
import os
import wx

window=wx.App()

s=smtplib.SMTP("smtp.gmail.com",587)
s.starttls()

info={}
user=""
passw=""

class pymailer(wx.Frame):
    
    def __init__(self):
        wx.Frame.__init__(self,None,-1,"Py-mailer",size=(500,700))

        self.panel=wx.Panel(self,-1)
        
        menubar=wx.MenuBar()

        filem=wx.Menu()
        filem.Append(201,"Quit")
        self.Bind(wx.EVT_MENU,self.Quit,id=201)

        viewm=wx.Menu()
        viewm.Append(202,"About")
        self.Bind(wx.EVT_MENU,self.About,id=202)

        menubar.Append(filem,"File")
        menubar.Append(viewm,"Help")

        self.SetMenuBar(menubar)

        wx.StaticText(self.panel,-1,"Welcome to Py-mailer.\nLogin with your Gmail credentials and you can send a text-only email to anyone quickly and easily.",pos=(10,10))
        wx.StaticText(self.panel,-1,"Please enter your Gmail login ID: ",pos=(10,100))
        wx.StaticText(self.panel,-1,"Please enter your Gmail login password:\n(will not be stored)",pos=(10,130))

        username=wx.TextCtrl(self.panel,101,"Login ID",pos=(220,100))
        user=username.GetValue()
        
        password=wx.TextCtrl(self.panel,102,"Password",pos=(220,130))
        passw=password.GetValue()

        login=wx.Button(self.panel,103,label="Login",pos=(10,170))
        self.Bind(wx.EVT_BUTTON,self.Login,id=103)
        
        wx.StaticText(self.panel,-1,"To:",pos=(10,250))
        wx.StaticText(self.panel,-1,"Subject:",pos=(10,280))
        wx.StaticText(self.panel,-1,"Message:",pos=(10,310))

        to_info=wx.TextCtrl(self.panel,104,pos=(80,250),size=(240,20))
        info["To"]=to_info.GetValue()
        
        sbj_info=wx.TextCtrl(self.panel,105,pos=(80,280),size=(240,20))
        info["Subject"]=sbj_info.GetValue()
        
        msg_info=wx.TextCtrl(self.panel,106,pos=(80,310),size=(240,150))
        info["Message"]=msg_info.GetValue()

        send=wx.Button(self.panel,107,"Send",pos=(245,470))
        self.Bind(wx.EVT_BUTTON,self.Send,id=107)

        self.Centre()
        self.Show()

    def Quit(self,event):
        self.Close()
    
    def About(self,event):
        about=wx.AboutDialogInfo()
        
        about.SetName("Py-Mailer")
        about.SetCopyright("(c) 2009 Sravan")
        about.SetWebSite("http://www.uberpix.wordpress.com")
        about.AddDeveloper("Sravan")
        about.AddDeveloper("Dan")

        wx.AboutBox(about)

    def Login(self,event):
        try:
            s.login(user,passw)
            wx.StaticText(self.panel,-1,"Logged in...",pos=(10,200))
        except:
            wx.StaticText(self.panel,-1,"Failed",pos=(10,200))

    def Send(self,event):
        to=info["To"]
        subject=info["Subject"]
        message=info["Message"]
        msg="To: "+to+"\nSubject: "+subject+"\n"+message
        s.sendmail(user,to,msg)
        s.quit()

pymailer()
window.MainLoop()

Why doesn't it work if I define the variables in the class?

They won't work inside the class unless you put 'self.' in front of them. If you just use the variable user inside __init__ user is destroyed at the end of __init__. What you'll want to do is use self.user to make it a class variable and not a function variable. Then you won't need to make them global.

Member Avatar for sravan953

Here are the first few lines, I did as you told:

class pymailer(wx.Frame):

    self.info={}
    self.user=""
    self.passw=""
    
    def __init__(self):
.
.
.
password=wx.TextCtrl(self.panel,102,"Password",pos=(220,130))
        self.passw=password.GetValue()

But it still doesn't work...

What do you mean? Does it have an error message or does it not work as expected?

Member Avatar for sravan953

This is the full code for reference:

import smtplib
import time
import os
import wx

window=wx.App()

s=smtplib.SMTP("smtp.gmail.com",587)
s.starttls()

class pymailer(wx.Frame):
    
    def __init__(self):
        wx.Frame.__init__(self,None,-1,"Py-mailer",size=(500,700))

        self.info={}
        self.user=""
        self.passw=""
        self.to=""
        self.subject=""
        self.message=""

        self.panel=wx.Panel(self,-1)
        
        menubar=wx.MenuBar()

        filem=wx.Menu()
        filem.Append(201,"Quit")
        self.Bind(wx.EVT_MENU,self.Quit,id=201)

        viewm=wx.Menu()
        viewm.Append(202,"About")
        self.Bind(wx.EVT_MENU,self.About,id=202)

        menubar.Append(filem,"File")
        menubar.Append(viewm,"Help")

        self.SetMenuBar(menubar)

        wx.StaticText(self.panel,-1,"Welcome to Py-mailer.\nLogin with your Gmail credentials and you can send a text-only email to anyone quickly and easily.",pos=(10,10))
        wx.StaticText(self.panel,-1,"Please enter your Gmail login ID: ",pos=(10,100))
        wx.StaticText(self.panel,-1,"Please enter your Gmail login password:\n(will not be stored)",pos=(10,130))

        username=wx.TextCtrl(self.panel,101,"Login ID",pos=(220,100))
        self.user=username.GetValue()
        
        password=wx.TextCtrl(self.panel,102,"Password",pos=(220,130))
        self.passw=password.GetValue()

        login=wx.Button(self.panel,103,label="Login",pos=(10,170))
        self.Bind(wx.EVT_BUTTON,self.Login,id=103)
        
        wx.StaticText(self.panel,-1,"To:",pos=(10,250))
        wx.StaticText(self.panel,-1,"Subject:",pos=(10,280))
        wx.StaticText(self.panel,-1,"Message:",pos=(10,310))

        to_info=wx.TextCtrl(self.panel,104,pos=(80,250),size=(240,20))
        self.info["To"]=to_info.GetValue()
        
        sbj_info=wx.TextCtrl(self.panel,105,pos=(80,280),size=(240,20))
        self.info["Subject"]=sbj_info.GetValue()
        
        msg_info=wx.TextCtrl(self.panel,106,pos=(80,310),size=(240,150))
        self.info["Message"]=msg_info.GetValue()

        send=wx.Button(self.panel,107,"Send",pos=(245,470))
        self.Bind(wx.EVT_BUTTON,self.Send,id=107)

        self.Centre()
        self.Show()

    def Quit(self,event):
        self.Close()
    
    def About(self,event):
        about=wx.AboutDialogInfo()
        
        about.SetName("Py-Mailer")
        about.SetCopyright("(c) 2009 Sravan")
        about.SetWebSite("http://www.uberpix.wordpress.com")
        about.AddDeveloper("Sravan")
        about.AddDeveloper("Dan")

        wx.AboutBox(about)

    def Login(self,event):
        try:
            s.login(user,passw)
            wx.StaticText(self.panel,-1,"Logged in...",pos=(10,200))
        except:
            wx.StaticText(self.panel,-1,"Failed",pos=(10,200))

    def Send(self,event):
        to=self.info["To"]
        subject=self.info["Subject"]
        message=self.info["Message"]
        msg="To: "+to+"\nSubject: "+subject+"\n"+message
        s.sendmail(user,to,msg)
        s.quit()

pymailer()
window.MainLoop()

On running, after entering all details (Login--> 'Failed', don't know why) and clicking on 'Send' it says:

Traceback (most recent call last):
File "C:\Documents and Settings\sravan953\My Documents\Sravan\PYTHON\GUI.py", line 98, in Send
s.sendmail(user,to,msg)
NameError: global name 'user' is not defined

Well, there are two problems.

1) in line 98, user should be self.user which will solve the error message. in line 88 user, passw should be self.user, self.passw

2) After fixing those two lines, try printing self.user and self.passw in the login function. When I did that it said that self.user is 'Login ID' and self.passw is 'Password' even after changing the user and password. The problem lies in where you GetValue(), you have it in __init__ where it will get the starting values when it should be in the functions that use it so user input is read.

Here's the code that seems to be working, with the changes from above. I've also added style=wx.TE_PASSWORD in line 46 to mask the password.

import smtplib
import time
import os
import wx

window=wx.App()

s=smtplib.SMTP("smtp.gmail.com",587)
s.starttls()

class pymailer(wx.Frame):
    
    def __init__(self):
        wx.Frame.__init__(self,None,-1,"Py-mailer",size=(500,700))

        self.info={}
        self.user=""
        self.passw=""
        self.to=""
        self.subject=""
        self.message=""

        self.panel=wx.Panel(self,-1)
        
        menubar=wx.MenuBar()

        filem=wx.Menu()
        filem.Append(201,"Quit")
        self.Bind(wx.EVT_MENU,self.Quit,id=201)

        viewm=wx.Menu()
        viewm.Append(202,"About")
        self.Bind(wx.EVT_MENU,self.About,id=202)

        menubar.Append(filem,"File")
        menubar.Append(viewm,"Help")

        self.SetMenuBar(menubar)

        wx.StaticText(self.panel,-1,"Welcome to Py-mailer.\nLogin with your Gmail credentials and you can send a text-only email to anyone quickly and easily.",pos=(10,10))
        wx.StaticText(self.panel,-1,"Please enter your Gmail login ID: ",pos=(10,100))
        wx.StaticText(self.panel,-1,"Please enter your Gmail login password:\n(will not be stored)",pos=(10,130))

        self.username=wx.TextCtrl(self.panel,101,"Login ID",pos=(220,100))
        
        self.password=wx.TextCtrl(self.panel,102,"Password",pos=(220,130), style=wx.TE_PASSWORD)

        login=wx.Button(self.panel,103,label="Login",pos=(10,170))
        self.Bind(wx.EVT_BUTTON,self.Login,id=103)
        
        wx.StaticText(self.panel,-1,"To:",pos=(10,250))
        wx.StaticText(self.panel,-1,"Subject:",pos=(10,280))
        wx.StaticText(self.panel,-1,"Message:",pos=(10,310))

        self.to_info=wx.TextCtrl(self.panel,104,pos=(80,250),size=(240,20))
        
        self.sbj_info=wx.TextCtrl(self.panel,105,pos=(80,280),size=(240,20))
     
        self.msg_info=wx.TextCtrl(self.panel,106,pos=(80,310),size=(240,150))

        send=wx.Button(self.panel,107,"Send",pos=(245,470))
        self.Bind(wx.EVT_BUTTON,self.Send,id=107)

        self.Centre()
        self.Show()

    def Quit(self,event):
        self.Close()
    
    def About(self,event):
        about=wx.AboutDialogInfo()
        
        about.SetName("Py-Mailer")
        about.SetCopyright("(c) 2009 Sravan")
        about.SetWebSite("http://www.uberpix.wordpress.com")
        about.AddDeveloper("Sravan")
        about.AddDeveloper("Dan")

        wx.AboutBox(about)

    def Login(self,event):
        self.user=self.username.GetValue()
        self.passw=self.password.GetValue()
        try:
            s.login(self.user,self.passw)
            wx.StaticText(self.panel,-1,"Logged in...",pos=(10,200))
        except:
            wx.StaticText(self.panel,-1,"Failed",pos=(10,200))

    def Send(self,event):
        self.info["To"]=self.to_info.GetValue()
        self.info["Subject"]=self.sbj_info.GetValue()
        self.info["Message"]=self.msg_info.GetValue()
        to=self.info["To"]
        subject=self.info["Subject"]
        message=self.info["Message"]
        msg="To: "+to+"\nSubject: "+subject+"\n"+message
        s.sendmail(self.user,to,msg)
        s.quit()

pymailer()
window.MainLoop()
Member Avatar for sravan953

Thanks, I will surely make the corrections try it out. And, in wx.TE_PASSWORD , what does TE stand for?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.