如何使用Django表单发送有关表单提交的电子邮件? [英] How to send email on form submission using django forms?

查看:95
本文介绍了如何使用Django表单发送有关表单提交的电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在实际网站上发送邮件之前,我正在运行一个小型测试Django本地SMTP服务器:
我必须在 settings.py 中配置这些设置。但是我应该放在哪里?

Prior to sending mails on actual website, I'm running a small test Django local SMTP server: I have to congifure these settings in settings.py. But where do I put it?

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'localhost'
EMAIL_PORT = '1025'
EMAIL_USE_TLS = True

那我该如何进行呢?请帮忙。

Then how do I proceed? Please help.

推荐答案

您需要将它们放入 settings.py 文件。您可以将以下设置放在 settings.py 文件中的任何位置。

You need to put these in your settings.py file. You can put the below settings anywhere in your settings.py file.

settings.py

...

EMAIL_HOST = 'smtp.gmail.com'  # since you are using a gmail account
EMAIL_PORT = 587  # Gmail SMTP port for TLS
EMAIL_USE_TLS = True    
...

local_settings.py

EMAIL_HOST_USER = 'your_username@gmail.com'
EMAIL_HOST_PASSWORD = 'your_gmail_password'

然后发送电子邮件,即可使用Django的 send_email()

Then to send an email, you can use Django's send_email().

from django.core.mail import send_mail

send_mail('My Subject', 'My message', 'from@example.com',
    ['to@example.com'], fail_silently=False)

然后使用 EMAIL_HOST settings.py 文件中的c>和 EMAIL_PORT 设置。 EMAIL_HOST_USER EMAIL_HOST_PASSWORD 设置(如果已设置)用于对SMTP服务器进行身份验证,而 EMAIL_USE_TLS 设置控制是否使用安全连接。

Mail is then sent using the SMTP host and port specified in the EMAIL_HOST and EMAIL_PORT settings in your settings.py file. The EMAIL_HOST_USER and EMAIL_HOST_PASSWORD settings, if set, are used to authenticate to the SMTP server, and the EMAIL_USE_TLS settings control whether a secure connection is used.

send_email()的签名:


send_mail(主题,消息,from_email,收件人列表,
fail_silently = False,auth_user = None,auth_password = None,
连接=无,html_message =无)

send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=None, auth_password=None, connection=None, html_message=None)

注意:您可以将gmail帐户信息放在 local_settings.py 并将此 local_settings 添加到git-ignore中。然后将此local_settings包含在您的 settings.py 中。这样,只有您才能看到您的 local_settings

Note: You can put your gmail account information in a local_settings.py and add this local_settings to git-ignore. Then include this local_settings in your settings.py. This way only you would be able to see your local_settings.

这篇关于如何使用Django表单发送有关表单提交的电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