TimeoutError - Traceback(最后一次调用),Django-app - 发送电子邮件。 Python

TimeoutError - Traceback (most recent call last), Django-app - sending e-mails. Python

我正在尝试使用 'mailgun' 和 gmail 邮件从我的 Django 应用程序发送电子邮件,但每次我都收到错误。

在我的应用程序中,我有以下代码:

settings.py

EMAIL_HOST = 'smtp.mailgun.org'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'aaa@mg.xxx.com'
EMAIL_HOST_PASSWORD = '#########'
EMAIL_USE_TLS = True

然后我运行命令行中的命令:

manage.py shell

[1] from django.core.mail import send_mail
[2] send_mail('subject', 'body of the message', 'aaa@mg.xxx.com', ['
recipient@aaa.com'])

我是用 gmail 还是用 'mailguna' 总是得到同样的错误

错误:

TimeoutError                              Traceback (most recent call last)
<ipython-input-27-4c559962ca7f> in <module>()
----> 1 send_mail('subject', 'body of the message', '###@mg.###.com', ['###@###.com'])

~\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\mail\__init__.py in send_mail(subject, message, from_email, recipient_list, fail_silently, auth_user, auth_password, connection, html_message)
     58         mail.attach_alternative(html_message, 'text/html')
     59
---> 60     return mail.send()
     61
     62

~\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\mail\message.py in send(self, fail_silently)
    289             # send to.
    290             return 0
--> 291         return self.get_connection(fail_silently).send_messages([self])
    292
    293     def attach(self, filename=None, content=None, mimetype=None):

~\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\mail\backends\smtp.py in send_messages(self, email_messages)
    101             return
    102         with self._lock:
--> 103             new_conn_created = self.open()
    104             if not self.connection or new_conn_created is None:
    105                 # We failed silently on open().

~\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\mail\backends\smtp.py in open(self)
     61             })
     62         try:
---> 63             self.connection = self.connection_class(self.host, self.port, **connection_params)
     64
     65             # TLS/SSL are mutually exclusive, so only attempt TLS over

~\AppData\Local\Programs\Python\Python37-32\lib\smtplib.py in __init__(self, host, port, local_hostname, timeout, source_address)
    249
    250         if host:
--> 251             (code, msg) = self.connect(host, port)
    252             if code != 220:
    253                 self.close()

~\AppData\Local\Programs\Python\Python37-32\lib\smtplib.py in connect(self, host, port, source_address)
    334         if self.debuglevel > 0:
    335             self._print_debug('connect:', (host, port))
--> 336         self.sock = self._get_socket(host, port, self.timeout)
    337         self.file = None
    338         (code, msg) = self.getreply()

~\AppData\Local\Programs\Python\Python37-32\lib\smtplib.py in _get_socket(self, host, port, timeout)
    305             self._print_debug('connect: to', (host, port), self.source_address)
    306         return socket.create_connection((host, port), timeout,
--> 307                                         self.source_address)
    308
    309     def connect(self, host='localhost', port=0, source_address=None):

~\AppData\Local\Programs\Python\Python37-32\lib\socket.py in create_connection(address, timeout, source_address)
    725
    726     if err is not None:
--> 727         raise err
    728     else:
    729         raise error("getaddrinfo returns an empty list")

~\AppData\Local\Programs\Python\Python37-32\lib\socket.py in create_connection(address, timeout, source_address)
    714             if source_address:
    715                 sock.bind(source_address)
--> 716             sock.connect(sa)
    717             # Break explicitly a reference cycle
    718             err = None

TimeoutError: [WinError 10060]

我尝试更改设置 'TLS'、'mail port' 和 'SSL',但它们总是以相同的错误结束。

当代码 运行 在应用程序外部时一切正常:

file_works_properly.py

import smtplib

from email.mime.text import MIMEText

msg = MIMEText('Title')  #Tresc wiadomosci
msg['Subject'] = "Hello world"
msg['From']    = "XXX@mg.bbb.com"
msg['To']      = "ccc@ccc.com"

s = smtplib.SMTP('smtp.mailgun.org', 587)

s.login('###@mg.bbb.com', '3###baa5e1f3###-26fa###0987')
s.sendmail(msg['From'], msg['To'], msg.as_string())
s.quit()

我们将不胜感激。

添加三个导入解决了我的问题(对于 'python manage.py shell' 或 views.py 文件):

进口(在浏览量中。py/shell):

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

其余活动不变:

settings.py

[...]
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'myemail@gmail.com'
EMAIL_HOST_PASSWORD = 'mypassowrd'
EMAIL_PORT = 587

发送电子邮件(在浏览量中。py/shell)

send_mail(subject, message, from_email, [to_list_email])

(*对于 gmail.com 很重要) - 通常必须启用此 option