Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
62 views
in Technique[技术] by (71.8m points)

python - How to configure Django Email on Production Server using Sudo

I have recently deployed a site that I developed in Django onto a production server running Ubuntu and Apache.

The site functioning in every respect with the exception of sending emails wherever I configure an email to be sent, the page keeps loading and nothing happens although in the back end whatever information was made is saved like the contact us.

On the local host emails are working perfectly fine and on the deployed site it is just not sending and the page keeps loading endlessly.

Here is an example of the contact us config views.py

def contact_us(request):
    if request.method == 'POST':  # check post
        form = ContactForm(request.POST)
        if form.is_valid():
            data = ContactMessage()  # create relation with model
            data.name = form.cleaned_data['name']  # get form input data
            data.email = form.cleaned_data['email']
            data.subject = form.cleaned_data['subject']
            data.message = form.cleaned_data['message']
            data.save()  # save data to table
            messages.success(request, "Your message has ben sent. Thank you for your message.")
            template = render_to_string("marketing/contact_us_email.html", {'first_name': request.user.first_name,
                                                                  'last_name': request.user.last_name, 'form': form})

            msg = EmailMessage('Contact Us Message', template,
                               settings.EMAIL_HOST_USER, ['[email protected]'])
            msg.content_subtype = "html"  # Main content is now text/html
            msg.fail_silently = False
            msg.send()
            return HttpResponseRedirect('/')

    form = ContactForm
    context = {'form': form}
    template = 'marketing/contact_us.html'
    return render(request, template, context)

Here is the email config in deployment settings.py

import json
from pathlib import Path

with open('/etc/config.json') as config_file:
    config= json.load(config_file)

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = config.get('EMAIL_USER')
EMAIL_HOST_PASSWORD = config.get('EMAIL_PASSWORD')

in my json file

{
        "SECRET_KEY":".............................",
        "EMAIL_USER":"............................",
        "EMAIL_PASSWORD":"...................."
}

I have tried to check ufw status

Status: active

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW       Anywhere
80/tcp                     ALLOW       Anywhere
22/tcp (v6)                ALLOW       Anywhere (v6)
80/tcp (v6)                ALLOW       Anywhere (v6)

I am new to deploying projects, I am not sure how to debug regarding this particular issue.

I have tried the following command:

(venv) user@django-server:~/Project$ /var/log/apache2/access.log
-bash: /var/log/apache2/access.log: Permission denied

Here is the logs when I used:sudo tail /var/log/syslog

