在Django中从POST接收数据后发送电子邮件[重复]
Posted
技术标签:
【中文标题】在Django中从POST接收数据后发送电子邮件[重复]【英文标题】:Sendind email after receiving data from POST in Django [duplicate] 【发布时间】:2021-09-14 22:07:52 【问题描述】:我希望用户填写一个表单(使用 Django 模型表单制作),然后从 POST 接收数据并使用 django send_mail 函数通过 gmail 发送一份副本。
这是我的 settings.py 配置
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST_USER = 'anyone@gmail.com'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_PASSWORD = ''
这是我的意见.py
def GetOrder(request):
mymodalform = MyOrderForm(request.POST)
if mymodalform.is_valid():
# Save in DB
mymodalform.save()
# Send a copy via gmail to anyone@gmail.com
email_subject = 'Order Credentials/Information from ' + mymodalform.name
# email_body = "Congratulations if you receive this email, then your order is already in treatment. Below are the order information..." + "Name : " + MyOrderForm.Name + "Phone Number : " + MyOrderForm.Phone + "Email Address : " + MyOrderForm.Email + "Additional Information : " + MyOrderForm.AdditionalInfo + "Ordered pamphlet : " + MyOrderForm.Product + "Number of copies ordered : " + MyOrderForm.Amount
#email_body = mymodalform.Product
from_email = 'anyone@gmail.com'
to_emails = ['anyone@gmail.com']
send_mail(
email_subject,
mymodalform.split(','),
from_email,
to_emails,
fail_silently=False
)
# Flash message to confirm order placement.
messages.add_message(
request,
messages.SUCCESS,
'Your order was placed successfully! We\'ll soon contact you to confirm the order.'
)
return redirect('Order')
这是我的forms.py
class MyOrderForm(forms.ModelForm):
#Metadata
class Meta:
model = MyOrderInfo
fields = [
'Name',
'Phone',
'Email',
'AdditionalInfo',
'Product',
'Amount',
]
# Definition of widgets
widgets =
'Name': forms.TextInput(
attrs=
'class': 'form-control',
'name': 'name',
'id': 'name',
'placeholder': 'Your Name...'
),
'Phone':forms.NumberInput(
attrs=
'class': 'form-control',
'name': 'organization',
'id': 'organization',
'placeholder': 'Your phone number...'
),
'Email': forms.EmailInput(
attrs=
'class': 'form-control',
'name': 'email',
'id': 'email',
'placeholder': 'Your Email address...'
),
'AdditionalInfo': forms.Textarea(
attrs=
'class': 'form-control',
'name': 'message',
'id': 'message',
'rows': '7',
'cols': '30',
'placeholder': 'Provide some extra information if any e.g. Your address (precisely), observations, Do you need delivery?, etc'
),
'Product': forms.Select(
choices= CHOICES,
attrs=
'class': 'custom-select',
'id': 'budget',
'name': 'budget'
),
'Amount': forms.NumberInput(
attrs=
'class': 'form-control',
'name': 'date',
'id': 'date',
'placeholder': 'Number of copies...'
)
需要有关如何通过POST
获取数据的帮助,然后通过电子邮件将其发送至anyone@gmail.com
。谢谢
【问题讨论】:
看起来应该可以工作......哪个部分不工作? @JoranBeasley。它可以工作,但 OP 可能想要加载模板django.template.loader.get_template()
,将 mymodalform
作为上下文传递,render
将输出作为 email_body
。
【参考方案1】:
只需创建一个表单,然后在 form.is_valid() 从字段数据中获取值然后发送电子邮件之后,在您的视图中(如另一个将数据保存到数据库的表单)。
@login_required
def sendmail(request):
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
name = form.cleaned_data['name']
email = form.cleaned_data['email']
message = form.cleaned_data['message'] + ' ' + "|| Senders email: " + f'email'
try:
send_mail(
name, #subject
message, #message
email, #from email
['sitemail.geo@gmail.com'], #to email
)
except BadHeaderError:
return httpResponse('Invalid header found')
messages.success(request, f'Thanks name! We received your email and will respond shortly...')
return redirect('home')
else:
form = ContactForm()
return render(request, 'sendmail/mail.html', 'form': form)
【讨论】:
以上是关于在Django中从POST接收数据后发送电子邮件[重复]的主要内容,如果未能解决你的问题,请参考以下文章