Django - [Errno 111] Connection refused

前端 未结 8 1511
感情败类
感情败类 2020-11-29 21:29

when I post a comment, do not save, crashes (error: [Errno 111] Connection refused), why?

views.py

import time
from calendar import month_name

from          


        
8条回答
  •  北荒
    北荒 (楼主)
    2020-11-29 22:05

    I also run into this error. Instead of using gmail, I decided to setup my own mailserver using postfix. See my reasons here.

    To setup postfix on Ubuntu 12.04:

    sudo apt-get install postfix
    

    Then, copy the config file to /etc/postfix/:

    cp /usr/share/postfix/main.cf.debian /etc/postfix/main.cf
    

    Add the following lines to main.cf:

    mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
    mydestination = localhost
    

    Reload the config file:

    /etc/init.d/postfix reload
    

    To test and see if postfix is working:

    telnet localhost 25
    

    Then enter the following line by line:

    mail from: whatever@whatever.com
    rcpt to: your_real_email_addr@blah.com
    data (press enter)
    type whatever content you feel like to type
    . (put an extra period on the last line and then press enter again)
    

    If it works, you should see something like this:

    250 2.0.0 Ok: queued as CC732427AE
    

    Next, put the following line in your Django's settings.py:

    EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
    EMAIL_HOST = 'localhost'
    EMAIL_PORT = 25
    EMAIL_HOST_USER = ''
    EMAIL_HOST_PASSWORD = ''
    EMAIL_USE_TLS = False
    DEFAULT_FROM_EMAIL = 'Server '
    

    To test if Django can send email via postfix, open Django shell:

    ./manage.py shell
    
    >>> from django.core.mail import send_mail
    >>> send_mail('Subject here', 'Here is the message.', 'from@example.com',
        ['to@example.com'], fail_silently=False)
    

    Check your spam inbox and you should see the email above shown.

提交回复
热议问题