gpt4 book ai didi

django 替代 EMAIL_HOST 设置

转载 作者:行者123 更新时间:2023-12-02 05:03:33 26 4
gpt4 key购买 nike

我想实现 django 管理命令,它通过 smtp 发送电子邮件,而不是来自 settings.py 文件的默认设置,例如:

EMAIL_HOST
EMAIL_HOST_USER
EMAIL_HOST_PASSWORD
FROM_MAIL
EMAIL_USE_TLS

我想发送不同于 settings.py 的替代设置,而不更改所有站点的电子邮件设置。

如何实现?

最佳答案

定义您的备用电子邮件设置,然后使用这些设置创建新的邮件连接:

设置.py

ALTERNATE_EMAIL_HOST_PASSWORD = 'your password'
ALTERNATE_EMAIL_HOST_USER = 'your user'
ALTERNATE_EMAIL_HOST = ''
ALTERNATE_EMAIL_PORT = 123
ALTERNATE_EMAIL_USE_TLS = True

然后使用这些设置创建新连接:

from django.core import mail
from django.core.mail import send_mail
from django.conf import settings

# create new connection
connection = mail.get_connection()
connection.password = settings.ALTERNATE_EMAIL_HOST_PASSWORD
connection.username = settings.ALTERNATE_EMAIL_HOST_USER
connection.host = settings.ALTERNATE_EMAIL_HOST
connection.port = settings.ALTERNATE_EMAIL_PORT
connection.use_tls = settings.ALTERNATE_EMAIL_USE_TLS

# send email using new connection you just created
send_mail('my subject', 'my message', settings.DEFAULT_FROM_EMAIL,
['abc@gmail.com'], connection=connection)

关于django 替代 EMAIL_HOST 设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16624238/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com