I’m running smtplib
to generate motivations emails to send to my department. As of recently, I was asked to send them out more times throughout the day. With that, I am moving my code from a script to more class based to handle more of the requests. When I moved my code to a class I started getting this error: [Errno 8] nodename nor servname provided, or not known
.
My script was pretty basic before putting it into a class
Script:
with smtplib.SMTP("smtp.gmail.com") as connection:
connection.starttls()
connection.login(my_email, my_password)
connection.sendmail(
from_addr=my_email,
to_addrs=testing_email,
msg=f"Subject:{current_day} Motivationnn{motivation}"
)
When I moved this to a class that’s when it started failing
import smtplib
import random
SMTP_INFORMATION = {
'gmail': 'gmail.smtp.com'
}
class EmailConnection:
def __init__(self, sender_email, receiver_email):
self.sender_email = sender_email
self.receiver_email = receiver_email
self.port = 587
self.correct_smtp = SMTP_INFORMATION
def generate_connection(self, motivational_quote):
with smtplib.SMTP(self.correct_smtp['gmail'], self.port, local_hostname='127.0.0.1') as connection:
connection.starttls()
connection.login(user=self.sender_email,
password='team_password')
connection.sendmail(from_addr=self.sender_email,
to_addrs=self.receiver_email,
msg=f"Subject:{time_of_day}Motivationnn{motivational_quote}")
connection.close()
def get_quotes(file_locator):
with open(file_locator, 'r') as file:
quotes = [line.strip() for line in file]
return quotes
my_connection = EmailConnection('[email protected]', '[email protected]')
my_connection.generate_connection(motivation)
The motivation
parameter is an API connection to get quotes. For this example, I pulled a couple down and put in a .txt
file
What am I missing to resolve this error?