Django - 发送电子邮件在shell中工作,但不在localhost上工作 - ConnectionRefusedError

时间:2017-09-12 01:48:28

标签: python django email localhost connection-refused

我是Django的新手,但我读过大约50页(主要是SO和Django文档)并没有发现任何适合我情况的东西。

我只是想通过Django中的send_mail函数发送电子邮件;虽然我希望通过Gmail上的SMTP工作,但现在它甚至不能通过localhost工作。当我运行我正在测试此send_mail函数的test_email.py文件时,出现以下错误:

ConnectionRefusedError :[WinError 10061]无法建立连接,因为目标计算机主动拒绝它

当我在shell中运行这个相同的send_mail调用(通过python manage.py shell)时,它完全没问题并按照我的预期显示电子邮件。只有在我尝试在Django中运行它时才会弹出错误。请注意,无论我在 settings.py 文件中添加以下两组设置的哪种排列,我都会收到上述错误:

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
EMAIL_HOST = 'localhost'
EMAIL_PORT = 1025
EMAIL_HOST_USER = 'myemail@gmail.com'
EMAIL_HOST_PASSWORD = ''
EMAIL_USE_TLS = False

我还在settings.py文件中使用了以下内容,以及这些设置与上一组的各种组合(虽然我想我已经超越了自己,看看localhost是如何工作的):< / p>

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'myemail@gmail.com' # Note I'm using my actual gmail address here in real life
EMAIL_HOST_PASSWORD = '****' # Note I'm using my actual gmail password here in real life

DEFAULT_FROM_EMAIL = 'myemail@gmail.com' # Note I'm using my actual gmail address here in real life
SERVER_EMAIL = 'myemail@gmail.com' # Note I'm using my actual gmail address here in real life

当我尝试运行localhost路由时,我在CMD提示符下运行以下命令:

python -m smtpd -n -c DebuggingServer localhost:1025

这是我正在尝试在Django中运行的 test_email.py 文件,我收到连接拒绝错误:

from django.core.mail import send_mail,EmailMessage

from django.conf import settings
settings.configure()

# Create your tests here.

# Email test #1: send email from my personal email to myself
send_mail(
    'Email Test #1',
    'Test if I can send email from my personal Gmail account to another email account via Django project.',
    settings.EMAIL_HOST_USER,
    ['myemail@gmail.com'],
    fail_silently=False
    )

对于为什么这甚至不通过localhost工作或者有关其他事情的建议的任何想法都将非常感激。

谢谢!

4 个答案:

答案 0 :(得分:1)

我认为gmail smtp服务存在一些问题。尝试使用smtp服务表单sendgrid。它也是免费的基本用法。我早些时候也面临同样的问题。

项目settings.py

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'sendgridusername'
EMAIL_HOST_PASSWORD = 'sedgridpasseord'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'yourmail@site.com'
  

仍然是错误

Try if `smtp` service is working on your your server.

答案 1 :(得分:0)

以下配置对我有用:

settings.py

EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey' # this is exactly the value 'apikey'
EMAIL_HOST_PASSWORD = 'sendgrid-api-key' # this is your API key
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
DEFAULT_FROM_EMAIL = 'your-email@example.com' # this is the sendgrid email

我使用了这种配置,并且可以正常工作,强烈建议使用环境变量来填充EMAIL_HOST_PASSWORDDEFAULT_FROM_EMAIL

import os # this should be at the top of the file

# ...

EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey'
EMAIL_HOST_PASSWORD = os.getenv("EMAIL_HOST_PASSWORD", "")
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
DEFAULT_FROM_EMAIL = os.getenv("DEFAULT_FROM_EMAIL", "")

要发送电子邮件,请使用以下代码:

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

send_mail('This is the title of the email',
          'This is the message you want to send',
          settings.DEFAULT_FROM_EMAIL,
          [
              settings.EMAIL_HOST_USER, # add more emails to this list of you want to
          ]
)

答案 2 :(得分:0)

请确保您的设置位于project settings.py而非app settings.py中,如下所示:

EMAIL_HOST = 'localhost'
EMAIL_PORT = 1025
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
EMAIL_USE_TLS = False

请注意EMAIL_HOST_USER的空字符串,我认为这是您的错误

使用以下命令运行虚拟python SMTP

python -m smtpd -n -c DebuggingServer localhost:1025

您将可以毫无问题地发送到本地smtp服务器。 Source

答案 3 :(得分:0)

确保您的邮件和密码正确后,尝试在Google帐户中启用此选项。

https://myaccount.google.com/lesssecureapps?pli=1

google阻止访问不安全的应用程序。

您的代码似乎正确!