I've written this code and I cannot figure out why the mail is not sent. In this code I check the POST field of the class request and I get the IP address inserted in a form. Once gotten the IP address I make a call to the server indicated by the IP and then I would like to send an email with the text of the response.
views.py
from django.shortcuts import render, redirect
from .models import Server
from django.http import HttpResponseRedirect
from .forms import ServerForm
import requests
from time import sleep
from django.core.mail import send_mail
def startPing(request):
servers = Server.objects.all()
context = {"servers": servers}
if request.method == 'POST':
indirizzoIP = request.POST.get('indirizzoIP')
timer = request.POST.get('timer')
indirizzoIP = "http://" + indirizzoIP
while True:
response = requests.get(indirizzoIP)
if response != "":
response = "Server OK"
send_mail(
'Report server',
'{}'.format(response),
'from@',
['to@'],
fail_silently=False,
)
print(response)
sleep(float(timer)*3600)
return render(request, 'homepage.html', context)
EDIT
When I compile the form with the server IP, in the console I don't see any error. I tried to remove the sendmail function and in the console I see this:
System check identified no issues (0 silenced).
September 23, 2022 - 09:28:15
Django version 3.2.6, using settings 'serverControl.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[23/Sep/2022 09:28:18] "GET / HTTP/1.1" 200 6354
Server OK
Server OK