2024-11-12-Tuesday


created: 2024-11-12 06:16 tags: - daily-notes


Tuesday, November 12, 2024

<< Timestamps/2024/11-November/2024-11-11-Monday|Yesterday | Timestamps/2024/11-November/2024-11-13-Wednesday|Tomorrow >>


🎯 Goal


🌟 Results

  • Set up the Sendgrid API and registered emails on the platform

🌱 Next Time


📝 Notes

I already have the template and everything ready to go for password reset functionality, specifically I have the password_reset_email.html HTML file all written out.

I do have the password change component of the application live in production. Specifically when it comes to User authentication I have

I can find the Django Routes / urls for the accounts app in /accounts/urls.py. I can also see that this is where the Django Routes are associated with the Django Templates (i.e the domain.com/accounts/registration/login).

When I try to access the /accounts/reset-password-request/ route, I get a 500 Response Error saying that there is no password_reset.html Django Template, which is true. However I'm a little confused as to how exactly I'm loading my other URLs.

It looks like my Django Views define a PasswordResetView that uses the password_reset.html template which doesn't exist...? Not sure how it finds it though, because the page that it's bringing up is the correct one (password_reset_form.html) but I'm not sure how exactly it keeps finding that one. In the logs I can see that it gives me a 400 Error claiming that the password_reset.html is not found, which is weird because I can still access that page...

I changed the views.py file to explicitly use the registration/password_reset_form.html Django Template, which fixed the 400 Error.

Then I proceeded to the password_reset route, entered an email, and was redirected to /accounts/password_reset/done/ with a message to check my email for a link to reset my password.

I saw the email was sent to the console, and it included a link with a verification component (http://127.0.0.1:8000/accounts/reset/MQ/cgdah6-005c3f5f433be3679dd6206a0fa3339b/) that successfully brought me to the password reset form of /accounts/reset/MQ/set-password/.

I filled in my new password and completed the password reset procedure at /accounts/reset/done/

And it did, in fact, change my account password. So it looks like the password reset functionality is implemented, I just need to figure out how to actually send that email.

It looks like emails could be displayed in the console because I added the

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' line.

Knowing that my password reset functionality works, I want to hook this up to one of my email accounts. Ideally I'll want to use my protonmail account, but I'll test it out with my gmail account. It looks like gmail already uses an SMTP relay. I tried configuring my gmail via the following in my config/settings.py file:

### LEFT OFF HERE 06/21/2022 ###
# https://medium.com/reactorlabs/sending-emails-with-sendgrid-django-and-aws-route-53-39117dc4cfa2
# SendGrid
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = os.environ.get('GMAIL_USERNAME')
EMAIL_HOST_PASSWORD = os.environ.get('GMAIL_PASSWORD')

But I got the following response:

# SMTPAuthenticationError at /accounts/password_reset/

(534, b'5.7.9 Application-specific password required. For more information, go to\n5.7.9  https://support.google.com/mail/?p=InvalidSecondFactor d2e1a72fcca58-72407a571besm11229977b3a.194 - gsmtp')

However, I think that I can see where my original Sendgrid configuration fell apart. I tried using the API directly via

import os
import requests

url = "https://api.sendgrid.com/v3/mail/send"
headers = {
    "Authorization": f"Bearer {os.environ.get('SENDGRID_API_KEY')}",
    "Content-Type": "application/json",
}
data = {
    "personalizations": [
        {
            "to": [{"email": "dimmin314@protonmail.com"}],
            "subject": "Test Email from SendGrid"
        }
    ],
    "from": {"email": "dimmin314@protonmail.com"},
    "content": [
        {
            "type": "text/plain",
            "value": "This is a test email sent from the SendGrid API."
        }
    ]
}

response = requests.post(url, headers=headers, json=data)
print(response.status_code, response.text)

This made a successful request, however I got a 400 Error that indicated that it was an unauthorized attempt:

401 {"errors":[{"message":"The provided authorization grant is invalid, expired, or revoked","field":null,"help":null}]}

When I entered the API key in manually, I was able to get a 200 Response Code indicating a successful use of the API, but still no email.

Then, I was able to find it (both the manually sent email and the password reset email) in my Email Activity section of the Sendgrid dashboard, but it is marked as "Processing".

Overall this is a good sign, I've got Sendgrid working / sending emails. I'll need to make sure I can actually receive emails before I complete this issue.


Notes created today

List FROM "" WHERE file.cday = date("2024-11-12") SORT file.ctime asc

Notes last touched today

List FROM "" WHERE file.mday = date("2024-11-12") SORT file.mtime asc

(Template referenced from Dann Berg, can be found here)


Previous Note 2024-11-07-Thursday Next Note 2024-11-13-Wednesday