无法在Django 2.3中获取要工作的send_mail。没有错误消息。没有指示电子邮件已发送或未发送的日志消息
因此,我一直在网上搜索,试图让我的代码发挥作用,我觉得我已经阅读了关于这个问题的每一篇文章,但仍然没有弄清楚为什么我不能让我的表格给我发送电子邮件。我创建了一个基于类的视图,它继承了FormView,并编写了一个方法,可以在任何时候有post请求时向我发送电子邮件。就我个人而言,我无法让它工作 对于那些同舟共济的人来说,这是一个似乎很有希望的帖子,所以希望这能帮助你,即使对我没有帮助: My views.py:(两个电子邮件地址相同。它应该模拟我向自己发送电子邮件。) my forms.py:无法在Django 2.3中获取要工作的send_mail。没有错误消息。没有指示电子邮件已发送或未发送的日志消息,django,python-3.x,forms,email,Django,Python 3.x,Forms,Email,因此,我一直在网上搜索,试图让我的代码发挥作用,我觉得我已经阅读了关于这个问题的每一篇文章,但仍然没有弄清楚为什么我不能让我的表格给我发送电子邮件。我创建了一个基于类的视图,它继承了FormView,并编写了一个方法,可以在任何时候有post请求时向我发送电子邮件。就我个人而言,我无法让它工作 对于那些同舟共济的人来说,这是一个似乎很有希望的帖子,所以希望这能帮助你,即使对我没有帮助: My views.py:(两个电子邮件地址相同。它应该模拟我向自己发送电子邮件。) my forms.py:
from django import forms
class UploadResumeForm(forms.Form):
first_name = forms.CharField(
widget=forms.TextInput(
attrs={
'type':'text',
'class': 'form-control',
'placeholder': 'First Name',
}),
required=True)
my settings.py(变量存储在一个.env文件中,我使用python decouple添加它们,而不公开github上的信息。但这些是它们的关联值)
URL.py:
from django.urls import path, re_path
from .views import CandRegisterView
re_path(r'^candidate-register/$', CandRegisterView.as_view(success_url="/candidate-register/"), name='cand_register'),
候选者注册码.html:
<form method= "post" action="" accept-charset="UTF-8" role="form">
{% csrf_token %}
<fieldset>
<div class="form-group">
<div class="input-group input-group-lg">
<span class="input-group-addon"><i class="fa fa-fw fa-user"></i></span>
{{form.first_name}}
<!-- <input type="text" class="form-control" placeholder="First Name" name={{form.first_name}}> -->
</div>
</div>
<input class="btn btn-lg btn-primary btn-block" type="submit" value="Send Email">
</fieldset>
</form>
我希望它能做的就是现在就给我发一个人的名字。后来我希望他们能给我发一份带有简历的文件,但我想我会从简单开始,让它变得更复杂,包括其他领域,但我甚至不能让它工作。任何帮助或提示都将不胜感激。似乎Post请求正在发生,但电子邮件未发送。我还尝试在python manage.py shell中使用send_mail函数,它显示了一个看起来很有希望的响应(日志显示了电子邮件的样子),但它没有向我的帐户发送电子邮件 在form类中,您需要定义一个可以在form\u valid之后调用的send\u mail函数。例如:
from django.core.mail import EmailMessage as email_msg
class UploadResume(forms.Form):
first_name = forms.CharField()
last_name = forms.CharField()
email = forms.EmailField()
resume_file = forms.FileField()
class Meta:
title = 'Resume Upload'
def send_message(self, email, first_name, last_name, file):
'''
This function will be used to create an email object that will then be \
transmitted via a connection call. This function takes in arguments that will \
be provided by the corresponding CBV's form_valid function.
'''
email_obj = email_msg(
subject=f'Resumed Uploaded by {first_name} {last_name}!',
body = 'You received a resume upload from {first_name} {last_name} \
at {email}. Please follow-up. \nThank you,\nYour Platform'
from_email=email,
to=['myemail@gmail.com'],
reply_to= ['myemail@gmail.com']
)
# after creating the email object with the cleaned data generated from
# the form fields, we will attach the resume contents to the object,
# then submit it via SMTP settings from the settings.py file.
attach_name = file.name
try:
attach_content = file.open().read()
except Exception:
attach_content = file.getvalue()
attach_mimetype = mimetypes.guess_type(attach_name)[0]
email_obj.attach(attach_name, attach_content, attach_mimetype)
try:
email_obj.send()
except Exception as e:
print(type(e), e.args, e)
从这里,您可以覆盖CBV中的form\u valid函数,使用form.cleaned\u data.get(insert\u arg\u here)
函数提取相关参数
如何做到这一点的示例如下:
class CandRegisterView(FormView):
template_name = 'website/candidate_register.html'
form_class = UploadResumeForm
def form_valid(self, form):
if form.is_valid:
email_addr = form.cleaned_data.get('email')
first_name = form.cleaned_data.get('first_name')
last_name = form.cleaned_data.get('last_name')
file = form.cleaned_data.get('resume_file')
form.send_message(email_addr, first_name, last_name, file)
else:
return form.errors
刚刚注意到这一点:当您在HTML中制作表单标记时,请确保标记中存在
enctype=“multipart/form data”
。因此我部分解决了这个问题。我遇到了这个堆栈溢出的答案,它与使用自己的密码设置邮件应用程序有关。尽管如此,我仍然不明白为什么我的代码没有发送电子邮件。我可以从shell中完成,但不能从代码中完成。这太棒了。它仍然不起作用,但这肯定会有助于清理我收到的电子邮件。“覆盖表单的有效功能”到底是什么意思?为了更好地回答这个问题,我更新了答案。我认为我的示例基本上可以复制并粘贴到您的文件中。基本上,通用FormView有一个特定的表单验证功能,在管理表单数据时执行该功能,并实现特定的功能(如发送电子邮件),则最好在调用form.is\u valid()
后获取已清理的数据。
[10/Jul/2019 13:19:21] "POST /candidate-register/ HTTP/1.1" 302 0
[10/Jul/2019 13:19:22] "GET /candidate-register/ HTTP/1.1" 200 16782
[10/Jul/2019 13:19:22] "GET /candidate-register/ HTTP/1.1" 200 16782
from django.core.mail import EmailMessage as email_msg
class UploadResume(forms.Form):
first_name = forms.CharField()
last_name = forms.CharField()
email = forms.EmailField()
resume_file = forms.FileField()
class Meta:
title = 'Resume Upload'
def send_message(self, email, first_name, last_name, file):
'''
This function will be used to create an email object that will then be \
transmitted via a connection call. This function takes in arguments that will \
be provided by the corresponding CBV's form_valid function.
'''
email_obj = email_msg(
subject=f'Resumed Uploaded by {first_name} {last_name}!',
body = 'You received a resume upload from {first_name} {last_name} \
at {email}. Please follow-up. \nThank you,\nYour Platform'
from_email=email,
to=['myemail@gmail.com'],
reply_to= ['myemail@gmail.com']
)
# after creating the email object with the cleaned data generated from
# the form fields, we will attach the resume contents to the object,
# then submit it via SMTP settings from the settings.py file.
attach_name = file.name
try:
attach_content = file.open().read()
except Exception:
attach_content = file.getvalue()
attach_mimetype = mimetypes.guess_type(attach_name)[0]
email_obj.attach(attach_name, attach_content, attach_mimetype)
try:
email_obj.send()
except Exception as e:
print(type(e), e.args, e)
class CandRegisterView(FormView):
template_name = 'website/candidate_register.html'
form_class = UploadResumeForm
def form_valid(self, form):
if form.is_valid:
email_addr = form.cleaned_data.get('email')
first_name = form.cleaned_data.get('first_name')
last_name = form.cleaned_data.get('last_name')
file = form.cleaned_data.get('resume_file')
form.send_message(email_addr, first_name, last_name, file)
else:
return form.errors