![](/img/trans.png)
[英]to send email with different 'from' email address using gmail smtp server in django
[英]I'm trying to send an email from Django using Gmail SMTP
我在我的帳戶設置中設置了不安全來源。 另外,這是我的 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
這可能對你有用。
在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)
如果 output 代碼為1
則 email 發送成功....
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.