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

时间:2017-08-19 15:31:20

标签: python django smtp

我有一个漂亮的联系表格"我自己写的模板(博客摘录),但表单需要PHP脚本。

由于我刚学习Django / Python,我现在还不想开始摆弄PHP。

要使表单有效,我需要做些什么?

HTML表单:

<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>

我在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 个答案:

答案 0 :(得分:0)

您应该阅读有关Django Forms的信息:https://docs.djangoproject.com/en/1.11/topics/forms/

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

这两个主题可以帮助你。