21

hello i want to sending email activation use django registration redux.

this is my setting.py

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

ACCOUNT_ACTIVATION_DAYS = 3
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'blahblah@gmail.com'
EMAIL_HOST_PASSWORD = 'blahpassword'
EMAIL_PORT = 465
EMAIL_USE_SSL = True

LOGIN_REDIRECT_URL = '/'

when i try with

python manage.py shell

from django.core.mail import send_mail

send_mail('Test', 'This is a test', 'youremail@gmail.com', ['toemail@gmail.com'])

i am getting error like this:

    Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/apsijogja/djangogirls/myvenv/local/lib/python2.7/site-packages/django/core/mail/__init__.py", line 62, in send_mail
    return mail.send()
  File "/home/apsijogja/djangogirls/myvenv/local/lib/python2.7/site-packages/django/core/mail/message.py", line 286, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/home/apsijogja/djangogirls/myvenv/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 92, in send_messages
    new_conn_created = self.open()
  File "/home/apsijogja/djangogirls/myvenv/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 50, in open
    self.connection = connection_class(self.host, self.port, **connection_params)
  File "/usr/lib/python2.7/smtplib.py", line 249, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/lib/python2.7/smtplib.py", line 310, in connect
    (code, msg) = self.getreply()
  File "/usr/lib/python2.7/smtplib.py", line 361, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed")
SMTPServerDisconnected: Connection unexpectedly closed

can you help me solve this problem?

Community
  • 1
  • 1
User0511
  • 665
  • 3
  • 11
  • 27
  • 9
    That's definitely the wrong port...Also, change your Gmail password, and avoid posting actual credentials in public Stack Overflow posts... – rnevius Mar 11 '15 at 07:07
  • try changing email port to 587 – irqed Mar 11 '15 at 07:09
  • possible duplicate of ["\[Errno 101\] Network is unreachable" when trying to send email using Django](http://stackoverflow.com/questions/14949492/errno-101-network-is-unreachable-when-trying-to-send-email-using-django) – rnevius Mar 11 '15 at 07:14
  • i already change with port 587 but still error – User0511 Mar 11 '15 at 07:15
  • @rnevius in that link i am not understand about blue host machine. – User0511 Mar 11 '15 at 07:19
  • can you see i already update my question and show error like that. how? – User0511 Mar 11 '15 at 08:12
  • Possible duplicate of [How to send an email with Python?](http://stackoverflow.com/questions/6270782/how-to-send-an-email-with-python) – tripleee Feb 17 '16 at 08:21
  • For those who are using a correct port but still ended up with this error. Switching the email backend back to `console` (`EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'`), confirming that your email ends up in the console upon password reset, before switching back to `smtp` helped to resolve this. – Jakob Oct 09 '21 at 11:41

9 Answers9

24

To use port 465, you need to call smtplib.SMTP_SSL(). Currently, it calls smtplib.SMTP() .. so,change your PORT from 465 into 587 it

if you want use PORT 465,

your EMAIL_BACKEND = 'django_smtp_ssl.SSLEmailBackend'

EMAIL_PORT=465

and you need to install django_smtp_ssl

otherwise you can keep,

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

EMAIL_PORT=465
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
Mohideen bin Mohammed
  • 18,813
  • 10
  • 112
  • 118
  • 4
    Update: Django >=1.7 includes support for SMTP with SSL/TLS; hence django-smtp-ssl is obsolete. https://github.com/bancek/django-smtp-ssl – Mark Mar 05 '20 at 00:43
3

For python3.0+

I found a workaround or maybe this is the solution , there is some issue in the django email package.

1)pip install sendgrid-django

2)Add these in the settings.py along with other email configurations

  EMAIL_BACKEND = 'sgbackend.SendGridBackend'
  SENDGRID_API_KEY = "YOUR SENDGRID API KEY"
  EMAIL_PORT = 465

3)Code sample to send mail(yours maybe different):

from django.core.mail import EmailMessage
send_email = EmailMessage(
    subject=subject,
    body=body,
    from_email=from_email,
    to=to_email,
    reply_to=reply_to,
    headers=headers,

)
send_email.send(fail_silently=fail_silently)

You have to certainly make changes to the code as i was just showing a example.

For debugging purpose only-sendgrid mail send using telnet

MrSoloDolo
  • 406
  • 5
  • 10
2

I was developing and testing on localhost with the Gmail SMTP server and ran into this error. I figured out this was because I was using the EMAIL_PORT = 465 on a EMAIL_USE_TLS = True configuration.

EMAIL_PORT = 465 is an SSL port configuration and is supposed to be used alongside the EMAIL_USE_SSL = True configuration and not on a EMAIL_USE_TLS = True configuration.

So anybody that will run into this issue in the future with same mistake as me should change that and everything should work fine. Below is what the full SMTP configuration looked like for me:

# Email server configuration
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' 
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'your_gmail_account@gmail.com'
EMAIL_HOST_PASSWORD = 'xxxxxxxxxxxxxxxx'
EMAIL_PORT = 465
EMAIL_USE_SSL = True

Also note that using TLS with EMAIL_PORT = 587 and EMAIL_USE_TLS = True didn't work for me on Gmail, so I had to switch to using SSL and it worked.

1

Please look into this Link: https://code.djangoproject.com/ticket/9575 and try sending via shell, it should work

nyk91
  • 47
  • 3
1

In my case using smtplib.SMTP_SSL() solve this problem. You can try this.

Nurul Akter Towhid
  • 3,046
  • 2
  • 33
  • 35
1

Like myself, if your deployed django app is on heroku and using https meaning you need port 465, you should not forget to add these same values below here to your heroku Config Vars. It threw 'connection unexpectedly closed' error untill i had to add this.

# settings.py

EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey'
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_HOST_PASSWORD')                   
                     # replace here with the long SendGrid API secret code 
                     # xyxyxyxyzzzyz on heroku's Config Vars
EMAIL_PORT =  465 
EMAIL_USE_SSL = True

Without that, debugging on localhost or development sever will work (with port 587 under http) but not at deployment under port 465 with https

Laenka-Oss
  • 939
  • 2
  • 17
  • 25
1

I'm using AWS SES and was having the same problem.

Using port 587 worked for me. Settings were pretty much identical to @User0511

PowerAktar
  • 2,341
  • 1
  • 21
  • 17
1

In my case the problem was that I used my api for EMAIL_HOST_PASSWORD. Then I found my SMPT password by going to https://app.mailgun.com/app/sending/domains/ Clicked on my domain name Then Selected on SMPT

Roizy Kish
  • 33
  • 5
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 14 '22 at 02:34
0

Normally this is raised because wrong SENDGRID credentials here you have to use

EMAIL_HOST_USER = 'apikey'

Mohamed Beltagy
  • 593
  • 4
  • 8