2

i am trying to execute the following code on terminal with django.core.mail send_mail

send_mail('some title','some text','myemail@gmail.com',['otheremail@gmail.com'])

but after execution the console is showing this error

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/adjtlikp/virtualenv/globalit-web/3.7/lib/python3.7/site-packages/django/core/mail/__init__.py", line 61, in send_mail
    return mail.send()
  File "/home/adjtlikp/virtualenv/globalit-web/3.7/lib/python3.7/site-packages/django/core/mail/message.py", line 284, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/home/adjtlikp/virtualenv/globalit-web/3.7/lib/python3.7/site-packages/django/core/mail/backends/smtp.py", line 102, in send_messages
    new_conn_created = self.open()
  File "/home/adjtlikp/virtualenv/globalit-web/3.7/lib/python3.7/site-packages/django/core/mail/backends/smtp.py", line 62, in open
    self.connection = self.connection_class(self.host, self.port, **connection_params)
  File "/opt/alt/python37/lib64/python3.7/smtplib.py", line 251, in __init__
    (code, msg) = self.connect(host, port)
  File "/opt/alt/python37/lib64/python3.7/smtplib.py", line 338, in connect
    (code, msg) = self.getreply()
  File "/opt/alt/python37/lib64/python3.7/smtplib.py", line 394, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed

my settings.py

EMAIL_USE_TLS = True  
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'   
EMAIL_HOST = 'smtp.gmail.com'  
EMAIL_HOST_PASSWORD = 'mypass'  
EMAIL_HOST_USER = 'myemail@gmail.com'   
EMAIL_PORT = 465 
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER

i tryed to change the EMAIL_PORT and the EMAIL_HOST but nothing changed the intersting is that in localhost the code work perfect but when i am using the host the errors came to me

Mher
  • 21
  • 1
  • 7
  • If it works on your dev env and not on prod then its likely that on the latter environment somethings is being overridden. How are you deploying it and what hosts are you using it to host it? On certain hosts like Heroku you need to explicitly define your `port` values accordingly in the `config Vars` see the answer here -https://stackoverflow.com/a/61112070/6505847. – AzyCrw4282 Sep 04 '20 at 03:31
  • You can also try setting `use_tls: EMAIL_USE_TLS use_ssl: EMAIL_USE_SSL` and see if it solves. See from docs here https://docs.djangoproject.com/en/3.1/topics/email/#smtp-backend – AzyCrw4282 Sep 04 '20 at 03:35
  • Thank you very much the error showed on because i entered the wrong port for EMAIL_USE_TLS in cause of TLS the port should be 25 or 587 in cause of SSL the port should be 465 thanks for the support – Mher Sep 04 '20 at 08:20

2 Answers2

0

The error showed on because, I entered the wrong port for EMAIL_USE_TLS in case of TLS the port should be 25 or 587 and in case of SSL the port should be 465.

Akshay
  • 1,019
  • 12
  • 21
Mher
  • 21
  • 1
  • 7
0

I had the same case. My settings were:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'apikey'
EMAIL_HOST_PASSWORD = config('PASS')

I also had a verified email on SendGrid. The reason I was getting this error was because I was not using the email I had verified on SendGrid. If you're using another email address to send an email from your website you might also get the following error:

(550, b'The from address does not match a verified Sender Identity. Mail cannot be sent until this error is resolved. Visit https://sendgrid.com/docs/for-developers/sending-email/sender-identity/ to see the Sender Identity requirements')

So in short, use only one existing email which you have verified on SendGrid then if the code works, check the inbox/spam of the email you used.