在django发送带有附件的电子邮件 [英] Sending emails with attachment in django
问题描述
我正在尝试发送附有django的一些图像的电子邮件。使用的代码是此代码段: http://www.djangosnippets.org/snippets/1063/。 Dunno为什么附件部分给我一个核心错误。
代码。 form.py
从django导入表单
从通用导入slugify_unique
从django.conf导入设置$来自django.core.cache的缓存
从django.contrib.admin导入小部件
从django.shortcuts导入get_object_or_404
类WorkForm(forms.Form):
name = forms.CharField(label ='Name and surname',max_length = 64,required = True)
nick = forms.CharField(label ='nickname',max_length = 40,required = True)
email = forms.EmailField(label ='e-mail',required = True)
image1 = forms.Field(label ='sample photo',widget = forms.FileInput,required = True)
image2 = forms.Field(label ='sample photo',widget = forms.FileInput,required = True)
image3 = forms.Field(label ='sample photo',widget = forms.FileInput,required = True)
text = forms.CharField(label ='关于你的几句话',widget = forms.Textarea, required = False)
views.py
从表单导入WorkForm
从django.core.mail导入send_mail,EmailMessage
def工作(请求):
template ='other / work.html'
如果request.method =='POST':
form = WorkForm(request.POST,request.FILES)
如果表单。 is_valid():
name = form.cleaned_data ['name']
nick = form.cleaned_data ['nick']
email = form.cleaned_data ['email']
subject ='Work'
text = form.cleaned_data ['text']
image1 = request.FILES ['image1']
image2 = request.FILES ['image2']
image3 = request.FILES ['image3']
try:
mail = EmailMessage(subject,text,['EMAIL_ADDRESS'],[email])
mail.attach(image1.name ,附上.read(),attach.content_type)
mail.attach(image2.name,attach.read(),attach.content_type)
mail.attach(image3.name,attach.read(),attach
mail.send()
template ='other / mail_sent.html'
除了:
返回附件错误
返回render_to_response(template,{ 'form':form},
context_instance = RequestContext(request))
else:
form = WorkForm()
return render_to_response(template,{'form':form}
context_instance = RequestContext(request))
这里是错误网站图片:
http://img201.imageshack.us/img201/6027/coreerror.png
我做错了什么?
您发布的错误追踪与实际代码似乎没有任何关系 - 它似乎是中间件的某种问题(大概在渲染500个错误页面时)。 / p>
但是,您的错误可能是由于使用未定义的变量名称 attach
在调用 mail.attach
。您没有附加
变量 - 您已经调用了发布的文件 image1
等,所以你应该使用
mail.attach(image1.name,image1.read(),image1.content_type)
/ pre>
邮件。 attach(image2.name,image2.read(),image2.content_type)
mail.attach(image3.name,image3.read(),image3.content_type)
I'm trying to send email with some images attached in django. Code used is this snippet : http://www.djangosnippets.org/snippets/1063/. Dunno why the attachment part returns me a core error.
THe code. forms.py
from django import forms from common import slugify_unique from django.conf import settings from django.core.cache import cache from django.contrib.admin import widgets from django.shortcuts import get_object_or_404 class WorkForm(forms.Form): name = forms.CharField(label='Name and surname', max_length=64, required = True ) nick = forms.CharField(label='nickname', max_length=40, required = True ) email = forms.EmailField(label='e-mail', required = True ) image1 = forms.Field(label='sample photo', widget = forms.FileInput, required = True ) image2 = forms.Field(label='sample photo', widget = forms.FileInput, required = True ) image3 = forms.Field(label='sample photo', widget = forms.FileInput, required = True ) text = forms.CharField(label='Few words about you', widget=forms.Textarea, required = False )
views.py
from forms import WorkForm from django.core.mail import send_mail, EmailMessage def work(request): template = 'other/work.html' if request.method == 'POST': form = WorkForm(request.POST, request.FILES) if form.is_valid(): name = form.cleaned_data['name'] nick = form.cleaned_data['nick'] email = form.cleaned_data['email'] subject = 'Work' text = form.cleaned_data['text'] image1 = request.FILES['image1'] image2 = request.FILES['image2'] image3 = request.FILES['image3'] try: mail = EmailMessage(subject, text, ['EMAIL_ADDRESS'], [email]) mail.attach(image1.name, attach.read(), attach.content_type) mail.attach(image2.name, attach.read(), attach.content_type) mail.attach(image3.name, attach.read(), attach.content_type) mail.send() template = 'other/mail_sent.html' except: return "Attachment error" return render_to_response(template, {'form':form}, context_instance=RequestContext(request)) else: form = WorkForm() return render_to_response(template, {'form':form}, context_instance=RequestContext(request))
And here's error site image : http://img201.imageshack.us/img201/6027/coreerror.png What am I doing wrong ?
解决方案The error traceback you've posted doesn't seem to have anything to do with the actual code - it seems to be some sort of problem with middleware (presumably when rendering the 500 error page).
However, your error is probably caused by your use of the undefined variable name
attach
in the calls tomail.attach
. You don't have anattach
variable - you've called the posted filesimage1
etc, so you should use those names.mail.attach(image1.name, image1.read(), image1.content_type) mail.attach(image2.name, image2.read(), image2.content_type) mail.attach(image3.name, image3.read(), image3.content_type)
这篇关于在django发送带有附件的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!