无法将 Django 设置为与 smtp.gmail.com 一起使用

2022-01-17 00:00:00 python django smtp

问题描述

我一直试图让 django 与 gmail 的 smtp 服务器一起发送邮件,但我总是得到这个回溯.任何帮助将不胜感激.

--settings.py -----

EMAIL_HOST = 'smtp.gmail.com'EMAIL_HOST_USER = 'user@gmail.com'EMAIL_HOST_PASSWORD = '你的密码'电子邮件端口 = 587EMAIL_USE_TLS = 真

---- python shell -----

<块引用><块引用>

从 django.core.mail 导入 EmailMessage

email = EmailMessage('Mail Test', 'This is a test', to=['somemail@something.com'])

email.send()

Traceback(最近一次调用最后一次):文件<console>",第 1 行,在 <module>文件/home/fiodorovich/Envs/fdict/lib/python2.7/site-packages/django/core/mail/message.py",第 251 行,在发送中return self.get_connection(fail_silently).send_messages([self])文件/home/fiodorovich/Envs/fdict/lib/python2.7/site-packages/django/core/mail/backends/smtp.py",第 86 行,在 send_messages发送 = self._send(消息)_send 中的文件/home/fiodorovich/Envs/fdict/lib/python2.7/site-packages/django/core/mail/backends/smtp.py",第 104 行email_message.message().as_string())文件/usr/local/lib/python2.7/smtplib.py",第 701 行,在 sendmail 中raise SMTPSenderRefused(code, resp, from_addr)SMTPSenderRefused: (530, '5.7.0 必须先发出 STARTTLS 命令.z15sm10449686anl.15', 'webmaster@localhost')

进行 unni 建议的修改时出现新错误.shell 不会执行,我收到此错误消息

**EMAIL_HOST_USER = 'some.account@gmail.com'**^SyntaxError:无效的语法

解决方案

我最近设置了这个,settings.py 配置略有不同.

移动:

EMAIL_USE_TLS = True

到 EMAIL_HOST 上方

添加:

DEFAULT_FROM_EMAIL = 'user@gmail.com'SERVER_EMAIL = 'user@gmail.com'

I've been trying to get django to work with gmail's smtp server to send mails but I always get this traceback. Any help will be most appreciated.

----- settings.py -----

EMAIL_HOST = 'smtp.gmail.com'

EMAIL_HOST_USER = 'user@gmail.com'

EMAIL_HOST_PASSWORD = 'your-password'

EMAIL_PORT = 587

EMAIL_USE_TLS = True

---- python shell -----

from django.core.mail import EmailMessage

email = EmailMessage('Mail Test', 'This is a test', to=['somemail@something.com'])

email.send()

Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/fiodorovich/Envs/fdict/lib/python2.7/site-packages/django/core/mail/message.py", line 251, in send
return self.get_connection(fail_silently).send_messages([self])
File "/home/fiodorovich/Envs/fdict/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 86, in send_messages
sent = self._send(message)
File "/home/fiodorovich/Envs/fdict/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 104, in _send
email_message.message().as_string())
File "/usr/local/lib/python2.7/smtplib.py", line 701, in sendmail
raise SMTPSenderRefused(code, resp, from_addr)
SMTPSenderRefused: (530, '5.7.0 Must issue a STARTTLS command first. z15sm10449686anl.15', 'webmaster@localhost')

Edit: New errors when made the modification suggested by unni. The shell won't execute and I'm getting this error message

**EMAIL_HOST_USER  = 'some.account@gmail.com'**
 ^
SyntaxError: invalid syntax

解决方案

I have recently set this up and had a slightly different settings.py config.

Move:

EMAIL_USE_TLS = True 

to the top above EMAIL_HOST

Add:

DEFAULT_FROM_EMAIL = 'user@gmail.com'
SERVER_EMAIL = 'user@gmail.com'

相关文章