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 2 years, 2 months ago
  2. TinyIntegerField by Lacour 1 year, 9 months ago
  3. Partial OpenID provider implementation from idproxy.net by simon 5 years, 10 months ago
  4. FileInput with download like (like the fileinput on admin) by zodman 2 years, 2 months ago
  5. Take a django.core.mail email message and replace any MEDIA_URL-served images with attached images by agrossman@gmail.com 1 year, 3 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', ...

#

DorthyBanks23 (on December 31, 2011):

This is known that cash can make people independent. But how to act when someone does not have money? The only one way is to receive the home loans or credit loan.

#

(Forgotten your password?)