#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os, traceback
import wx
from library import i18n
import send_email_funct
_ = i18n.i18n()
class EMailFrame(wx.Frame):
""" Send an email
"""
def __init__(self, parent, conf, files=None, receiver=None):
""" Pass me the list of receivers and the server.
If the server are not set, I'll use localhost
"""
if receiver is None: receiver = ()
if files is None: files = ()
self._receiver = receiver
self._files = files
self._conf = conf
super(EMailFrame, self).__init__(parent, -1, _("email_frame_title"))
panel = wx.Panel(self)
# email info section
lbl_address = wx.StaticText(panel, -1, _("email_lbl_address")+":")
self._tctrl_address = wx.TextCtrl(panel)
lbl_subject = wx.StaticText(panel, -1, _("email_lbl_subject")+":")
self.tctrl_subject = wx.TextCtrl(panel)
lbl_text = wx.StaticText(panel, -1, _("email_lbl_text")+":")
self._tctrl_text = wx.TextCtrl(panel, style=wx.TE_MULTILINE)
sz_send_info = wx.GridBagSizer()
flg_sz_send_info = wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.EXPAND | wx.ALL
sz_send_info.Add(lbl_address, (0, 0), (1, 1), flg_sz_send_info, 5)
sz_send_info.Add(self._tctrl_address, (0, 1), (1, 1), flg_sz_send_info, 5)
sz_send_info.Add(lbl_subject, (1, 0), (1, 1), flg_sz_send_info, 5)
sz_send_info.Add(self.tctrl_subject, (1, 1), (1, 1), flg_sz_send_info, 5)
sz_send_info.AddGrowableCol(1)
# email text section
sz_text = wx.BoxSizer(wx.VERTICAL)
sz_text.Add(lbl_text, 0, wx.ALIGN_LEFT | wx.ALL, 5)
sz_text.Add(self._tctrl_text, 1, wx.ALIGN_CENTER | wx.EXPAND | wx.ALL, 5)
# email section
sz_email = wx.BoxSizer(wx.VERTICAL)
sz_email.Add(sz_send_info, 0, wx.ALIGN_CENTER | wx.EXPAND | wx.ALL, 5)
sz_email.Add(sz_text, 1, wx.ALIGN_CENTER | wx.EXPAND | wx.ALL, 5)
# fax attach section
self._lbox_attach_fax = wx.ListBox(panel, size=wx.Size(150, -1),
style=wx.LB_HSCROLL | wx.LB_EXTENDED)
self._lbox_attach_fax.Bind(wx.EVT_CHAR, self._onCharOnListBox)
dt = MyFileDropTarget(self._lbox_attach_fax)
self._lbox_attach_fax.SetDropTarget(dt)
btn_fax_browse = wx.Button(panel, -1, _("email_btn_fax_browse")+"...")
btn_fax_browse.Bind(wx.EVT_BUTTON, self._onFaxBrowseBtn)
lbl_attach_fax = wx.StaticText(panel, -1, _("email_lbl_attach_fax")+":")
sz_attach_fax = wx.BoxSizer(wx.VERTICAL)
sz_attach_fax.Add(lbl_attach_fax, 0, wx.ALIGN_LEFT | wx.EXPAND | wx.ALL, 5)
sz_attach_fax.Add(self._lbox_attach_fax, 1, wx.ALIGN_CENTER | wx.EXPAND | wx.ALL, 5)
sz_attach_fax.Add(btn_fax_browse, 0, wx.ALIGN_CENTER | wx.ALL, 5)
# north section
sz_north = wx.BoxSizer(wx.HORIZONTAL)
sz_north.Add(sz_email, 1, wx.ALIGN_LEFT | wx.EXPAND | wx.ALL, 5)
sz_north.Add(sz_attach_fax, 0, wx.ALIGN_LEFT | wx.EXPAND | wx.ALL, 5)
# send button
btn_send = wx.Button(panel, -1, _("email_btn_send"))
btn_send.Bind(wx.EVT_BUTTON, self._onSendBtn)
# main sizer
sz_main = wx.BoxSizer(wx.VERTICAL)
sz_main.Add(sz_north, 1, wx.ALIGN_CENTER | wx.EXPAND | wx.ALL, 5)
sz_main.Add(btn_send, 0, wx.ALIGN_CENTER | wx.ALL, 5)
#add values passed
for file_ in self._files:
self._lbox_attach_fax.Append(file_)
self.tctrl_subject.SetValue(self._conf.get("txt_smtp_regarding"))
panel.SetSizerAndFit(sz_main)
self.SetClientSize(panel.GetSize())
self.SetSize(wx.Size(575, 370))
self.SetMinSize(wx.Size(575, 370))
self.CenterOnScreen()
self.Show()
def _onCharOnListBox(self, evt):
""" If the char pressed on the list box is "CANC", removes the items
selected from the list box
"""
if evt.GetKeyCode() == wx.WXK_DELETE:
items_idxs = self._lbox_attach_fax.GetSelections()
if not items_idxs: return
for idx in items_idxs:
self._lbox_attach_fax.Delete(idx)
evt.Skip()
def _onFaxBrowseBtn(self, evt):
""" Shows a frame that allows to choose a file from the file system
"""
wildcard = "All files (*.*)|*.*"
paths = None
def_dir = os.getcwd()
file_dlg = wx.FileDialog(self, message=_("email_filedlg_choose_fax"),
defaultDir=def_dir, defaultFile="", wildcard=wildcard,
style=wx.OPEN | wx.MULTIPLE | wx.CHANGE_DIR
)
if file_dlg.ShowModal() == wx.ID_OK:
paths = file_dlg.GetPaths()
file_dlg.Destroy()
if paths is not None:
lBoxInsertItems(self._lbox_attach_fax, paths)
def _onSendBtn(self, evt):
""" sends the email message
"""
server = self._conf.get("txt_smtp_host")
from_ = self._conf.get("txt_smtp_from")
user = self._conf.get("txt_smtp_user")
passwd = self._conf.get("txt_smtp_pass")
to = self._tctrl_address.GetValue().split(";")
regard = self.tctrl_subject.GetValue()
text = self._tctrl_text.GetValue()
attach = self._lbox_attach_fax.GetItems()
"""send_from, send_to, subject, text, files=[],
server="localhost", user=None, passwd=None
"""
try:
send_email_funct.send_mail(from_, to, regard, text, attach,
server, user, passwd)
wx.MessageBox(_("email_send"), "Ok!", wx.ICON_INFORMATION)
self.Close()
except:
msg = traceback.format_exc()
print msg
wx.MessageBox(msg, "Error!", wx.ICON_ERROR)
def add_fax(self, path):
"""
"""
self._lbox_attach_fax.Append(path)
def lBoxInsertItems(list_box, file_names):
""" inserts the items in the passed list_box
"""
lst_items = list_box.GetItems()
for file_ in file_names:
if file_ in lst_items:
continue
file_name = os.path.split(file)[1]
list_box.Append( file_ )
class MyFileDropTarget(wx.FileDropTarget):
def __init__(self, window):
wx.FileDropTarget.__init__(self)
self.window = window
def OnDropFiles(self, x, y, filenames):
""" append rows to list box when user drag file here
"""
lBoxInsertItems(self.window, filenames)
if __name__ == "__main__":
app = wx.PySimpleApp()
EMailFrame(None)
app.MainLoop()
|