Jan 24 20:42:48 django-server kernel: [ 6559.610360] [UFW BLOCK] IN=eth0 OUT= MAC=f2:3c:92:6a:a2:2e:5a:b0:f6:f2:ad:52:08:00 SRC=180.76.76.76 DST=172.105.20.139 LEN=84 TOS=0x00 PREC=0x00 TTL=43 ID=11161 PROTO=ICMP TYPE=0 CODE=0 ID=10784 SEQ=0
Jan 24 20:42:57 django-server kernel: [ 6568.510344] [UFW BLOCK] IN=eth0 OUT= MAC=f2:3c:92:6a:a2:2e:5a:b0:f6:f2:ad:52:08:00 SRC=194.147.140.103 DST=172.105.20.139 LEN=40 TOS=0x00 PREC=0x00 TTL=245 ID=36909 PROTO=TCP SPT=48813 DPT=4308 WINDOW=1024 RES=0x00 SYN URGP=0
Jan 24 20:43:05 django-server kernel: [ 6576.489136] [UFW BLOCK] IN=eth0 OUT= MAC=f2:3c:92:6a:a2:2e:5a:b0:f6:f2:ad:52:08:00 SRC=223.5.5.5 DST=172.105.20.139 LEN=84 TOS=0x00 PREC=0x00 TTL=116 ID=11169 PROTO=ICMP TYPE=0 CODE=0 ID=10784 SEQ=0
Jan 24 20:43:16 django-server kernel: [ 6588.096503] [UFW BLOCK] IN=eth0 OUT= MAC=f2:3c:92:6a:a2:2e:5a:b0:f6:f2:ad:52:08:00 SRC=175.176.40.44 DST=172.105.20.139 LEN=60 TOS=0x08 PREC=0x00 TTL=41 ID=5992 DF PROTO=TCP SPT=58866 DPT=554 WINDOW=65535 RES=0x00 SYN URGP=0
Jan 24 20:43:22 django-server kernel: [ 6594.383419] [UFW BLOCK] IN=eth0 OUT= MAC=f2:3c:92:6a:a2:2e:5a:b0:f6:f2:ad:52:08:00 SRC=142.250.78.106 DST=172.105.20.139 LEN=60 TOS=0x00 PREC=0x80 TTL=50 ID=14200 PROTO=TCP SPT=443 DPT=53584 WINDOW=65535 RES=0x00 ACK SYN URGP=0
Jan 24 20:43:37 django-server kernel: [ 6609.134643] [UFW BLOCK] IN=eth0 OUT= MAC=f2:3c:92:6a:a2:2e:5a:b0:f6:f2:ad:52:08:00 SRC=129.6.15.29 DST=172.105.20.139 LEN=76 TOS=0x00 PREC=0x20 TTL=52 ID=33054 PROTO=UDP SPT=123 DPT=123 LEN=56
Jan 24 20:43:42 django-server kernel: [ 6614.123887] [UFW BLOCK] IN=eth0 OUT= MAC=f2:3c:92:6a:a2:2e:5a:b0:f6:f2:ad:52:08:00 SRC=142.250.78.106 DST=172.105.20.139 LEN=60 TOS=0x00 PREC=0x80 TTL=50 ID=65111 PROTO=TCP SPT=443 DPT=53582 WINDOW=65535 RES=0x00 ACK SYN URGP=0
Jan 24 20:43:56 django-server kernel: [ 6627.945386] [UFW BLOCK] IN=eth0 OUT= MAC=f2:3c:92:6a:a2:2e:5a:b0:f6:f2:ad:52:08:00 SRC=45.155.205.32 DST=172.105.20.139 LEN=40 TOS=0x00 PREC=0x20 TTL=244 ID=1247 PROTO=TCP SPT=51101 DPT=7993 WINDOW=1024 RES=0x00 SYN URGP=0
Jan 24 20:44:02 django-server kernel: [ 6633.768443] [UFW BLOCK] IN=eth0 OUT= MAC=f2:3c:92:6a:a2:2e:5a:b0:f6:f2:ad:52:08:00 SRC=142.250.78.106 DST=172.105.20.139 LEN=60 TOS=0x00 PREC=0x80 TTL=50 ID=11134 PROTO=TCP SPT=443 DPT=53579 WINDOW=65535 RES=0x00 ACK SYN URGP=0
Jan 24 20:44:16 django-server kernel: [ 6647.454385] [UFW BLOCK] IN=eth0 OUT= MAC=f2:3c:92:6a:a2:2e:5a:b0:f6:f2:ad:52:08:00 SRC=154.73.167.39 DST=172.105.20.139 LEN=62 TOS=0x00 PREC=0x00 TTL=51 ID=62076 DF PROTO=UDP SPT=58743 DPT=554 LEN=42
(venv) ahesham@django-server:~/Project$ sudo tail /var/log/syslog
Jan 24 20:44:57 django-server kernel: [ 6688.601259] [UFW BLOCK] IN=eth0 OUT= MAC=f2:3c:92:6a:a2:2e:5a:b0:f6:f2:ad:52:08:00 SRC=188.210.155.24 DST=172.105.20.139 LEN=60 TOS=0x00 PREC=0x00 TTL=244 ID=29174 DF PROTO=TCP SPT=17692 DPT=554 WINDOW=65535 RES=0x00 SYN URGP=0
Jan 24 20:45:01 django-server CRON[3699]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
Jan 24 20:45:06 django-server kernel: [ 6698.191321] [UFW BLOCK] IN=eth0 OUT= MAC=f2:3c:92:6a:a2:2e:5a:b0:f6:f2:ad:52:08:00 SRC=5.52.105.55 DST=172.105.20.139 LEN=40 TOS=0x00 PREC=0x00 TTL=231 ID=3396 DF PROTO=TCP SPT=53854 DPT=554 WINDOW=0 RES=0x00 ACK RST URGP=0
Jan 24 20:45:17 django-server kernel: [ 6709.135708] [UFW BLOCK] IN=eth0 OUT= MAC=f2:3c:92:6a:a2:2e:5a:b0:f6:f2:ad:52:08:00 SRC=129.6.15.28 DST=172.105.20.139 LEN=76 TOS=0x00 PREC=0x20 TTL=52 ID=46172 PROTO=UDP SPT=123 DPT=123 LEN=56
Jan 24 20:45:36 django-server kernel: [ 6727.528369] [UFW BLOCK] IN=eth0 OUT= MAC=f2:3c:92:6a:a2:2e:5a:b0:f6:f2:ad:52:08:00 SRC=176.54.129.234 DST=172.105.20.139 LEN=60 TOS=0x08 PREC=0x40 TTL=128 ID=11651 DF PROTO=TCP SPT=25663 DPT=554 WINDOW=65535 RES=0x00 SYN URGP=0
Jan 24 20:45:57 django-server kernel: [ 6748.751374] [UFW BLOCK] IN=eth0 OUT= MAC=f2:3c:92:6a:a2:2e:5a:b0:f6:f2:ad:52:08:00 SRC=51.39.168.251 DST=172.105.20.139 LEN=1228 TOS=0x00 PREC=0x00 TTL=44 ID=34033 DF PROTO=UDP SPT=2775 DPT=554 LEN=1208
Jan 24 20:46:01 django-server kernel: [ 6752.489383] [UFW BLOCK] IN=eth0 OUT= MAC=f2:3c:92:6a:a2:2e:5a:b0:f6:f2:ad:52:08:00 SRC=223.5.5.5 DST=172.105.20.139 LEN=84 TOS=0x00 PREC=0x00 TTL=116 ID=11291 PROTO=ICMP TYPE=0 CODE=0 ID=10784 SEQ=0
Jan 24 20:46:04 django-server kernel: [ 6755.471870] [UFW BLOCK] IN=eth0 OUT= MAC=f2:3c:92:6a:a2:2e:5a:b0:f6:f2:ad:52:08:00 SRC=119.29.29.29 DST=172.105.20.139 LEN=84 TOS=0x08 PREC=0x00 TTL=50 ID=52379 PROTO=ICMP TYPE=0 CODE=0 ID=10784 SEQ=0
Jan 24 20:46:07 django-server kernel: [ 6758.426753] [UFW BLOCK] IN=eth0 OUT= MAC=f2:3c:92:6a:a2:2e:5a:b0:f6:f2:ad:52:08:00 SRC=114.114.114.114 DST=172.105.20.139 LEN=84 TOS=0x08 PREC=0x20 TTL=80 ID=60475 PROTO=ICMP TYPE=0 CODE=0 ID=10784 SEQ=0
Jan 24 20:46:17 django-server kernel: [ 6769.135354] [UFW BLOCK] IN=eth0 OUT= MAC=f2:3c:92:6a:a2:2e:5a:b0:f6:f2:ad:52:08:00 SRC=129.6.15.28 DST=172.105.20.139 LEN=76 TOS=0x00 PREC=0x20 TTL=52 ID=47969 PROTO=UDP SPT=123 DPT=123 LEN=56

My question what is the reason for this error and how to fix it?

question from:https://stackoverflow.com/questions/65875004/how-to-configure-django-email-on-production-server-using-sudo

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To check connectivity between your server and gmail you can simply use telnet:

telnet smtp.gmail.com 587

if you see something like this

Trying 173.194.76.108...
Connected to smtp.gmail.com.
Escape character is '^]'.
220 smtp.gmail.com ESMTP a184sm19715075wme.35 - gsmtp

the connection is fine and the problem lies elsewhere (probably in your code). If you get a "connection refused" or "connection timeout", you have a network problem: you can't reach gmail on port 587 probably due to a firewall between you and gmail. You have to enable outgoing traffic from the server to TCP port 587 (you or your network admin have to do it). I don't know ufw but you can try disabling it to find out if it's causing you problem: if it solves the issue, as i wrote before, you have to configure your firewall to allow outgoing traffic to port 587 TCP.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...