Exemple #1
0
def reply_all_message(request, folder, uid):
    '''Reply to a message'''
    # Get the message
    M = serverLogin( request )
    folder_name =  base64.urlsafe_b64decode(str(folder))
    folder = M[folder_name]
    message = folder[int(uid)]

    # Extract the relevant headers
    to_addr = mail_addr_str(message.envelope['env_from'][0])

    cc_addr =  join_address_list(message.envelope['env_to']+
        message.envelope['env_cc'])

    subject = _('Re: ') + message.envelope['env_subject'].decode('utf-8')

    # Extract the message text
    text = ''
    for part in message.bodystructure.serial_message():
        if part.is_text() and part.test_plain():
            text += message.part( part )

    # Quote the message
    text = quote_wrap_lines(text)
    text = (mail_addr_name_str(message.envelope['env_from'][0]) +
        _(' wrote:\n') + text)

    # Invoque the compose message form
    return send_message( request, text=text,  to_addr=to_addr, cc_addr=cc_addr,
                         subject=subject)
Exemple #2
0
def reply_all_message(request, folder, uid):
    '''Reply to a message'''
    # Get the message
    M = serverLogin(request)
    folder_name = base64.urlsafe_b64decode(str(folder))
    folder = M[folder_name]
    message = folder[int(uid)]

    # Extract the relevant headers
    to_addr = mail_addr_str(message.envelope['env_from'][0])

    cc_addr = join_address_list(message.envelope['env_to'] +
                                message.envelope['env_cc'])

    subject = _('Re: ') + message.envelope['env_subject'].decode('utf-8')

    # Extract the message text
    text = ''
    for part in message.bodystructure.serial_message():
        if part.is_text() and part.test_plain():
            text += message.part(part)

    # Quote the message
    text = quote_wrap_lines(text)
    text = (mail_addr_name_str(message.envelope['env_from'][0]) +
            _(' wrote:\n') + text)

    # Invoque the compose message form
    return send_message(request,
                        text=text,
                        to_addr=to_addr,
                        cc_addr=cc_addr,
                        subject=subject)
Exemple #3
0
def send_message(request, text='', to_addr='', cc_addr='', bcc_addr = '', subject='',
    attachments=''):
    '''Generic send message view
    '''
    if request.method == 'POST':
        new_data = request.POST.copy()
        other_action = False

        old_files = []
        if new_data.has_key('saved_files'):
            if new_data['saved_files']:
                old_files = new_data['saved_files'].split(',')

        uploaded_files = UploadFiles( request.user,
            old_files = old_files,
            new_files = request.FILES.getlist('attachment[]'))

        # Check if there is a request to delete files
        for key in new_data:
            match = delete_re.match(key)
            if match:
                id = int(match.groups()[0])
                uploaded_files.delete_id(id)
                other_action = True

        # Check if the cancel button was pressed
        if  new_data.has_key('cancel'):
            # Delete the files
            uploaded_files.delete()
            # return
            return HttpResponseRedirect('/')

        # create an hidden field with the file list.
        # In case the form does not validate, the user doesn't have
        # to upload it again
        new_data['saved_files'] = ','.join([ '%d' % Xi
            for Xi in uploaded_files.id_list()])

        user_profile = request.user.get_profile()

        form = ComposeMailForm(new_data, request = request)

        if new_data.has_key('upload'):
            other_action = True

        if form.is_valid() and not other_action:
            # get the data:
            form_data = form.cleaned_data

            subject = form_data['subject']
            from_addr  = form_data['from_addr']

            to_addr    = join_address_list( form_data['to_addr'] )
            cc_addr    = join_address_list( form_data['cc_addr'] )
            bcc_addr   = join_address_list( form_data['bcc_addr'] )

            text_format = form_data['text_format']
            message_text = form_data['message_text'].encode('utf-8')

            config = WebpymailConfig( request )

            if text_format == MARKDOWN and HAS_MARKDOWN:
                md = markdown.Markdown(output_format='HTML')
                message_html = md.convert(smart_unicode(message_text))
                css = config.get('message', 'css')
                # TODO: use a template to get the html and insert the css
                message_html = '<html>\n<style>%s</style><body>\n%s\n</body>\n</html>' % (css, message_html)
            else:
                message_html = None

            message = compose_rfc822( from_addr, to_addr, cc_addr, bcc_addr,
                subject, message_text, message_html, uploaded_files )

            try:
                host = config.get('smtp', 'host')
                port = config.getint('smtp', 'port')
                user = config.get('smtp', 'user')
                passwd = config.get('smtp', 'passwd')
                security = config.get('smtp', 'security').upper()
                use_imap_auth = config.getboolean('smtp', 'use_imap_auth')

                if use_imap_auth:
                    user = request.session['username']
                    passwd = request.session['password']

                send_mail( message,  host, port, user, passwd, security)
            except SMTPRecipientsRefused, detail:
                error_message = ''.join(
                    ['<p>%s' % escape(detail.recipients[Xi][1])
                     for Xi in detail.recipients ] )
                return render_to_response('mail/send_message.html', {'form':form,
                    'server_error': error_message,
                    'uploaded_files': uploaded_files},
                    context_instance=RequestContext(request))
            except SMTPException, detail:
                return render_to_response('mail/send_message.html', {'form':form,
                    'server_error': '<p>%s' % detail,
                    'uploaded_files': uploaded_files},
                    context_instance=RequestContext(request))
            except Exception, detail:
                error_message = '<p>%s' % detail
                return render_to_response('mail/send_message.html', {'form':form,
                    'server_error': error_message,
                    'uploaded_files': uploaded_files},
                    context_instance=RequestContext(request))
