Email Attachment

 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
------forms.py----------
from django import forms

class EmailForm(forms.Form):
    email = forms.EmailField()
    subject = forms.CharField(max_length=100)
    attach = forms.Field(widget = forms.FileInput)
    message = forms.CharField(widget = forms.Textarea)

-------views.py--------------

def send_email(request):
    if request.method != 'POST':
        form = EmailForm()
        return render_to_response('admin/Email.html', {'email_form': form})
	
    form = EmailForm(request.POST, request.FILES)	
    if form.is_valid():
        subject = form.cleaned_data['subject']
        message = form.cleaned_data['message']
        email = form.cleaned_data['email']
        attach = request.FILES['attach']
        try:
            mail = EmailMessage(subject, message, settings.EMAIL_HOST_USER, [email])
            mail.attach(attach.name, attach.read(), attach.content_type)
            mail.send()
            return render_to_response('admin/Email.html', {'message': 'Sent email to %s'%email})
       except:
           return render_to_response('admin/Error.html', {'message': 'Either the attachment is too  big or corrupt'})
       return render_to_response('admin/Email.html', {'message': 'Unable to send email. Please try again later'})	

---------template------

{% extends "admin/base_site.html" %}

{% block stylesheet %}{% load adminmedia %}{% admin_media_prefix %}css/dashboard.css{% endblock %}
{% block bodyclass %}dashboard{% endblock %}
{% block coltype %}colMS{% endblock %}

{% if not is_popup %}{% block breadcrumbs %}<div class="breadcrumbs"><a href="../">Home</a> &rsaquo; Send Email</div>{% endblock %}{% endif %}

{% block content %}
{{message}}
{% if email_form %}
<form method="POST" action ="." enctype="multipart/form-data">
<br></br>
{{email_form.as_p}}

<label>&nbsp;</label><label>&nbsp;</label><label>&nbsp;</label>
<input type ="submit"  name = "send" value = "Send"/>
</form>
{% endif %}
{% endblock content %}

More like this

  1. Simple "html email with images" sender by andres_torres_marroquin 3 years, 1 month ago
  2. Sending html emails with inline attachments/images using Django by alexhayes 5 months, 2 weeks ago
  3. Email multiattachment and multireciepments by fordexa 5 years, 7 months ago
  4. HTML Email with Inline Attachments (images) by hunterford 3 years, 6 months ago
  5. Base64Field: base64 encoding field for storing binary data in Django TextFields by bikeshedder 4 years, 8 months ago

Comments

KpoH (on September 18, 2008):

you forget

from django.core.mail import EmailMessage

#

peterbe (on September 18, 2008):

Great snippet! Really simple. However, I would strongly suggest not using an empty except: statement. Find out what errors can happen that aren't "python related" and write them in the except statement. Alternatively you can do this:

try:
    ...lines of code...
except (ValueError, TypeError, AttributeError, KeyError):
    raise
except:
    return render_to_response('admin/Error.html', ...

#

(Forgotten your password?)