Django 电子邮件附加方法没有正确获取参数
Django email attach method is not taking parameters right
我的问题是在 django 中发送电子邮件时我想附加一个文件。
如果我这样做:
email.attach("Random_name", uploaded_file.read())
有效,我的电子邮件已发送。但是如果不是字符串 "Random name" 我在那里放了一个代表上传文件名的变量:
uploaded_file = request.FILES['stl_file']
uploaded_file_name = request.FILES['stl_file'].name
email.attach(uploaded_file_name, uploaded_file.read())
整个事情都崩溃了,我得到了 email.send() 方法的 ValueError "need more than 1 value to unpack"。我已经检查了变量 upload_file 和 upload_file_name(通过使用 pdb 工具)并且在调用附加方法之前它们都获得了正确的值。
这是我尝试发送邮件的视图:
def print(request):
if request.method == 'POST':
form = PrintForm(data=request.POST, request = request)
if form.is_valid():
contact_name = request.POST.get('contact_name', '')
contact_email = request.POST.get('contact_email', '')
form_content = request.POST.get('content', '')
supervisor = form.cleaned_data['supervisor']
template = get_template('threeD/email/contact_template_for_printing.txt')
context = Context({
'contact_name': contact_name,
'supervisor': supervisor,
'contact_email': contact_email,
'form_content': form_content,
})
content = template.render(context)
subject = "New message"
email = EmailMessage(
subject,
content,
contact_email,
[supervisor],
headers={'Reply-To': contact_email}
)
if request.FILES:
uploaded_file = request.FILES['stl_file']
uploaded_file_name = request.FILES['stl_file'].name
email.attach(uploaded_file_name, uploaded_file.read())
email.send()
messages.success(request, "Thank you for your message.")
return redirect('/index/print/')
else:
form = PrintForm(request=request)
context_dict = {}
context_dict['printers'] = Printer.objects.all()
context_dict['form'] = form
return render(request, 'threeD/print.html', context_dict)
和我的表格:
class PrintForm(forms.Form):
contact_name = forms.CharField(required=True)
contact_email = forms.EmailField(required=True)
supervisor = forms.ChoiceField(
choices=[(str(sup.email), str(sup.name)) for sup in Supervisors.objects.all()]
)
stl_file = forms.FileField(required=False)
stl_file.help_text = "Upload your file as .STL format. If you have more than one file, " \
"make a .zip and upload them all at once"
content = forms.CharField(
required=True,
widget=forms.Textarea
)
所以我得到的错误是这样的:
http://dpaste.com/2YZQ941
如有任何帮助,我将不胜感激。
我正在使用 Django 1.9 版本
已解决
最后硬编码文件类型为'application/octet-stream',如:
uploaded_file = request.FILES['stl_file']
uploaded_file_name = request.FILES['stl_file'].name
email.attach(uploaded_file_name, uploaded_file.read(), 'application/octet-stream')
email.send()
我认为它需要一种内容类型,也许可以尝试更多类似的东西
uploaded_file = form.cleaned_data.get('stl_file', '')
email.attach(uploaded_file.name, uploaded_file.read(), uploaded_file.content_type)
我的问题是在 django 中发送电子邮件时我想附加一个文件。
如果我这样做:
email.attach("Random_name", uploaded_file.read())
有效,我的电子邮件已发送。但是如果不是字符串 "Random name" 我在那里放了一个代表上传文件名的变量:
uploaded_file = request.FILES['stl_file']
uploaded_file_name = request.FILES['stl_file'].name
email.attach(uploaded_file_name, uploaded_file.read())
整个事情都崩溃了,我得到了 email.send() 方法的 ValueError "need more than 1 value to unpack"。我已经检查了变量 upload_file 和 upload_file_name(通过使用 pdb 工具)并且在调用附加方法之前它们都获得了正确的值。
这是我尝试发送邮件的视图:
def print(request):
if request.method == 'POST':
form = PrintForm(data=request.POST, request = request)
if form.is_valid():
contact_name = request.POST.get('contact_name', '')
contact_email = request.POST.get('contact_email', '')
form_content = request.POST.get('content', '')
supervisor = form.cleaned_data['supervisor']
template = get_template('threeD/email/contact_template_for_printing.txt')
context = Context({
'contact_name': contact_name,
'supervisor': supervisor,
'contact_email': contact_email,
'form_content': form_content,
})
content = template.render(context)
subject = "New message"
email = EmailMessage(
subject,
content,
contact_email,
[supervisor],
headers={'Reply-To': contact_email}
)
if request.FILES:
uploaded_file = request.FILES['stl_file']
uploaded_file_name = request.FILES['stl_file'].name
email.attach(uploaded_file_name, uploaded_file.read())
email.send()
messages.success(request, "Thank you for your message.")
return redirect('/index/print/')
else:
form = PrintForm(request=request)
context_dict = {}
context_dict['printers'] = Printer.objects.all()
context_dict['form'] = form
return render(request, 'threeD/print.html', context_dict)
和我的表格:
class PrintForm(forms.Form):
contact_name = forms.CharField(required=True)
contact_email = forms.EmailField(required=True)
supervisor = forms.ChoiceField(
choices=[(str(sup.email), str(sup.name)) for sup in Supervisors.objects.all()]
)
stl_file = forms.FileField(required=False)
stl_file.help_text = "Upload your file as .STL format. If you have more than one file, " \
"make a .zip and upload them all at once"
content = forms.CharField(
required=True,
widget=forms.Textarea
)
所以我得到的错误是这样的:
http://dpaste.com/2YZQ941
如有任何帮助,我将不胜感激。
我正在使用 Django 1.9 版本
已解决
最后硬编码文件类型为'application/octet-stream',如:
uploaded_file = request.FILES['stl_file']
uploaded_file_name = request.FILES['stl_file'].name
email.attach(uploaded_file_name, uploaded_file.read(), 'application/octet-stream')
email.send()
我认为它需要一种内容类型,也许可以尝试更多类似的东西
uploaded_file = form.cleaned_data.get('stl_file', '')
email.attach(uploaded_file.name, uploaded_file.read(), uploaded_file.content_type)