与Djang发送邮件失败

2024-03-29 08:20:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用django.core.mail发送电子邮件,但出现错误:

Request Method: POST

Request URL:    http://localhost:8000/accounts/signup/

Django Version: 1.11.10

Exception Type: gaierror

Exception Value:    

[Errno -2] Name or service not known

Exception Location: /usr/lib/python2.7/socket.py in create_connection,
line 557

Python
Executable: /home/lucas/Documentos/projetos/advogados/venv/bin/python

Python Version: 2.7.12

在那之后,我试着用这样的python脚本发送电子邮件,效果很好:

import smtplib    
server = smtplib.SMTP('smtp.gmail.com', 587) 
server.starttls()  
server.login("my@email.com", "mypassword")    
msg = "YOUR MESSAGE!"  
server.sendmail("my@email.com", "to@email.com", msg) 
server.quit()

因此,我创建了自己的电子邮件发送器,用于Django:

import smtplib 
from django.conf import settings


def send_custom_mail(msg, to, subject):
    message = 'Subject: {}\n\n{}'.format(subject, msg)
    server = smtplib.SMTP(settings.EMAIL_HOST, settings.EMAIL_PORT)
    server.starttls()
    server.login(settings.EMAIL_HOST_USER, settings.EMAIL_HOST_PASSWORD)
    server.sendmail(settings.EMAIL_HOST_USER, to, message)
    server.quit()

它仍然不工作,我收到同样的错误。你知道吗

所以我删除了设置变量并使用了实际值,它可以工作!!!你知道吗

我意识到这个问题与设置变量有关,我记录了它们,它们是type,host的string,pass和user,port的int。好 啊!你知道吗

我不知道如何检查或修复,其他变量,如允许的\u主机和秘密\u密钥工作正常

我正在使用decouple lib从设置中删除敏感数据


Tags: todjangoimportcomhostsettingsserver电子邮件