def attach(req, macct_id, mdir_name_enc, mail_uid, filename_enc): try: jm = JMail(req, tmpl_name='msg/attach', macct_id=macct_id, imap_start=True) except JMailError as e: return e.response() try: mdir = JMailMDir(name_encode=mdir_name_enc) msg = mdir.msg_get(mail_uid) except JMailError as e: return e.response() a = None for p in msg.parts(): if p.is_attach(): if p.filename_encode() == filename_enc: a = p if a is None: return jm.error(404, 'No attach found!') resp = HttpResponse(a.payload(), content_type='{}; charset={}'.format( a.content_type(), a.get_charset())) resp['Content-Disposition'] = 'attachment; filename="{}"'.format(a.filename()) jm.log.dbg('Msg attach: ', resp['Content-Disposition']) jm.end() return resp
def home(req): try: jm = JMail(req, tmpl_name='user/home') except JMailError as e: return e.response() jm.tmpl_data({}) return jm.render()
def edit(req, macct_id): try: jm = JMail(req, tmpl_name='macct/edit') macct = jm.macct_get(macct_id) except JMailError as e: if jm: jm.end() return e.response() jm.log.dbg('macct: ', macct) dbobj = JMailMAcct.objects.get(pk=macct_id) if req.method == 'POST': try: form = JMailMAcctForm(req.POST, instance=dbobj) form.save() return jm.redirect('macct:edit', macct_id) except Exception as e: return jm.error(500, str(e)) else: jm.tmpl_data({ 'macct': macct, 'macct_formset': JMailMAcctForm(instance=dbobj).as_p(), }) return jm.render()
def logout(req): try: jm = JMail(req) except JMailError as e: return e.response() django_logout(req) jm.tmpl_data({'user': None}) return jm.render()
def auth(req): try: jm = JMail(req, user_auth=False) except JMailError as e: return e.response() if not req.method == 'POST': return jm.error(400, 'Bad request') user_email = req.POST.get('jmail_user', None) if not user_email: return jm.error(400, 'No user') user_pass = req.POST.get('jmail_user_pass', None) if not user_pass: return jm.error(400, 'No user pass') user = authenticate(username=user_email, password=user_pass) if user is None: return jm.error(401, 'Invalid login') else: if user.is_active: django_login(req, user) return jm.redirect('home') else: return jm.error(401, 'Disabled user') return jm.render()
def source(req, macct_id, mdir_name_enc, mail_uid): try: jm = JMail(req, tmpl_name='msg/source', macct_id=macct_id, imap_start=True) mdir = JMailMDir(name_encode=mdir_name_enc) msg = mdir.msg_get(mail_uid, peek=False) jm.tmpl_data({ 'load_navbar_path': True, 'mdir': mdir, 'msg': msg, }) except JMailError as e: return e.response() return jm.render(charset=msg.get_charset())
def read(req, macct_id, mdir_name_enc, mail_uid, read_html=None): try: jm = JMail(req, tmpl_name='msg/read', macct_id=macct_id, imap_start=True) except JMailError as e: return e.response() if read_html is not None: read_html = True jm.log.dbg('read HTML: ', read_html) try: mdir = JMailMDir(name_encode=mdir_name_enc) msg = mdir.msg_get(mail_uid, peek=False) except JMailError as e: return e.response() jm.tmpl_data({ 'load_navbar_path': True, 'mdir': mdir, 'msg': msg, 'read_html': read_html, }) return jm.render(charset=msg.get_charset())
def reply(req, macct_id, mdir_name_enc, msg_uid, subcmd='reply'): try: jm = JMail(req, tmpl_name='msg/compose', macct_id=macct_id, imap_start=True) except JMailError as e: return e.response() try: mdir = JMailMDir(name_encode=mdir_name_enc) msg = mdir.msg_get(msg_uid, peek=False) except JMailError as e: return e.response() # TODO / FIXME # * Salvo que sea un replyall, limpiar CC y BCC # -- set From from_orig = msg.headers.get('from') msg.headers.set_hdr('from', jm.macct['address']) # -- set To reply_to_orig = msg.headers.get('reply_to', None) if reply_to_orig is None: jm.log.dbg('reply using header: To') else: from_orig = reply_to_orig jm.log.dbg('reply using header: Reply-To') if subcmd == 'forward': msg.headers.set_hdr('to', '') else: msg.headers.set_hdr('to', from_orig) # -- remove CC if not replyall if subcmd == 'reply': msg.headers.set_hdr('cc', '') # -- quote body date_orig = msg.headers.get('date', None) msg.body = __msg_body_quote(msg.body, date_orig, from_orig) # -- update subject subject_new = __msg_subject_update(subcmd, msg.headers.get('subject', '')) msg.headers.set_hdr('subject', subject_new) jm.tmpl_data({ 'load_navbar_path': True, 'msg': msg, 'mdir': mdir, }) return jm.render(charset=msg.get_charset())
def remove(req, macct_id): try: jm = JMail(req, tmpl_name='macct/remove') macct = jm.macct_get(macct_id) except JMailError as e: if jm: jm.end() return e.response() jm.log.dbg('macct: ', macct) jm.tmpl_data({'macct': macct}) if req.method == 'POST': try: dbobj = JMailMAcct.objects.get(pk=macct_id) dbobj.delete() except Exception as e: return jm.error(500, str(e)) return jm.render(tmpl_name='macct/removed') else: return jm.render(tmpl_name='macct/remove_confirm')
def login(req): try: jm = JMail(req, user_auth=False) except JMailError as e: return e.response() if req.user.is_authenticated(): return jm.redirect('home') jm.tmpl_data({}) return jm.render()
def admin(req): try: jm = JMail(req, tmpl_name='macct/admin') except JMailError as e: return e.response() jm.tmpl_data({ 'mail': { 'accounts': jm.macct_get_all(), }, }) return jm.render()
def debug(req): # coverage: exclude jm = JMail(req, user_auth=False) if not jm.devmode: return jm.error(400, 'Bad request') ddata = jm.debug_data() ddata.append('python: {}'.format(sys.version)) ddata.append('') ddata.append('req.path: {}'.format(req.path)) ddata.append('req.path_info: {}'.format(req.path_info)) ddata.append('req.encoding: {}'.format(req.encoding)) ddata.append('req.scheme: {}'.format(req.scheme)) ddata.append('') ddata.append('req.user: {}'.format(type(req.user))) ddata.append('req.user: {}'.format(sorted(dir(req.user)))) ddata.append('req.user: {}'.format(req.user)) ddata.append('req.user.is_authenticated: {}'.format( req.user.is_authenticated())) ddata.append('req.user.groups: {}'.format(type(req.user.groups))) ddata.append('') ddata.append('req.session: {}'.format(req.session)) ddata.append('req.session: {}'.format(sorted(dir(req.session)))) ddata.append('req.session.items: {}'.format(sorted(req.session.items()))) ddata.append('') ddata.append('dir(req): {}'.format(sorted(dir(req)))) ddata.append('req: {}'.format(req)) ddata.append('') ddata.append('jmail settings: {}'.format(type(jmail_settings))) ddata.append('') for sn in sorted(dir(jmail_settings)): if not sn.startswith('_'): ddata.append('{}={}'.format( sn, str(getattr(jmail_settings, sn, None)).strip())) ddata.append('') ddata.append('django settings: {}'.format(type(settings))) ddata.append('') for sn in sorted(dir(settings)): if not sn.startswith('_'): ddata.append('{}={}'.format( sn, str(getattr(settings, sn, None)).strip())) ddata.append('') jm.tmpl_data({ 'debug': { 'data': ddata, }, }) return jm.render()
def home(req): from jmail.macct.models import JMailMAcct try: jm = JMail(req) except JMailError as e: return e.response() jm.tmpl_data({ 'mail': { 'accounts': jm.macct_get_all(), }, }) return jm.render()
def create(req): try: jm = JMail(req, tmpl_name='macct/create') except JMailError as e: return e.response() if req.method == 'POST': try: macct = JMailMAcct(user=jm.user) form = JMailMAcctForm(req.POST, instance=macct) jm.log.dbg(sorted(dir(form))) jm.log.dbg(form.fields) form.save() except Exception as e: jm.log.err(e) return jm.error(500, str(e)) return jm.redirect('home') else: jm.tmpl_data({ 'form_data': JMailMAcctForm().as_p(), }) return jm.render()
def compose(req, macct_id): try: jm = JMail(req, tmpl_name='msg/compose', macct_id=macct_id) except JMailError as e: return e.response() msg_saved = jm.cache_get('compose:save') compose_restore = False if msg_saved is not None: msg = JMailMessage(source=msg_saved) compose_restore = True else: msg = JMailMessage(source=b'') msg.headers.set_hdr('From', jm.macct['address']) jm.tmpl_data({ 'load_navbar_path': True, 'msg': msg, 'compose_restore': compose_restore, }) return jm.render(charset=msg.get_charset())
def send(req, macct_id): # TODO: el mail se deberia guardar en INBOX.Queue primero, después # iniciar el proceso que mande el/los mails que haya en INBOX.Queue # "parecido" a lo que hace claws. # Mientras tanto, lo guardamos en el cache para que no se pierda # si falla el envio y luego compose lo puede "rescatar". try: jm = JMail(req, macct_id=macct_id) except JMailError as e: return e.response() jm.log.dbg('mail send: ') td = jm.tmpl_data({'load_navbar_path': True}) if req.method != 'POST': return jm.error(400, 'bad request', tmpl_data=td) req_cmd = req.POST.get('jmail_cmd') if req_cmd == 'discard': # -- discard composing return __compose_discard(jm, macct_id) try: msg = MIMEText(req.POST.get('mail_body'), _charset='utf-8') msg['From'] = req.POST.get('mail_from') msg['To'] = req.POST.get('mail_to') except Exception as e: return jm.error(500, 'could not create email: '+str(e), tmpl_data=td) mail_subject = req.POST.get('mail_subject', '') if mail_subject != '': msg['Subject'] = mail_subject mail_cc = req.POST.get('mail_cc', '') if mail_cc != '': msg['Cc'] = mail_cc mail_bcc = req.POST.get('mail_bcc', '') if mail_bcc != '': msg['Bcc'] = mail_bcc msg['Reply-To'] = msg['From'] msg['Message-ID'] = '<{}.{}@jmail>'.format(time.time(), jm.user) msg['Date'] = time.strftime(jm.conf.get('DATE_HEADER_FORMAT')) msg['X-Mailer'] = 'jmail v{}'.format(jm.version) # -- save email to cache in case the SMTP fails jm.cache_set('compose:save', msg.as_string()) try: smtp = jm.smtp_init() smtp.send_message(msg) smtp.quit() except Exception as e: jm.log.err('msg send: ', str(e)) return jm.error(500, 'SMTP error: '+str(e), tmpl_data=td) # -- if all was fine, remove the message from the cache jm.cache_del('compose:save') return jm.message('mail sent!', tmpl_data=jm.tmpl_data({'load_navbar_path': True}))