Member Avatar for sravan953

Hey All,

I have some code here, the problem is in line 82-83:

import smtplib
import wx

window=wx.App()

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

        try:
            self.s=smtplib.SMTP("smtp.gmail.com",587)
            self.s.starttls()
        except:
            wx.MessageBox("Error 001: Could not connect to the internet, please try again later.","Internet Connection Error")

        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.\n\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",style=(wx.TE_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,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.sub_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("Daniel Pacheco")

        wx.AboutBox(about)

    def Login(self,event):

        self.user=self.username.GetValue()
        self.passw=self.password.GetValue()

        if(self.user=="" and self.passw==""):
            wx.MessageBox("Error 002: You did not enter your username and/or password","Enter Login Details")

        try:
            self.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.to=self.to_info.GetValue()
        self.subject=self.sub_info.GetValue()
        self.message=self.msg_info.GetValue()
        self.msg="To: "+self.to+"\nSubject: "+self.subject+"\n"+self.message
        self.s.sendmail(self.user,self.to,self.msg)
        self.s.quit()        

        wx.StaticText(self.panel,-1,"Your message has been sent!",pos=(175,500))        

pymailer()
window.MainLoop()

- because once I login successfully, if I again login with wrong details, the "Failed" text doesn't overwrite the "Logged in..." text fully.... so what do I do?

Thanks a load

Recommended Answers

All 8 Replies

How about instead of just generating an arbitrary StaticText object, assign it to a persistent member of your class (let's call it self.login_status_text ). Then instead of generating it each time, simply update the contents to the proper string (It's been years since messing with wxPython but if I remember correctly you might want to use SetLabel in this case).

You can set size so the text get overwrite.
And som color look better.

try:
    self.s.login(self.user,self.passw)
    wx.StaticText(self.panel,-1,"Logged in...",pos=(10,200),size = (150,20)).SetForegroundColour('blue')         
except:
    wx.StaticText(self.panel,-1,"Failed",pos=(10,200),size = (150,20)).SetForegroundColour('red')

For me i have to have this put in this lines or it dont work for me.

self.s.ehlo()
self.s.starttls()
self.s.ehlo()

And your design need some work,can give som tips later if you want that.

Member Avatar for sravan953

You can set size so the text get overwrite.
And som color look better.

try:
    self.s.login(self.user,self.passw)
    wx.StaticText(self.panel,-1,"Logged in...",pos=(10,200),size = (150,20)).SetForegroundColour('blue')         
except:
    wx.StaticText(self.panel,-1,"Failed",pos=(10,200),size = (150,20)).SetForegroundColour('red')

For me i have to have this put in this lines or it dont work for me.

self.s.ehlo()
self.s.starttls()
self.s.ehlo()

And your design need some work,can give som tips later if you want that.

I would be glad to get some tips from you!

Member Avatar for sravan953

How about instead of just generating an arbitrary StaticText object, assign it to a persistent member of your class (let's call it self.login_status_text ). Then instead of generating it each time, simply update the contents to the proper string (It's been years since messing with wxPython but if I remember correctly you might want to use SetLabel in this case).

I changed lines 81-85 to this:

try:
            self.s.login(self.user,self.passw)
            self.loginmsg.SetLabel("Logged in...")
        except:
            self.loginmsg.SetLabel("Failed to login, please try again...")

But it still doesn't overwrite properly... guess I will just have to settle on snippsat's opinion...

Hi here are som changes.
Done some work on this,i was thinking making somthing simelar as this for fun.
So helping you a little is no problem,set my self as co-developer in about box if you dont mind.

Here are changes.

----------| Change log |---------

* Change on size and design | New headerpicture

* Take away maximize window (wx.MINIMIZE_BOX)

* Ico for Frame and about box

* New about box style

* Picture buttons

* Bigger message Box and multiline enable(wx.TE_MULTILINE)

* Exception handling on mail sent with new StaticText message

* Better documentation of code

----------| idèe for future development |--------

* Email attachment(file)

* Contact list load

* Save default login

http://www.esnips.com/doc/c2e05e06-508b-4dbd-a678-110819cb05ed/py-mailer

import smtplib
import time
import os
import wx
from wx.lib.wordwrap import wordwrap

