Django 发送电子邮件
- 2025-03-21 09:05:00
- admin 原创
- 36
问题描述:
我知道有 20 个问题与我的问题类似,但我已经尝试了一天多的时间让电子邮件与 Django 协同工作。
我收到此错误:[Errno 111] Connection refused
当我尝试发送电子邮件时
这是我创建电子邮件并尝试在我的视图中发送它的地方:
try:
msg = EmailMessage(subject, message, from_email, [receiver])
msg.content_subtype = "html"
msg.send()
我的设置文件如下:
EMAIL_HOST = "localhost"
DEFAULT_FROM_EMAIL = "myemail@gmail.com"
EMAIL_PORT = 25
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
我尝试过使用进行测试发送python -m smtpd -n -c DebuggingServer localhost:1025
并取得了成功,但当真正进行操作时,却没有成功。
当我尝试从 shell 执行 send_mail 时,我得到了这个回溯:
>>> from django.core.mail import send_mail
>>> send_mail('Test', 'Test', 'myemail@gmail.com', ['myemail@gmail.com'])
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.6/dist-packages/django/core/mail/__init__.py", line 61, in send_mail
connection=connection).send()
File "/usr/local/lib/python2.6/dist-packages/django/core/mail/message.py", line 251, in send
return self.get_connection(fail_silently).send_messages([self])
File "/usr/local/lib/python2.6/dist-packages/django/core/mail/backends/smtp.py", line 79, in send_messages
new_conn_created = self.open()
File "/usr/local/lib/python2.6/dist-packages/django/core/mail/backends/smtp.py", line 42, in open
local_hostname=DNS_NAME.get_fqdn())
File "/usr/lib/python2.6/smtplib.py", line 239, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python2.6/smtplib.py", line 295, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python2.6/smtplib.py", line 273, in _get_socket
return socket.create_connection((port, host), timeout)
File "/usr/lib/python2.6/socket.py", line 561, in create_connection
raise error, msg
error: [Errno 111] Connection refused
我似乎对此一无所获。任何帮助或建议都将不胜感激。谢谢
此外,如果您想看到其他内容,请发表评论。
解决方案 1:
您是否想使用 Gmail 帐户?那么可以尝试以下方法:
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'your-username@gmail.com'
EMAIL_HOST_PASSWORD = 'your-password'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
然后尝试测试(django <1.4)
python manage.py shell
>>> from django.core.mail import send_mail
>>> send_mail('test email', 'hello world', to=['test@email.com'])
如果你使用 django 1.4 请使用以下命令:
python manage.py shell
>>> from django.core.mail import send_mail
>>> send_mail('test email', 'hello world', 'your@email.com', ['test@email.com'])
如果您没有使用 gmail 帐户,但仍然遇到问题,那么只需尝试将EMAIL_HOST_USER
和添加EMAIL_HOST_PASSWORD
到您已有的帐户中。如果您仍然遇到问题,可能是您的网络阻止了您。操作系统或路由器上的防火墙。
感谢 knite 更新了语法。给他 +1,并感谢 pranavk 让我了解 django 1.4 中的语法变化
解决方案 2:
首先创建应用程序专用密码
访问您的Google 帐户安全页面。然后点击两步验证:
点击Google 帐户安全
App passwords
页面:
创建一个
App
,选择Mail
并命名:
记下
App Password
:
然后将适当的值添加到settings.py中:
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'your-username@gmail.com'
EMAIL_HOST_PASSWORD = 'Application spectific password(for eg: smbumqjiurmqrywn)'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
你可以使用 shell 来测试它:
python manage.py shell
>>> from django.core.mail import send_mail
>>> send_mail('Test', 'This is a test', 'your@email.com', ['toemail@email.com'],
fail_silently=False)
解决方案 3:
@mongoose_za 有一个很好的答案,但是在 Django 1.4+ 中语法有点不同。
而不是:
send_mail('test email', 'hello world', to=['test@email.com'])
使用
send_mail('test email', 'hello world', 'your@email.com', ['test@email.com'])
前四个参数是必需的:主题、消息、发件人电子邮件和收件人列表。
解决方案 4:
在 gmail 设置中启用 pop3。
为此 django 应用程序创建应用程序特定密码。(http://support.google.com/accounts/bin/answer.py?hl=en&answer=185833)
解决方案 5:
我会避免使用 GMail。它对一些电子邮件有用,但之后,您可能会发现所有电子邮件都被拒绝或被归为垃圾邮件。我使用亚马逊的“SES”服务与 Django-SES 解决了这个问题。
解决方案 6:
在 settings.py 中,使用smtp作为后端而不是控制台
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
解决方案 7:
将以下最低设置放入服务器上的 settings.py 或 local_settings.py 文件中。
EMAIL_HOST = 'localhost'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
您可以拥有自己的邮件服务器,而不必使用有很多限制的 smtp.gmail.com。
您可以通过安装自己的邮件服务器来实现:
sudo apt-get install sendmail
解决方案 8:
首先在 settings.py 文件中设置电子邮件后端变量。
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'your-username@gmail.com'
EMAIL_HOST_PASSWORD = 'your-password'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
然后,创建一个电子邮件对象来发送邮件。
from django.core import mail
connection = mail.get_connection() # Manually open the connection
connection.open() # Construct an email message that uses the connection
email = mail.EmailMessage( 'Hello', 'Body goes here', 'from@example.com', ['to1@example.com'], connection=connection, )
email.send() # Send the email
connection.close()
扫码咨询,免费领取项目管理大礼包!