I am trying to send an email using smtplib, an app password, and my gmail account and I’m continuously receiving the following error: “2024-09-24 10:32:55,131 ERROR Failed to send notification: (535, b’5.7.8 Username and Password not accepted. For more information, go ton5.7.8 https://support.google.com/mail/?p=BadCredentials af79cd13be357-7acde53d6c8sm73847185a.44 – gsmtp’)”
I’ve tried enabling IMAP and POP like I saw in other stack overflow threads:
Enabled POP/IMAP
I am also using a generated app password.
Here is the code associated:
# notifier.py
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import os
from src.utils.logger import main_logger
class Notifier:
def __init__(self, config):
self.config = config
self.smtp_server = config['smtp_server']
self.smtp_port = config['smtp_port']
self.sender_email = config['sender_email']
self.sender_password = os.getenv('EMAIL_PASSWORD')
self.recipient_email = config['recipient_email']
def send_notification(self, subject, message):
try:
msg = MIMEMultipart()
msg['From'] = self.sender_email
msg['To'] = self.recipient_email
msg['Subject'] = subject
msg.attach(MIMEText(message, 'plain'))
with smtplib.SMTP(self.smtp_server, self.smtp_port) as server:
server.starttls()
server.login(self.sender_email, self.sender_password)
server.send_message(msg)
main_logger.info(f"Notification sent: {subject}")
except Exception as e:
main_logger.error(f"Failed to send notification: {str(e)}")
There is also of course an associated .env and config.yaml file as well.
Cole J. Dermott is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.