window = wx.App()
class pymailer(wx.Frame):
    '''
    Program to send gmail message    
    '''    
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY,"Py-mailer",size=(330,520),
                        style = wx.DEFAULT_DIALOG_STYLE | wx.MINIMIZE_BOX)        
        #----| Window color |---#
        self.SetBackgroundColour("#d8d8bf")       
        
        #---| Add a panel so it looks the correct on all platforms |---#  
        self.panel = wx.Panel(self, wx.ID_ANY)
        
        #---| Ico for window |---#  
        ico = wx.Icon('email1.ico', wx.BITMAP_TYPE_ICO)
        self.SetIcon(ico)
        
        #----| Gmail server |---#
        self.info = {}
        self.s = smtplib.SMTP("smtp.gmail.com",587)
        self.s.ehlo()
        self.s.starttls()
        self.s.ehlo()       
        
        #---| Menu bar |---# 
        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)        
        
         #---| Picture Buttons |---# 
        self.send = wx.BitmapButton(self.panel,107,wx.Bitmap("py_email1.png"),
                                       pos = (7,430), size = (65,26))
        self.Bind(wx.EVT_BUTTON,self.Send,id = 107)        
        self.login = wx.BitmapButton(self.panel,103,wx.Bitmap("py_email2.png"),
                                       pos = (7,123), size = (65,26))
        self.Bind(wx.EVT_BUTTON,self.Login,id = 103)
        
        #---| Standar Buttons |---#  
        #login = wx.Button(self.panel,103,label = "Login", pos = (10,120))
        #self.Bind(wx.EVT_BUTTON,self.Login,id = 103)
        #send = wx.Button(self.panel, 107, "Send", pos = (50,400))
        #self.Bind(wx.EVT_BUTTON, self.Send,id = 107)    
        
        #---| Picture |---# 
        self.bitmap_1 = wx.StaticBitmap(self, -1, wx.Bitmap("py1.png",
                          wx.BITMAP_TYPE_ANY),pos = (43,5 ), size = (250,42))        
        
        #---| Label Text |---#
        wx.StaticText(self, -1, '---------------------------------------------------------------------------',
                                                pos = (10,107), size = (400,13 )).SetForegroundColour('blue')
        wx.StaticText(self, -1, '---------------------------------------------------------------------------',
                                                 pos = (10,35), size = (400,13 )).SetForegroundColour('blue')
        wx.StaticText(self, -1, '---------------------------------------------------------------------------',
                                                 pos = (10,150), size = (400,13 )).SetForegroundColour('blue')        
        #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,"Enter your Gmail login ID: ",pos = (10,55))
        wx.StaticText(self.panel,-1,"Enter your Gmail password:\n(will not be stored)",pos = (10,80))
        wx.StaticText(self.panel,-1,"To:",pos = (10,180))
        wx.StaticText(self.panel,-1,"Subject:",pos = (10,210))
        wx.StaticText(self.panel,-1,"Message",pos = (135,240)).SetForegroundColour('brown')         

        #----| TextBox |---# 
        self.username = wx.TextCtrl(self.panel,101,"tomlaa1@gmail.com",pos = (150,50),size = (160,20))        
        self.password = wx.TextCtrl(self.panel,102,"qqqqqq11",style = (wx.TE_PASSWORD), pos = (150,80),size = (160,20))
        self.to_info = wx.TextCtrl(self.panel,104,pos = (70,180),size = (240,20))       
        self.sbj_info = wx.TextCtrl(self.panel,105,pos = (70,210),size = (240,20))        
        self.msg_info = wx.TextCtrl(self.panel,106,style = wx.TE_MULTILINE,pos = (13,265),size = (300,150))    

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

    def Login(self,event):
        self.user=self.username.GetValue()
        self.passw=self.password.GetValue()
        try:
            self.s.login(self.user,self.passw)
            wx.StaticText(self.panel,-1,"You are logged in...",pos = (130,127)).SetForegroundColour('blue') 
        except:
            wx.StaticText(self.panel,-1,"Login in failed",pos = (100,125)).SetForegroundColour('red')

    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()        
        
        self.to=self.info["To"]
        self.subject = self.info["Subject"]
        self.message = self.info["Message"]
        self.msg = "To: " + self.to + "\nSubject: " + self.subject+"\n" + self.message
        
        try:            
            self.s.sendmail(self.user,self.to,self.msg)
            wx.StaticText(self.panel,-1,"Mail send successfully",pos = (120,435)).SetForegroundColour('blue') 
            self.s.quit()
        except:
            wx.StaticText(self.panel,-1,"Mail not sent",pos = (120,435)).SetForegroundColour('red')            
            
    def About(self,event):        
       '''
        Show about window
       '''        
       info = wx.AboutDialogInfo()
       info.Name = "Py-Mailer"
       info.Version = "version ?"
       info.Copyright = ""
       info.Description = wordwrap(
       'info about this program......' ,
       350, wx.ClientDC(self))       
       info.WebSite = ("http://www.uberpix.wordpress.com", "Sravan home")      
       info.Developers = [ "Dan Sravan\n"
                            'Co-developer:\n'
                            'Snippsat',]
       info.License = wordwrap(licenseText, 500, wx.ClientDC(self))     
       wx.AboutBox(info)
