使用电子邮件模板从Django发送电子邮件

时间:2022-10-23 19:19:18

I have a pretty "contact form" template that I haven't written myself (snippet from a blog), but the form calls for a PHP script.

我有一个漂亮的“联系表单”模板,我自己没有写过(来自博客的片段),但表单需要PHP脚本。

Since I am just learning Django/Python, I wouldn't want to start fiddling with PHP just yet.

由于我只是学习Django / Python,我不想开始摆弄PHP。

What would I need to do to make the form work?

我需要做些什么才能使表单生效?

HTML form:

<form id="ajax-contact"  method="post" action="contact-form-mail.php" role="form">
<div class="messages" id="form-messages"></div>

<div class="form-group">
<label for="form_name">Name</label>
<input id="form_name" type="text" name="name" class="form-control" placeholder="name">
<div class="help-block with-errors"></div>
</div>

<div class="form-group">
<label for="form_email">Email</label>
<input id="form_email" type="email" name="email" class="form-control" placeholder="email" required="required" data-error="Valid email is required.">
<div class="help-block with-errors"></div>
</div>

<div class="form-group">
<label for="form_message">Message</label>
<textarea id="form_message" name="message" class="form-control" placeholder="Message" rows="4" required="required" data-error="leave a message."></textarea>
<div class="help-block with-errors"></div>
</div>

<input type="submit" class="btn btn-light" value="Send message">

<br>
<small class="text-muted"><strong>*</strong> These fields are required.</small>

</form>

I've set up SMTP settings as follows in settings.py:

我在settings.py中设置了以下SMTP设置:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'myemail@gmail.com'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'myemail@gmail.com'
SERVER_EMAIL = 'myemail@gmail.com'

1 个解决方案

#1


0  

You should read up about Django Forms: https://docs.djangoproject.com/en/1.11/topics/forms/

您应该阅读Django Forms:https://docs.djangoproject.com/en/1.11/topics/forms/

And Django Email: https://docs.djangoproject.com/en/1.11/topics/email/

和Django电子邮件:https://docs.djangoproject.com/en/1.11/topics/email/

This two topics should help you out.

这两个主题应该可以帮到你。

#1


0  

You should read up about Django Forms: https://docs.djangoproject.com/en/1.11/topics/forms/

您应该阅读Django Forms:https://docs.djangoproject.com/en/1.11/topics/forms/

And Django Email: https://docs.djangoproject.com/en/1.11/topics/email/

和Django电子邮件:https://docs.djangoproject.com/en/1.11/topics/email/

This two topics should help you out.

这两个主题应该可以帮到你。