nonetype error when securing passwords in environment variables in windows 11

37 views Asked by At

2023-10-17 00:21:31.357 Uncaught app exception Traceback (most recent call last): File "D:\pythonProject\portfolio\send_email.py", line 21, in send_email server.login(username, password) File "C:\Users\DELL\AppData\Local\Programs\Python\Python310\lib\smtplib.py", line 739, in login (code, resp) = self.auth( File "C:\Users\DELL\AppData\Local\Programs\Python\Python310\lib\smtplib.py", line 652, in auth authobject(challenge).encode('ascii'), eol='') AttributeError: 'NoneType' object has no attribute 'encode'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "C:\Users\DELL\AppData\Local\Programs\Python\Python310\lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 541, in _run_script exec(code, module.dict) File "D:\pythonProject\portfolio\pages\Contact Us.py", line 17, in
send_email(message) File "D:\pythonProject\portfolio\send_email.py", line 20, in send_email with smtplib.SMTP_SSL(host, port, context=context) as server: File "C:\Users\DELL\AppData\Local\Programs\Python\Python310\lib\smtplib.py", line 284, in exit raise SMTPResponseException(code, message) smtplib.SMTPResponseException: (535, b'5.7.8 Username and Password not accepted. Lear n more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials ja10-20020a170902efca00b001c62e3e1286sm8861643plb.166 - gsmtp')

import smtplib, ssl
from dotenv import load_dotenv   #for python-dotenv method
import os

from pathlib import Path  
env_path = Path('.') / '.env'
load_dotenv(dotenv_path=env_path)


def send_email(message):
    host = "smtp.gmail.com"
    port = 465

    username = "[email protected]"
    password = os.environ.get("pswrd")

    receiver = "[email protected]"
    context = ssl.create_default_context()

    with smtplib.SMTP_SSL(host, port, context=context) as server:
        server.login(username, password)
        server.sendmail(username, receiver, message)

I used this code to send mail in my program and created a environment variable to secure my password in it from control panel > System and Security > System > Advanced system Settings>Environment Variables but it is showing that the password as not accepted when i used os.environ.get("pswrd") in python console it is also showing none

0

There are 0 answers