licenseText = 'Copyright(c) 2009 Sravan'     

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

Hi here are som changes.
Done some work on this,i was thinking making somthing simelar as this for fun.
So helping you a little is no problem,set my self as co-developer in about box if you dont mind.

Here are changes.

----------| Change log |---------

* Change on size and design | New headerpicture

* Take away maximize window (wx.MINIMIZE_BOX)

* Ico for Frame and about box

* New about box style

* Picture buttons

* Bigger message Box and multiline enable(wx.TE_MULTILINE)

* Exception handling on mail sent with new StaticText message

* Better documentation of code

----------| idèe for future development |--------

* Email attachment(file)

* Contact list load

* Save default login

http://www.esnips.com/doc/c2e05e06-508b-4dbd-a678-110819cb05ed/py-mailer

import smtplib
import time
import os
import wx
from wx.lib.wordwrap import wordwrap

window = wx.App()
class pymailer(wx.Frame):
    '''
    Program to send gmail message    
    '''    
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY,"Py-mailer",size=(330,520),
                        style = wx.DEFAULT_DIALOG_STYLE | wx.MINIMIZE_BOX)        
        #----| Window color |---#
        self.SetBackgroundColour("#d8d8bf")       
        
        #---| Add a panel so it looks the correct on all platforms |---#  
        self.panel = wx.Panel(self, wx.ID_ANY)
        
        #---| Ico for window |---#  
        ico = wx.Icon('email1.ico', wx.BITMAP_TYPE_ICO)
        self.SetIcon(ico)
        
        #----| Gmail server |---#
        self.info = {}
        self.s = smtplib.SMTP("smtp.gmail.com",587)
        self.s.ehlo()
        self.s.starttls()
        self.s.ehlo()       
        
        #---| Menu bar |---# 
        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)        
        
         #---| Picture Buttons |---# 
        self.send = wx.BitmapButton(self.panel,107,wx.Bitmap("py_email1.png"),
                                       pos = (7,430), size = (65,26))
        self.Bind(wx.EVT_BUTTON,self.Send,id = 107)        
        self.login = wx.BitmapButton(self.panel,103,wx.Bitmap("py_email2.png"),
                                       pos = (7,123), size = (65,26))
        self.Bind(wx.EVT_BUTTON,self.Login,id = 103)
        
        #---| Standar Buttons |---#  
        #login = wx.Button(self.panel,103,label = "Login", pos = (10,120))
        #self.Bind(wx.EVT_BUTTON,self.Login,id = 103)
        #send = wx.Button(self.panel, 107, "Send", pos = (50,400))
        #self.Bind(wx.EVT_BUTTON, self.Send,id = 107)    
        
        #---| Picture |---# 
        self.bitmap_1 = wx.StaticBitmap(self, -1, wx.Bitmap("py1.png",
                          wx.BITMAP_TYPE_ANY),pos = (43,5 ), size = (250,42))        
        
        #---| Label Text |---#
        wx.StaticText(self, -1, '---------------------------------------------------------------------------',
                                                pos = (10,107), size = (400,13 )).SetForegroundColour('blue')
        wx.StaticText(self, -1, '---------------------------------------------------------------------------',
                                                 pos = (10,35), size = (400,13 )).SetForegroundColour('blue')
        wx.StaticText(self, -1, '---------------------------------------------------------------------------',
                                                 pos = (10,150), size = (400,13 )).SetForegroundColour('blue')        
        #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,"Enter your Gmail login ID: ",pos = (10,55))
        wx.StaticText(self.panel,-1,"Enter your Gmail password:\n(will not be stored)",pos = (10,80))
        wx.StaticText(self.panel,-1,"To:",pos = (10,180))
        wx.StaticText(self.panel,-1,"Subject:",pos = (10,210))
        wx.StaticText(self.panel,-1,"Message",pos = (135,240)).SetForegroundColour('brown')         

        #----| TextBox |---# 
        self.username = wx.TextCtrl(self.panel,101,"tomlaa1@gmail.com",pos = (150,50),size = (160,20))        
        self.password = wx.TextCtrl(self.panel,102,"qqqqqq11",style = (wx.TE_PASSWORD), pos = (150,80),size = (160,20))
        self.to_info = wx.TextCtrl(self.panel,104,pos = (70,180),size = (240,20))       
        self.sbj_info = wx.TextCtrl(self.panel,105,pos = (70,210),size = (240,20))        
        self.msg_info = wx.TextCtrl(self.panel,106,style = wx.TE_MULTILINE,pos = (13,265),size = (300,150))    

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

    def Login(self,event):
        self.user=self.username.GetValue()
        self.passw=self.password.GetValue()
        try:
            self.s.login(self.user,self.passw)
            wx.StaticText(self.panel,-1,"You are logged in...",pos = (130,127)).SetForegroundColour('blue') 
        except:
            wx.StaticText(self.panel,-1,"Login in failed",pos = (100,125)).SetForegroundColour('red')

    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()        
        
        self.to=self.info["To"]
        self.subject = self.info["Subject"]
        self.message = self.info["Message"]
        self.msg = "To: " + self.to + "\nSubject: " + self.subject+"\n" + self.message
        
        try:            
            self.s.sendmail(self.user,self.to,self.msg)
            wx.StaticText(self.panel,-1,"Mail send successfully",pos = (120,435)).SetForegroundColour('blue') 
            self.s.quit()
        except:
            wx.StaticText(self.panel,-1,"Mail not sent",pos = (120,435)).SetForegroundColour('red')            
            
    def About(self,event):        
       '''
        Show about window
       '''        
       info = wx.AboutDialogInfo()
       info.Name = "Py-Mailer"
       info.Version = "version ?"
       info.Copyright = ""
       info.Description = wordwrap(
       'info about this program......' ,
       350, wx.ClientDC(self))       
       info.WebSite = ("http://www.uberpix.wordpress.com", "Sravan home")      
       info.Developers = [ "Dan Sravan\n"
                            'Co-developer:\n'
                            'Snippsat',]
       info.License = wordwrap(licenseText, 500, wx.ClientDC(self))     
       wx.AboutBox(info)
licenseText = 'Copyright(c) 2009 Sravan'     

pymailer()
window.MainLoop()

How did you make those images?

How did you make those images?

Du you mean the Attached Thumbnails?
Thats just screenshot capture with free program jing.
http://www.jingproject.com/

Note that the way you use try/except is not very wise.

try:            
            self.s.sendmail(self.user,self.to,self.msg)
            wx.StaticText(self.panel,-1,"Mail send successfully",pos = (120,435)).SetForegroundColour('blue') 
            self.s.quit()
        except:
            wx.StaticText(self.panel,-1,"Mail not sent",pos = (120,435)).SetForegroundColour('red')

You should do something like this instead:

try:            
            self.s.sendmail(self.user,self.to,self.msg)
        except LoginError:
            wx.StaticText(self.panel,-1,"Mail not sent",pos = (120,435)).SetForegroundColour('red')            
        else:
            wx.StaticText(self.panel,-1,"Mail send successfully",pos = (120,435)).SetForegroundColour('blue') 
            self.s.quit()
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.