【问题标题】:I'm trying to send an email from Django using Gmail SMTP我正在尝试使用 Gmail SMTP 从 Django 发送电子邮件
【发布时间】:2021-06-24 17:56:19
【问题描述】:

我在我的帐户设置中设置了不安全来源。另外,这是我的 settings.py 文件。有人可以帮帮我吗?

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com '
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER =' my email'
EMAIL_HOST_PASSWORD = 'my password'

当我编写此代码时,我在终端中收到此错误

PS D:\start here\silicium7\config-project> python manage.py sendtestemail atabarzega79@gmail.com 
Traceback (most recent call last):
 File "manage.py", line 22, in <module>
   main()
 File "manage.py", line 18, in main
   execute_from_command_line(sys.argv)
 File "D:\start here\silicium7\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
   utility.execute()
 File "D:\start here\silicium7\lib\site-packages\django\core\management\__init__.py", line 395, in execute
   self.fetch_command(subcommand).run_from_argv(self.argv)
 File "D:\start here\silicium7\lib\site-packages\django\core\management\base.py", line 330, in run_from_argv
   self.execute(*args, **cmd_options)
 File "D:\start here\silicium7\lib\site-packages\django\core\management\base.py", line 371, in execute
   output = self.handle(*args, **options)
 File "D:\start here\silicium7\lib\site-packages\django\core\management\commands\sendtestemail.py", line 33, in handle
   recipient_list=kwargs['email'],
 File "D:\start here\silicium7\lib\site-packages\django\core\mail\__init__.py", line 61, in send_mail
   return mail.send()
 File "D:\start here\silicium7\lib\site-packages\django\core\mail\message.py", line 284, in send
   return self.get_connection(fail_silently).send_messages([self])
 File "D:\start here\silicium7\lib\site-packages\django\core\mail\backends\smtp.py", line 102, in send_messages
   new_conn_created = self.open()
 File "D:\start here\silicium7\lib\site-packages\django\core\mail\backends\smtp.py", line 62, in open
   self.connection = self.connection_class(self.host, self.port, **connection_params)
 File "C:\Users\Ata Barzegar\AppData\Local\Programs\Python\Python37\lib\smtplib.py", line 251, in __init__
   (code, msg) = self.connect(host, port)
 File "C:\Users\Ata Barzegar\AppData\Local\Programs\Python\Python37\lib\smtplib.py", line 336, in connect
   self.sock = self._get_socket(host, port, self.timeout)
 File "C:\Users\Ata Barzegar\AppData\Local\Programs\Python\Python37\lib\smtplib.py", line 307, in _get_socket
   self.source_address)
   for res in getaddrinfo(host, port, 0, SOCK_STREAM):
 File "C:\Users\Ata Barzegar\AppData\Local\Programs\Python\Python37\lib\socket.py", line 752, in getaddrinfo
   for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

【问题讨论】:

    标签: python django email smtp


    【解决方案1】:

    这可能对你有用。

    settings.py中添加DEFAULT_FROM_EMAIL,并通过python shell进行测试:

    EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
    EMAIL_HOST = 'smtp.gmail.com '
    EMAIL_USE_TLS = True
    EMAIL_PORT = 587
    EMAIL_HOST_USER ='realmail@gmail.com'
    EMAIL_HOST_PASSWORD = '**********'
    
    DEFAULT_FROM_EMAIL = 'testmail@gmail.com>'
    

    运行 python shell

    >>> python manage.py shell
    >>> from django.conf import settings
    >>> from django.core.mail import send_mail
    >>>
    >>> subject = 'Some subject'
    >>> from_email = settings.DEFAULT_FROM_EMAIL   
    >>> message = 'This is my test message'
    >>> recipient_list = ['mytestmail@gmail.com','test2@gmail.com']           
    >>> html_message = '<h1>This is my HTML test</h1>'
    >>> send_mail(subject, message, from_email, recipient_list, fail_silently=False, html_message=html_message)
    

    如果输出代码为1,则电子邮件发送成功....

    【讨论】:

      猜你喜欢
      • 2014-08-15
      • 1970-01-01
      • 2015-10-22
      • 2023-03-25
      • 1970-01-01
      • 2014-12-24
      • 2016-10-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多