def smtpaddfiletoattach(widget, vbox): ''' adds an hbox to the smtp dialog; allows you to attach multiple files to your email. ''' hb = flauxtext.gtk.HBox() e = flauxtext.gtk.Entry() l = flauxtext.gtk.Label("attach file:") l.show() hb.pack_start(l) hb.pack_start(e) d = flauxtext.gtk.FileChooserDialog("Open..", None, flauxtext.gtk.FILE_CHOOSER_ACTION_OPEN, (flauxtext.gtk.STOCK_CANCEL, 0, flauxtext.gtk.STOCK_OPEN, 1)) d.set_property("show-hidden", flauxtext.openshowhidden) d.set_default_response(1) d.set_property("select-multiple", True) d.set_current_folder(flauxtext.os.path.dirname(self.current_filename())) while 1: # event loops rock r = d.run() if r == 0: break if r == 1: fnyms = d.get_filenames() if (len(fnyms) == 1) and flauxtext.os.path.isdir(fnyms[0]): d.set_current_folder(fnyms[0]) continue else: for filename in fnyms: hb = flauxtext.gtk.HBox() e = flauxtext.gtk.Entry() l = flauxtext.gtk.Label("attach file:") l.show() hb.pack_start(l) hb.pack_start(e) e.set_text(filename) self.extrafilestoattachentries.append(e) e.show() hb.show() vbox.pack_start(hb) break d.destroy() def smtpbodytogglecb(checkbutton, tehbox): ''' called when you toggle the "attach current file" checkbutton in the smtp dialog; hides or shows tehbox, which is a scrolledwindowed textbuffer containing the body of your message. ''' tehbox.set_property('visible', checkbutton.get_active()) tehbox.get_toplevel().resize(1, 1) def smtperror(reason): ''' called in except statements in smtp(); opens a tiny dialog informing you why your email could not be sent. ''' d = self.mk_dialog('smtp error', (flauxtext.gtk.STOCK_OK, flauxtext.gtk.RESPONSE_OK)) d.vbox.pack_start(flauxtext.gtk.Label(reason + '\nemail not sent.')) d.run() def smtp(action, *a): ''' opens a big honkin' dialog asking you for information necessary to send the current file in an email. ''' if self.smtpcontactslist == None: smtpcontactsfile = open(flauxtext.contactsfn) self.smtpcontactslist = flauxtext.re.split('\r?\n?', smtpcontactsfile.read()) smtpcontactsfile.close() curfn = self.current_filename() d = self.mk_dialog('smtp ala flauxtext', (flauxtext.gtk.STOCK_OK, 1, flauxtext.gtk.STOCK_CANCEL, 0)) d.set_resizable(True) srvhbox = flauxtext.gtk.HBox() d.vbox.pack_start(srvhbox) srvhbox.pack_start(flauxtext.gtk.Label("server:")) srv = flauxtext.gtk.Entry() if flauxtext.smtpserver != None: srv.set_text(flauxtext.smtpserver) srvhbox.pack_start(srv) namehbox = flauxtext.gtk.HBox() d.vbox.pack_start(namehbox) namehbox.pack_start(flauxtext.gtk.Label("login name:")) name = flauxtext.gtk.Entry() namehbox.pack_start(name) name.set_text(flauxtext.smtpname) fromhbox = flauxtext.gtk.HBox() d.vbox.pack_start(fromhbox) fromhbox.pack_start(flauxtext.gtk.Label('from:')) frome = flauxtext.gtk.Entry() fromcompletion = flauxtext.gtk.EntryCompletion() fromcompletion.set_text_column(0) fromcompletionlist = flauxtext.gtk.ListStore(str) tocompletionlist = flauxtext.gtk.ListStore(str) for i in flauxtext.smtpfromlist: fromcompletionlist.append([i]) tocompletionlist.append([i]) fromcompletion.set_model(fromcompletionlist) frome.set_completion(fromcompletion) fromhbox.pack_start(frome) tohbox = flauxtext.gtk.HBox() d.vbox.pack_start(tohbox) tohbox.pack_start(flauxtext.gtk.Label('to:')) to = flauxtext.gtk.Entry() tocompletion = flauxtext.gtk.EntryCompletion() tocompletion.set_model(tocompletionlist) tocompletion.set_text_column(0) to.set_completion(tocompletion) tohbox.pack_start(to) for i in self.smtpcontactslist: tocompletionlist.append([i]) for i in flauxtext.smtpalias.keys(): tocompletionlist.append([i]) subhbox = flauxtext.gtk.HBox() d.vbox.pack_start(subhbox) subhbox.pack_start(flauxtext.gtk.Label('subject:')) sub = flauxtext.gtk.Entry() sub.connect('activate', lambda sub_: d.response(1)) subhbox.pack_start(sub) if curfn != 'untitled': sub.set_text(flauxtext.os.path.basename(curfn)) bufis = flauxtext.gtk.CheckButton("attach current file, and use below as body.") d.vbox.pack_start(bufis) bufis.set_active(curfn != 'untitled') bufis.set_property('no-show-all', curfn == 'untitled') # if current buffer is untitled, this will not be shown when we d.show_all() bighbox = flauxtext.gtk.HBox() bighbox.set_property('no-show-all', curfn == 'untitled') # if current buffer is untitled, this will not be shown when we d.show_all() d.vbox.pack_start(bighbox) scrolldw = flauxtext.gtk.ScrolledWindow() bighbox.pack_start(scrolldw) textview = flauxtext.gtk.TextView() scrolldw.add(textview) scrolldw.set_size_request(400,400) textview.set_wrap_mode(flauxtext.gtk.WRAP_WORD) textview.modify_font(flauxtext.pango.FontDescription(flauxtext.font[0])) body = textview.get_buffer() body.set_text('attached') bufis.connect('toggled', smtpbodytogglecb, bighbox) self.extrafilestoattachentries=[] attachfilebutton = flauxtext.gtk.Button("attach another file") d.vbox.pack_start(attachfilebutton) attachfilebutton.connect('clicked', smtpaddfiletoattach, d.vbox) if len(sub.get_text()) < 1: sub.grab_focus() if len(to.get_text()) < 1: to.grab_focus() if len(frome.get_text()) < 1: frome.grab_focus() if len(name.get_text()) < 1: name.grab_focus() if len(srv.get_text()) < 1: srv.grab_focus() if d.run(): if bufis.get_active(): bdy = body.get_text(body.get_start_iter(),body.get_end_iter()) else: curbuf = self.current_buffer() bdy = curbuf.get_text(curbuf.get_start_iter(), curbuf.get_end_iter()) server = srv.get_text() tofield = to.get_text() to_addrs = tofield.split(';') for i in range(len(to_addrs)): if to_addrs[i] in flauxtext.smtpalias.keys(): to_addrs.append(flauxtext.smtpalias[to_addrs.pop(i)]) smtpcontactsfile = open(flauxtext.contactsfn, 'w') for i in to_addrs: if (i not in self.smtpcontactslist) and (i not in flauxtext.smtpalias.keys()): self.smtpcontactslist.append(i) self.smtpcontactslist.sort() smtpcontactsfile.write('\n'.join(self.smtpcontactslist)) smtpcontactsfile.close() fromfield = frome.get_text() subject = sub.get_text() username = name.get_text() files_to_attach = [] for i in self.extrafilestoattachentries: files_to_attach.append(i.get_text()) if bufis.get_active(): files_to_attach.append(curfn) if len(server) < 3: smtperror("you must fill the server field.") return False if len(fromfield) < 1: smtperror("you must fill the from field.") return False for i in to_addrs: if len(i) < 2: smtperror("you must fill the from field.") return False try: flauxtext.imp0rt('smtplib') s = flauxtext.smtplib.SMTP(server, 25, username) try: flauxtext.imp0rt('mimetypes', 'email.MIMEMultipart', 'email.MIMEText', 'email.MIMEImage', 'email.MIMEAudio', 'email.MIMEBase', 'email.Message') outer = flauxtext.email.MIMEMultipart.MIMEMultipart() outer['Subject'] = subject outer['To'] = ', '.join(to_addrs) outer['From'] = fromfield outer.preamble = 'get a MIME-aware email client, like thunderbird.' # To guarantee the message ends with a newline outer.epilogue = '' guts = flauxtext.email.MIMEText.MIMEText(bdy) outer.attach(guts) for path in files_to_attach: ctype, encoding = flauxtext.mimetypes.guess_type(path) if ctype is None or encoding is not None: # No guess could be made, or the file is encoded (compressed), so # use a generic bag-of-bits type. ctype = 'application/octet-stream' maintype, subtype = ctype.split('/', 1) if maintype == 'text': fp = open(path) msg = flauxtext.email.MIMEText.MIMEText(fp.read(), _subtype=subtype) fp.close() elif maintype == 'image': fp = open(path, 'rb') msg = flauxtext.email.MIMEImage.MIMEImage(fp.read(), _subtype=subtype) fp.close() elif maintype == 'audio': fp = open(path, 'rb') msg = flauxtext.email.MIMEAudio.MIMEAudio(fp.read(), _subtype=subtype) fp.close() else: fp = open(path, 'rb') msg = flauxtext.email.MIMEBase.MIMEBase(maintype, subtype) msg.set_payload(fp.read()) fp.close() flauxtext.email.Encoders.encode_base64(msg) msg.add_header('Content-Disposition', 'attachment', filename=flauxtext.os.path.basename(path)) outer.attach(msg) s.sendmail(fromfield, to_addrs, outer.as_string()) self.flash_info("i'm almost positive that your email was sent successfully.", 2e3) s.quit() except flauxtext.smtplib.SMTPException, e: smtperror("invalid email format.\nSMTPException\n"+str(e.args)) except Exception, e: smtperror("invalid email format.\nsocket error\n"+str(e.args)) except flauxtext.smtplib.SMTPException, e: smtperror("are you sure that's a valid smtp server?\nSMTPException\n"+str(e.args)) except Exception, e: smtperror("are you sure that you're connected and that that's a valid smtp server?\nsocket error\n"+str(e.args))