2017-04-17 70 views
1

我想發送和Django電子郵件形式與附加文件,但我無法弄清楚如何發送文件(由用戶上傳)而不保存在本地。 我有我的方式:發送電子郵件與Django中附加文件

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()] 
    ) 
    file = forms.FileField() 
    content = forms.CharField(
     required=True, 
     widget=forms.Textarea 
    ) 

和我的觀點:

def print(request): 
    # context = dict() 
    # context['printers'] = Printer.objects.all() 
    # return render(request, 'threeD/print.html', context) 

    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" 

      try: 
       email = EmailMessage(
        subject, 
        content, 
        contact_email, 
        [supervisor], 
        headers={'Reply-To': contact_email} 
       ) 
       #email.attach(...) 
       email.send() 
      except: 
       return "Attachment error" 

      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) 

所以,在我看來,當我發送一封電子郵件,是有辦法叫email.attach(file)這將附加文件到郵件併發送它,但沒有本地保存文件? (表單和視圖工作正常,沒有文件功能實現)

編輯:
這裏是HTML代碼片段,在這裏我呼籲形式:

<div class="panel-body"> 
     <form role="form" action="" method="post"> 
      {% csrf_token %} 
      {% load bootstrap %} 
       {{ form|bootstrap }} 

      <div class="text-center"> 
       <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-send"></span> Send a message</button> 
      </div> 
     </form> 
</div> 

任何幫助將是非常讚賞。謝謝!

回答

0

事情是這樣的:

def send_email(request): 
    ... 
    email = EmailMessage(
     subject, 
     content, 
     contact_email, 
     [to], 
     headers={'Reply-To': contact_email} 
    ) 
    if request.FILES: 
     uploaded_file = request.FILES['file'] # file is the name value which you have provided in form for file field 
     email.attach(uploaded_file.name, uploaded_file.read(), uploaded_file.content_type) 
    email.send() 
+0

非常感謝您的幫助。當我說「file = forms.FileField()」時,我的表單中是否有任何錯誤?因爲使用你的方法進行視圖,它仍然不起作用,它寫道「此字段是必需的。」在提交後的字段附近,就像沒有附加文件一樣,即使它是 –

+0

編輯您的問題以添加表單的html代碼,並在控件進入視圖時添加request.FILES值。 –

+0

我已經添加了我的html代碼,這是相當原始的。我很抱歉地說,但恐怕我不知道如何獲得request.FILES價值時,控制權的意見。 :/ –