# forms # class WriteEmailForm(forms.Form): to = forms.CharField(widget=forms.TextInput(), label=_('To'), help_text='add') subject = forms.CharField(widget=forms.TextInput(), label=_('Subject')) body = forms.CharField(widget=forms.Textarea(), label=_('Body')) # views # def write_message(request): template = 'your_template/write_message.html' if request.POST: mail_data = request.POST.copy() mail_data.update(request.FILES) form = WriteEmailForm(request.POST) if form.is_valid(): from_email = request.user.email subject = form.cleaned_data['subject'] to = form.cleaned_data['to'].split(';') body = form.cleaned_data['body'] msg = EmailMessage(subject, body, from_email, to) for field, value in mail_data.items(): if field.__contains__('attachment') and value != u'': msg.attach(value.name, value.read(), value.content_type) msg.content_subtype = "html" try: msg.send() except: return render_to_response('your_template/send_message_err.html', { 'to': to }, RequestContext(request)) else: return render_to_response('your_template/send_message_ok.html', { 'to': to }, RequestContext(request)) else: return render_to_response(template, { 'form': form }, RequestContext(request)) else: form = WriteEmailForm() return render_to_response(template, { 'form': form }, RequestContext(request)) # your_template # {% extends 'base.html' %} {% load i18n %} {% block scripts %} {% endblock %} {% block content %}

{% trans 'Write message' %}


{{ form }}
From:{{ request.user.username }} <{{ request.user.email }}>
{% trans "Attach file" %}
{% endblock %} # addition_fields.js jquery code # var attachment_count = 1; function delNewFormItem(i, n, t) { $('tr#'+n+i).remove(); if ( t == 'attachment') attachment_count-- }; $(document).ready(function() { $('a#add_attachment').click(function(){ if ( attachment_count < 6 ) { $(this).parent().parent().after("File "+attachment_count+" Remove"); attachment_count++; } else { alert('Max attachment count is 5!') }; }); });