0
我正在一個窗體發送電子郵件哪裏有可選的附件。發送電子郵件與可選附件在Django
當我嘗試發送電子郵件時未附加文件。 我得到這個錯誤
重點「文件」中MultiValueDict未發現:{}
任何想法,我做錯了什麼?我想直接發送到電子郵件地址,而無需將文件上傳到我們的服務器
forms.py
class jopcion(forms.Form):
subject = forms.CharField(max_length=100)
thecontent = forms.CharField(widget=forms.Textarea)
file = forms.FileField(widget= forms.FileInput (attrs={'name': 'file'}),required=False)
views.py
def novo(request, template_name='mailme.html'):
if request.method == "POST":
formulario = jopcion(request.POST or None, request.FILES or None)
if formulario.is_valid():
subject = request.POST['subject']
message = request.POST['thecontent']
attach = request.FILES['file']
destination = '[email protected]'
html_content = (subject,message,attach)
msg = EmailMultiAlternatives('Customer email address', html_content, 'fro[email protected]', [destination])
msg.attach(attach.name, attach.read(), attach.content_type)
msg.attach_alternative(html_content, 'text/html')#definimos el contenido como html
msg.send() #enviar en correo
return render_to_response('done.html', context_instance=RequestContext(request))
else:
formulario = jopcion()
ctx = {'form': formulario, 'text_dc': file}
return render_to_response(template_name, ctx , context_instance = RequestContext(request))
您的代碼在嘗試獲取並將其附加到電子郵件之前未檢查是否存在'FILES ['file']'。如果它是可選的,那麼您需要在嘗試附加它之前首先檢查它是否存在。 – solarissmoke