Login

Email multiattachment and multireciepments

Author:
fordexa
Posted:
September 18, 2008
Language:
Python
Version:
1.0
Tags:
email attachment
Score:
1 (after 1 ratings)

If you want use html formatting download tinyMCE

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# forms #
class WriteEmailForm(forms.Form):
    to = forms.CharField(widget=forms.TextInput(), label=_('To'), help_text='<a href="javascript:;" id="add_reciepment">add</a>')
    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 %}
<script src="{{ MEDIA_URL }}js/addition_fields.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript" src="{{ MEDIA_URL }}js/tiny_mce/tiny_mce.js"></script>
<script language="javascript" type="text/javascript">
	tinyMCE.init({
		mode : "textareas",
		theme : "advanced"
	});
</script>
{% endblock %}

{% block content %}
	<h3>{% trans 'Write message' %}</h3>
	<br />
	<form method="post" action="./" enctype="multipart/form-data" class="message_form">
		<table align="left" cellspacing="1" cellpadding="3" border="0">
			<tr><th>From:</th><td><span id="from_email">{{ request.user.username }}&nbsp;&#60;{{ request.user.email }}&#62;</span></td></tr>
			{{ form }}
<tr><td colspan="2"><a href="javascript:;" id="add_attachment">{% trans "Attach file" %}</a></td></tr>
			<tr><td colspan="2"><input type="submit" value="{% trans 'Send letter' %}"></td></tr>
		</table>
	</form>
{% 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("<tr id='3"+attachment_count+"'><th>File "+attachment_count+"</th><td><input id='id_attachment"+attachment_count+"' name='attachment"+attachment_count+"' type='file'><a href='javascript:;' onclick='delNewFormItem("+attachment_count+", "+'3'+','+'"attachment"'+");'>&nbsp;Remove</a></td>");
			attachment_count++;
		} else {
			alert('Max attachment count is 5!')
		};
	});
});

More like this

  1. Get email and push on couchdb - utils by magik_cypress 3 years, 4 months ago
  2. Email validation by mulamvenky 2 years, 1 month ago
  3. Testing email sending by monikasulik 4 months, 2 weeks ago
  4. email backend which use sendmail subprocess by pawnhearts 5 years, 5 months ago
  5. Email-obfuscator Template Tag by gronimo 3 years, 1 month ago

Comments

Please login first before commenting.