Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Sending Email : SMTPServerDisconnected: Connection unexpectedly closed

Tags:

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 = '[email protected]'
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', '[email protected]', ['[email protected]'])

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?

like image 207
User0511 Avatar asked Mar 11 '15 07:03

User0511


2 Answers

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
like image 103
Mohideen bin Mohammed Avatar answered Oct 05 '22 06:10

Mohideen bin Mohammed


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

like image 27
MrSoloDolo Avatar answered Oct 05 '22 07:10

MrSoloDolo