Exemple #4
0
def send_message(request,
                 text='',
                 to_addr='',
                 cc_addr='',
                 bcc_addr='',
                 subject='',
                 attachments=''):
    '''Generic send message view
    '''
    if request.method == 'POST':
        new_data = request.POST.copy()
        other_action = False

        old_files = []
        if new_data.has_key('saved_files'):
            if new_data['saved_files']:
                old_files = new_data['saved_files'].split(',')

        uploaded_files = UploadFiles(
            request.user,
            old_files=old_files,
            new_files=request.FILES.getlist('attachment[]'))

        # Check if there is a request to delete files
        for key in new_data:
            match = delete_re.match(key)
            if match:
                id = int(match.groups()[0])
                uploaded_files.delete_id(id)
                other_action = True

        # Check if the cancel button was pressed
        if new_data.has_key('cancel'):
            # Delete the files
            uploaded_files.delete()
            # return
            return HttpResponseRedirect('/')

        # create an hidden field with the file list.
        # In case the form does not validate, the user doesn't have
        # to upload it again
        new_data['saved_files'] = ','.join(
            ['%d' % Xi for Xi in uploaded_files.id_list()])

        user_profile = request.user.get_profile()

        form = ComposeMailForm(new_data, request=request)

        if new_data.has_key('upload'):
            other_action = True

        if form.is_valid() and not other_action:
            # get the data:
            form_data = form.cleaned_data

            subject = form_data['subject']
            from_addr = form_data['from_addr']

            to_addr = join_address_list(form_data['to_addr'])
            cc_addr = join_address_list(form_data['cc_addr'])
            bcc_addr = join_address_list(form_data['bcc_addr'])

            text_format = form_data['text_format']
            message_text = form_data['message_text'].encode('utf-8')

            config = WebpymailConfig(request)

            if text_format == MARKDOWN and HAS_MARKDOWN:
                md = markdown.Markdown(output_format='HTML')
                message_html = md.convert(smart_unicode(message_text))
                css = config.get('message', 'css')
                # TODO: use a template to get the html and insert the css
                message_html = '<html>\n<style>%s</style><body>\n%s\n</body>\n</html>' % (
                    css, message_html)
            else:
                message_html = None

            message = compose_rfc822(from_addr, to_addr, cc_addr, bcc_addr,
                                     subject, message_text, message_html,
                                     uploaded_files)

            try:
                host = config.get('smtp', 'host')
                port = config.getint('smtp', 'port')
                user = config.get('smtp', 'user')
                passwd = config.get('smtp', 'passwd')
                security = config.get('smtp', 'security').upper()
                use_imap_auth = config.getboolean('smtp', 'use_imap_auth')

                if use_imap_auth:
                    user = request.session['username']
                    passwd = request.session['password']

                send_mail(message, host, port, user, passwd, security)
            except SMTPRecipientsRefused, detail:
                error_message = ''.join([
                    '<p>%s' % escape(detail.recipients[Xi][1])
                    for Xi in detail.recipients
                ])
                return render_to_response(
                    'mail/send_message.html', {
                        'form': form,
                        'server_error': error_message,
                        'uploaded_files': uploaded_files
                    },
                    context_instance=RequestContext(request))
            except SMTPException, detail:
                return render_to_response(
                    'mail/send_message.html', {
                        'form': form,
                        'server_error': '<p>%s' % detail,
                        'uploaded_files': uploaded_files
                    },
                    context_instance=RequestContext(request))
            except Exception, detail:
                error_message = '<p>%s' % detail
                return render_to_response(
                    'mail/send_message.html', {
                        'form': form,
                        'server_error': error_message,
                        'uploaded_files': uploaded_files
                    },
                    context_instance=RequestContext(request))