Django Sending Email : SMTPServerDisconnected: Connection unexpectedly closed

后端 未结 6 1720

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

this is my setting.py

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


        
相关标签:
6条回答
  • 2021-02-13 02:48

    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
    
    0 讨论(0)
  • 2021-02-13 02:55

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

    0 讨论(0)
  • 2021-02-13 02:59

    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

    0 讨论(0)
  • 2021-02-13 03:01

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

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

    0 讨论(0)
  • 2021-02-13 03:09

    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

    0 讨论(0)
  • 2021-02-13 03:11

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

    0 讨论(0)
提交回复
热议问题