当详细信息来自数据库时,电子邮件发送django返回错误



我有一个在django帮助文件中发送邮件的函数,我的函数在下面给出

def mail_send(data):
result = {}
getTemplate = EmailTemplate.objects.filter(pk=data['type']).first()
if getTemplate != None:
templates = Template(getTemplate.template)
config = EmailConfiguration.objects.filter(pk=1).first()
context = Context(
{
'name': data['name'],
'password': data['password'],
'site_name': config.site_name
}
)
msg_plain = 'Login Details ' + data['name'] + ' / ' + data['password']
msg_html = templates.render(context)
EMAIL_USE_TLS = config.tls
EMAIL_HOST = config.host
EMAIL_HOST_USER = config.from_email
EMAIL_HOST_PASSWORD = config.password
EMAIL_PORT = config.port
mail = send_mail(
data['msg'],
msg_plain,
EMAIL_HOST_USER,
[data['email']],
fail_silently=False,
html_message=msg_html,
)
else:
result['msg'] = 'Template Not Found .Unable to send Email..'
result['status'] = False

它返回给我的错误如下:[Erno 111]连接拒绝

但是当我把所有这些设置都放在settings.py中时,它对我来说很好,但我不想要这个。我想从数据库中来并发送邮件。请建议我从最后一天起就被困在这里。我是django的新手,所以对我来说越来越困难

这是一个非常奇怪的设置,可能不是最好的做法。

尽管如此,Django仍然支持你。

让我们看看send_mail的签名

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

遗憾的是,我们不能在这里给它主机,但我们可以给它一个连接。

from django.core.mail import get_connection
def mail_send(data):
....
connection = get_connection(
host=config.host,
port=config.port,
username=config.from_email,
password=config.password,
use_tls=config.tls,
)
mail = send_mail(
data['msg'],
msg_plain,
config.from_email,
[data['email']],
fail_silently=False,
html_message=msg_html,
connection=connection,
)

最新更新