wxListMailer release update

Python:
2.2.1 (#2, Jun 17 2002, 12:06:51)
[GCC 2.96 20000731 (Red Hat Linux 7.3 2.96-110)]
wxWindows 2.3.2
wxPython: 2.3.2.1

···

________________________________________________________________________________

This is a new version with the wxPython version added and the subject problem solved.

I have attached a copy of the program and send this using the program itself.

---
Nigel W. Moriarty
Building 4R0230
Physical Biosciences Division
Lawrence Berkeley National Laboratory
Berkeley, CA 94720-8235
Phone : 510-486-5709
Fax : 510-486-5909
Email : NWMoriarty@LBL.gov
Web : CCI.LBL.gov

"""
A wxPython-users list mailer written in wxPython

Nigel W. Moriarty

"""
from wxPython.wx import *
from wxPython.wx import __version__
from wxPython.lib.filebrowsebutton import FileBrowseButton
import os, sys
import smtplib
import string
import pickle

class MailPanel(wxPanel):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MailPanel.__init__
        kwds["style"] = wxTAB_TRAVERSAL
        wxPanel.__init__(self, *args, **kwds)
        self.From = wxStaticText(self, -1, "From:")
        self.From_text = wxTextCtrl(self, -1, "")
        self.To = wxStaticText(self, -1, "To:")
        self.To_text = wxTextCtrl(self, -1, "", style=wxTE_READONLY)
        self.CC = wxStaticText(self, -1, "CC:")
        self.CC_text = wxTextCtrl(self, -1, "")
        self.Attach = wxStaticText(self, -1, "Attachment:")
        self.Attach_text = FileBrowseButton(self, -1)
        self.Subject = wxStaticText(self, -1, "Subject:")
        self.Subject_text = wxTextCtrl(self, -1, "")
        self.Body = wxStaticText(self, -1, "Body:")
        self.Body_text = wxTextCtrl(self, -1, "", style=wxTE_MULTILINE)

        self.__set_properties()
        self.__do_layout()
        # end wxGlade

    def __set_properties(self):
        # begin wxGlade: MailPanel.__set_properties
        self.Body_text.SetSize((600, 600))
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: MailPanel.__do_layout
        grid_sizer_1 = wxFlexGridSizer(4, 2, 0, 0)
        grid_sizer_1.Add(self.From, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5)
        grid_sizer_1.Add(self.From_text, 0, wxALL|wxEXPAND, 5)
        grid_sizer_1.Add(self.To, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5)
        grid_sizer_1.Add(self.To_text, 0, wxALL|wxEXPAND, 5)
        grid_sizer_1.Add(self.CC, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5)
        grid_sizer_1.Add(self.CC_text, 0, wxALL|wxEXPAND, 5)
        grid_sizer_1.Add(self.Attach, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5)
        grid_sizer_1.Add(self.Attach_text, 0, wxALL|wxEXPAND, 5)
        grid_sizer_1.Add(self.Subject, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5)
        grid_sizer_1.Add(self.Subject_text, 0, wxALL|wxEXPAND, 5)
        grid_sizer_1.Add(self.Body, 0, wxALL, 5)
        grid_sizer_1.Add(self.Body_text, 0, wxALL|wxEXPAND, 5)
        self.SetAutoLayout(1)
        self.SetSizer(grid_sizer_1)
        grid_sizer_1.Fit(self)
        grid_sizer_1.SetSizeHints(self)
        self.Layout()
        # end wxGlade

# end of class MailPanel

class MyFrame(wxFrame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MyFrame.__init__
        kwds["style"] = wxDEFAULT_FRAME_STYLE
        wxFrame.__init__(self, *args, **kwds)
        self.panel_1 = MailPanel(self, -1)

        self.__set_properties()
        self.__do_layout()
        # end wxGlade

    def __set_properties(self):
        # begin wxGlade: MyFrame.__set_properties
        self.SetTitle("wxPython List Mailer")
        self.SetSize((700, 850))
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: MyFrame.__do_layout
        sizer_1 = wxBoxSizer(wxVERTICAL)
        sizer_1.Add(self.panel_1, 1, wxEXPAND, 0)
        self.SetAutoLayout(1)
        self.SetSizer(sizer_1)
        self.Layout()
        # end wxGlade

# end of class MyFrame

prefs_file = '.wxlist_mail_prefs'

class empty:
  def __repr__(self):
    outl = 'Class '+self.__class__.__name__
    for attr in self.__dict__:
      print attr, str(self.__dict__[attr])
      outl += "\n\t%s : %s" % (attr, str(self.__dict__[attr]))
    return outl

class MyApp(wxApp):
  def OnInit(self):
    self.done = 0
    self.frame = MyFrame(None, wxNewId(), 'wxPython List Mailer')

    self.frame.menubar = wxMenuBar()

    ID_SEND = wxNewId()
    ID_QUIT = wxNewId()
    self.frame.ProgramMenu = wxMenu()
    self.frame.ProgramMenu.Append(ID_SEND,
                                  "Send Mail\tCtrl-M",
                                  'Send this email to wxPython List')
    self.frame.ProgramMenu.Append(ID_QUIT,
                                  "Quit\tCtrl-Q",
                                  'Quit')
    self.frame.menubar.Append(self.frame.ProgramMenu,
                              "Program")

    self.frame.SetMenuBar(self.frame.menubar)

    EVT_MENU(self.frame, ID_SEND, self.OnSend)
    EVT_MENU(self.frame, ID_QUIT, self.OnQuit)
    
    self.panel = MailPanel(self.frame, wxNewId())

    self.user = os.getlogin()
    self.user_home = os.path.expanduser("~%s" % self.user)

    self.prefs = self.GetPrefs()

    if not self.prefs:
      self.prefs = self.ExtractPrefs()

    self.panel.From_text.SetValue(self.prefs.email)
    self.panel.To_text.SetValue("wxPython-users@lists.wxwindows.org")

    header = "Python:\n%s\nwxWindows %d.%d.%d\nwxPython: %s\n%s\n" \
             % (sys.version,
                wxMAJOR_VERSION,
                wxMINOR_VERSION,
                wxRELEASE_NUMBER,
                __version__,
                '_'*80,
                )
    header += "%s%s\n%s" % ('\n'*3, '-'*3, self.GetSignature())
    self.panel.Body_text.SetValue(header)

    self.frame.Show()

    return true

  def OnSend(self, evt):
    server = smtplib.SMTP(self.prefs.smtp)
    sender = self.prefs.email
    recipient = self.panel.To_text.GetValue() \
                +', '+self.panel.CC_text.GetValue()
    msg = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\X-Mailer: My-Mail\r\n\r\n%s\n" \
           % (self.panel.From_text.GetValue(),
              recipient,
              self.panel.Subject_text.GetValue(),
              self.panel.Body_text.GetValue(),
              )
    if self.panel.Attach_text.GetValue():
      f = open(self.panel.Attach_text.GetValue(),'r')
      lines = f.read()
      f.close()
      msg += '===\n'+lines
    server.sendmail(self.panel.From_text.GetValue(),
                    recipient,
                    msg
                    )
    server.quit()

    self.SetPrefs(self.prefs)

    self.OnQuit(evt)

  def OnQuit(self, evt):
    self.frame.Close(1)
    sys.exit()

  def GetPrefs(self):
    try:
      file = open(os.path.join(self.user_home, prefs_file),'r')
      prefs = pickle.load(file)
      file.close()
    except:
      prefs = None
    return prefs
      
  def SetPrefs(self, prefs):
    try:
      file = open(os.path.join(self.user_home, prefs_file),'w')
      pickle.dump(prefs, file)
      file.close()
    except:
      print 'failed to save'

  def ExtractPrefs(self):
    prefs = empty()
    dlg = wxTextEntryDialog(None,
                            'Enter your email address',
                            'Email Address',
                            self.user,
                            )
    if dlg.ShowModal() == wxID_OK:
      prefs.email = dlg.GetValue()
    dlg = wxTextEntryDialog(None,
                            'Enter your SMTP server',
                            'SMTP server',
                            )
    if dlg.ShowModal() == wxID_OK:
      prefs.smtp = dlg.GetValue()
    return prefs

  def GetSignature(self):
    f = open(os.path.join(self.user_home, '.signature'), 'r')
    lines = f.read()
    f.close()
    return lines

app = MyApp()
app.MainLoop()

Nigel,

The op sys info that Python provides tells you about where it was built,
not what it's currently running on. You should add the op sys info to
this program. The best way I know of is:

import os
if os.name=='posix':
  cmd = 'uname -a'
else:
  cmd = 'ver'
osInfo = os.popen(cmd).read().strip()

The only caveat I know of is that on *some* instances of Windows ME,
os.popen() freezes indefinitely.

···

On Monday 05 May 2003 02:45 pm, nwmoriarty@lbl.gov wrote:

Python:
2.2.1 (#2, Jun 17 2002, 12:06:51)
[GCC 2.96 20000731 (Red Hat Linux 7.3 2.96-110)]
wxWindows 2.3.2
wxPython: 2.3.2.1
_____________________________________________________________________
___________

This is a new version with the wxPython version added and the subject
problem solved.

I have attached a copy of the program and send this using the program
itself.

--
Chuck
http://ChuckEsterbrook.com

Thanks Chuck. I inserted the additional information.

OS: Linux legless.lbl.gov 2.4.18-26.8.0 #1 Mon Feb 24 09:32:55 EST 2003 i686 athlon i386 GNU/Linux
Python: 2.2.1 (#2, Jun 17 2002, 12:06:51) [GCC 2.96 20000731 (Red Hat Linux 7.3 2.96-110)]
wxWindows 2.3.2
wxPython: 2.3.2.1

I've been working on porting it to MSW and I can't get a value from the
subject control using self.panel.Subject_text.GetValue(). It works fine on LINUX but not on MSW.

wxListMailer.py (7.5 KB)

···

import os
if os.name=='posix':
        cmd = 'uname -a'
else:
        cmd = 'ver'
osInfo = os.popen(cmd).read().strip()

The only caveat I know of is that on *some* instances of Windows ME,
os.popen() freezes indefinitely.

Chuck Esterbrook wrote:

The op sys info that Python provides tells you about where it was built, not what it's currently running on. You should add the op sys info to this program. The best way I know of is:

import os
if os.name=='posix':
  cmd = 'uname -a'
else:
  cmd = 'ver'
osInfo = os.popen(cmd).read().strip()

The only caveat I know of is that on *some* instances of Windows ME, os.popen() freezes indefinitely.

I think there is a new module in Python 2.3 that gives lots of info about the platform...

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!