Django: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123)

89
April 06, 2022, at 03:10 AM

I'm trying to email using Python's smtplib. This is the email() function:

def email(to, subject_parameter, body_parameter):
    subject = subject_parameter
    body = body_parameter
    sender_email = "pfcbookclub@gmail.com"
    receiver_email = to
    password = "Zt2.~[3d*.[Y5&5r"
    # Create a multipart message and set headers
    message = MIMEMultipart()
    message["From"] = sender_email
    message["To"] = receiver_email
    message["Subject"] = subject
    message["Bcc"] = "pfcbookclub@gmail.com"
    # Add body to email
    message.attach(MIMEText(body, "plain"))
    text = message.as_string()
    # Log in to server using secure context and send email
    context = ssl.create_default_context()
    with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
        server.login(sender_email, password)
        server.sendmail(sender_email, receiver_email, text)

When someone signs up on my website, I want an email to be sent to the website's email address (not to the user).

I've done this:

def register(request):
    registered = False
    ctx = {}
    if request.method == 'POST':
        username = request.POST.get("username")
        full_name = request.POST.get("full_name")
        password = request.POST.get("password")
        email_id = request.POST.get("email")
        house_number = request.POST.get("house_number")
        phone_number = request.POST.get("phone_number")
        if len(User.objects.filter(username=username)) == 0:
            ctx["username_exists"] = False
            user_form = UserForm(data=request.POST)
            if user_form.is_valid():
                user = user_form.save()
                user.set_password(user.password)
                user.save()
                user_profile = UserProfileInfo(user=user,full_name=full_name, email=email_id, house_number=house_number, phone_number=phone_number)
                user_profile.save()
                subject = username + " just registered"
                body = username + " just registered on pfcbookclub.com. Link to admin: https://pfcbookclub.com/Caput_Draconis/"
                email("pfcbookclub@gmail.com", subject, body) # Error on this line
                username = request.POST.get('username')
                password = request.POST.get('password')
                user = authenticate(username=username, password=password)
                if user:
                    if user.is_active:
                        login(request,user)
                        return HttpResponseRedirect(reverse('index'))
                    else:
                        return HttpResponse("Your account was inactive.")
                else:
                    print("Someone tried to login and failed.")
                    print("They used username: {} and password: {}".format(username,password))
                    return HttpResponse("Invalid login details given")
            else:
                return HttpResponse("Contains @")
        else:
            ctx["username_exists"] = True
            return render(request,'main/registration.html', ctx)
    elif request.method == "GET":
        return render(request,'main/registration.html', ctx)

But when I register, I get the error ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123).

Full traceback (stack trace):

/Users/adithraghav/Documents/Work/centel/books/venv/lib/python3.9/site-packages/django/db/models/fields/__init__.py:1351: RuntimeWarning: DateTimeField UserRents.return_date received a naive datetime (2022-02-21 00:00:00) while time zone support is active.
  warnings.warn("DateTimeField %s.%s received a naive datetime "
Internal Server Error: /main/rent/alPuEivQ3P/
Traceback (most recent call last):
  File "/Users/adithraghav/Documents/Work/centel/books/venv/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/Users/adithraghav/Documents/Work/centel/books/venv/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/adithraghav/Documents/Work/centel/books/venv/lib/python3.9/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/Users/adithraghav/Documents/Work/centel/books/main/views.py", line 240, in rent
    email(book.owner.email, subject, body)
  File "/Users/adithraghav/Documents/Work/centel/books/main/views.py", line 85, in email
    with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 1034, in __init__
    SMTP.__init__(self, host, port, local_hostname, timeout,
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 253, in __init__
    (code, msg) = self.connect(host, port)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 339, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 1041, in _get_socket
    new_socket = self.context.wrap_socket(new_socket,
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py", line 500, in wrap_socket
    return self.sslsocket_class._create(
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py", line 1040, in _create
    self.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py", line 1309, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123)

This error occurs not only in the register() route but in all routes where I send an email.

READ ALSO
Average using loops

Average using loops

Anyone know how to rearrange this code so that when the values that are inputted are 3 4 8 4 , the average is 475

53
I think it's a basic question but I don't get why I get 0 in this function

I think it's a basic question but I don't get why I get 0 in this function

I'm wondering why I get 0 for the print(a) call in the last lineI put 0 for the first loop of the add()function and it should return 1 and so on in the for loop

74
Python variable won't update outside of module

Python variable won't update outside of module

I have a variable called store_number that I pass in from my mainpy into a module called store_selector which increments the store number until it finds a valid store

73
I can't install anything with pip [duplicate]

I can't install anything with pip [duplicate]

i tried installing numPy using this command

29