Html 用Django发送带有附件的Gmail

Html 用Django发送带有附件的Gmail,html,django,Html,Django,我试图发送Gmail的附件也在Django 这里是我的视图.py: def index(request): if request.method != 'POST': form = EmailForm() context = {'email_form': form} return render(request, 'app/index.html', context) form = EmailForm(request.POST, req

我试图发送Gmail的附件也在Django

这里是我的视图.py

def index(request):
    if request.method != 'POST':
        form = EmailForm()
        context = {'email_form': form}
        return render(request, 'app/index.html', context)

    form = EmailForm(request.POST, request.FILES)
    if form.is_valid():
        subject = form.cleaned_data['subject']
        message = form.cleaned_data['message']
        email = form.cleaned_data['email']
        attach = request.FILES['attach']
        try:
            mail = EmailMessage(subject, message, settings.EMAIL_HOST_USER, [email])
            mail.attach_file(attach.name, attach.read(), attach.content_type)
            mail.send()
            context = {'message': 'email sended'}
            print("email sended")
            return render(request, 'app/email_template.html', context)
        except:
            context = {'message': 'Either the attachment is too  big or corrupt'}
            print("Either the attachment is too  big or corrupt")
            return render(request, 'app/index.html', context)
        context = {'message': 'Unable to send email. Please try again later'}
        return render(request, 'app/index.html', context)
class EmailForm(forms.Form):
    email = forms.EmailField()
    subject = forms.CharField(max_length=100)
    attach = forms.Field(widget = forms.FileInput)
    message = forms.CharField(widget = forms.Textarea)
<div class="container-fluid">
    <div class="col-xl-4">
        <h1>Welcome in django's world</h1>

        {{message}}

        <form method="POST" action ="." enctype="multipart/form-data">
        {% csrf_token %}
        <br></br>
        {{email_form.as_p}}

        <label>&nbsp;</label><label>&nbsp;</label><label>&nbsp;</label>
        <input type ="submit"  name = "send" value = "Send"/>
        </form>

    </div>
</div>
forms.py

def index(request):
    if request.method != 'POST':
        form = EmailForm()
        context = {'email_form': form}
        return render(request, 'app/index.html', context)

    form = EmailForm(request.POST, request.FILES)
    if form.is_valid():
        subject = form.cleaned_data['subject']
        message = form.cleaned_data['message']
        email = form.cleaned_data['email']
        attach = request.FILES['attach']
        try:
            mail = EmailMessage(subject, message, settings.EMAIL_HOST_USER, [email])
            mail.attach_file(attach.name, attach.read(), attach.content_type)
            mail.send()
            context = {'message': 'email sended'}
            print("email sended")
            return render(request, 'app/email_template.html', context)
        except:
            context = {'message': 'Either the attachment is too  big or corrupt'}
            print("Either the attachment is too  big or corrupt")
            return render(request, 'app/index.html', context)
        context = {'message': 'Unable to send email. Please try again later'}
        return render(request, 'app/index.html', context)
class EmailForm(forms.Form):
    email = forms.EmailField()
    subject = forms.CharField(max_length=100)
    attach = forms.Field(widget = forms.FileInput)
    message = forms.CharField(widget = forms.Textarea)
<div class="container-fluid">
    <div class="col-xl-4">
        <h1>Welcome in django's world</h1>

        {{message}}

        <form method="POST" action ="." enctype="multipart/form-data">
        {% csrf_token %}
        <br></br>
        {{email_form.as_p}}

        <label>&nbsp;</label><label>&nbsp;</label><label>&nbsp;</label>
        <input type ="submit"  name = "send" value = "Send"/>
        </form>

    </div>
</div>
模板

def index(request):
    if request.method != 'POST':
        form = EmailForm()
        context = {'email_form': form}
        return render(request, 'app/index.html', context)

    form = EmailForm(request.POST, request.FILES)
    if form.is_valid():
        subject = form.cleaned_data['subject']
        message = form.cleaned_data['message']
        email = form.cleaned_data['email']
        attach = request.FILES['attach']
        try:
            mail = EmailMessage(subject, message, settings.EMAIL_HOST_USER, [email])
            mail.attach_file(attach.name, attach.read(), attach.content_type)
            mail.send()
            context = {'message': 'email sended'}
            print("email sended")
            return render(request, 'app/email_template.html', context)
        except:
            context = {'message': 'Either the attachment is too  big or corrupt'}
            print("Either the attachment is too  big or corrupt")
            return render(request, 'app/index.html', context)
        context = {'message': 'Unable to send email. Please try again later'}
        return render(request, 'app/index.html', context)
class EmailForm(forms.Form):
    email = forms.EmailField()
    subject = forms.CharField(max_length=100)
    attach = forms.Field(widget = forms.FileInput)
    message = forms.CharField(widget = forms.Textarea)
<div class="container-fluid">
    <div class="col-xl-4">
        <h1>Welcome in django's world</h1>

        {{message}}

        <form method="POST" action ="." enctype="multipart/form-data">
        {% csrf_token %}
        <br></br>
        {{email_form.as_p}}

        <label>&nbsp;</label><label>&nbsp;</label><label>&nbsp;</label>
        <input type ="submit"  name = "send" value = "Send"/>
        </form>

    </div>
</div>

现在我该如何解决这个问题,才能成功发送Gmail?

除了e:print(e.args)@c.grey例外,Expection打印-->是什么?我只想成功发送Gmail。是的,但你需要告诉我你收到了什么Django例外你在Gmail中启用了电子邮件发送吗?您必须向第三方应用程序授予权限。如@c.grey所述,将“除外”替换为“除外”,因为e:print(e)将帮助您进行批量打印,并适当地传递参数。。以下是expectation打印-->的链接,例外情况除外:e:print(e.args)@c.grey我只想成功发送Gmail。是的,但您需要告诉您获得的Django例外情况启用Gmail中的电子邮件发送了吗?您必须向第三方应用程序授予权限。如@c.grey所述,将“除外”替换为“除外”,因为e:print(e)将帮助您进行批量打印,并适当地传递参数。。这里是链接