Django电子邮件附加方法没有正确的参数

时间:2017-04-18 15:42:44

标签: python django forms filefield

我的问题是,在django发送电子邮件时,我想附加一个文件。

如果我这样做:

email.attach("Random_name", uploaded_file.read()) 

它有效,我的电子邮件发送。但如果不是字符串“随机名称”,我将表示上传文件名的变量放在:

        uploaded_file = request.FILES['stl_file']
        uploaded_file_name = request.FILES['stl_file'].name
        email.attach(uploaded_file_name, uploaded_file.read())

整个事情一团糟,我得到一个ValueError“需要超过1个值才能解压缩”的email.send()方法。我检查了变量upload_file和upload_file_name(通过使用pdb工具),并且在调用attach方法之前它们都获得了正确的值。
以下是我在尝试发送邮件的地方:

    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()

1 个答案:

答案 0 :(得分:1)

我认为它需要一种内容类型,也许可以尝试更像这样的内容

uploaded_file = form.cleaned_data.get('stl_file', '')
email.attach(uploaded_file.name, uploaded_file.read(), uploaded_file.content_type)