0

I know I have already asked a question like this before but I have made my code much cleaner and I am still coming up with a problem.

My code goes like this:

 class Email_Stuff:
 def Get_From_Email():
 #code to open up window and get email address
 emailaddr = #the input
 return emailaddr
 def Get_To_Email():
 #code to open up window and get to email address
 recipaddr = #the input
 return recipaddr
 def Get_Email_Address():
 #code to open up window and get email username
 EmailUser = #the input
 return EmailUser
 def Get_Email_Password():
 #code to open up window and get email password
 EmailPass = #the input
 return EmailPass
 def Send_Email():
 import smtplib
 server = smtplib.SMTP('smtp.gmail.com', 587)
 server.login((EmailUser),(EmailPass))
 message = "Python Test Email"
 server.sendmail(emailaddr,recipaddr,message)

I need to get the variables: emailaddr, recipaddr, EmailUser, and EmailPass into the function Send_Email. I'm not sure how I could do that though because when I run this code, it tells me that "the global name isn't defined".

Any ideas?

Jeroen Vannevel
44.6k25 gold badges114 silver badges174 bronze badges
asked Dec 28, 2013 at 3:37
1
  • Do you intend the methods to be static? Commented Dec 28, 2013 at 3:45

2 Answers 2

2

Make emailaddr, recipaddr, EmailUser, and EmailPass become instance variables by adding prefix "self.".

class Email_Stuff():
 def Get_From_Email(self):
 #code to open up window and get email address
 self.emailaddr = #the input
 def Get_To_Email(self):
 #code to open up window and get to email address
 self.recipaddr = #the input
 def Get_Email_Address(self):
 #code to open up window and get email username
 self.EmailUser = #the input
 def Get_Email_Password(self):
 #code to open up window and get email password
 self.EmailPass = #the input
 def Send_Email(self):
 import smtplib
 server = smtplib.SMTP('smtp.gmail.com', 587)
 server.login((self.EmailUser),(self.EmailPass))
 message = "Python Test Email"
 server.sendmail(self.emailaddr,self.recipaddr,self.message)
instance = Email_Stuff()
instance.Get_From_Email()
instance.Get_To_Email()
instance.Get_Email_Address()
instance.Get_Email_Password()
instance.Send_Email()

BTW, name of methods should be lowercase.

answered Dec 28, 2013 at 3:45
Sign up to request clarification or add additional context in comments.

6 Comments

ok i tried this and now I am getting this error message: TypeError: Get_From_Email() missing 1 required positional argument: 'self'
be careful. Send_Email has a param 'self'.
And Send_Email must be called by an instance. You can new an instance by instance = Email_Staff()
where would I put that @LeeWong
is there any way to hook this code up to a button so that it would run as soon as I run the application?
|
0

First, if you want these functions to methods associated with an instance of this class, then each method will need a reference to the instance itself as the first argument, usually designated as self, though you can name it anything you like.

For example:

 def Get_Email_Password(self):
 #code to open up window and get email password
 EmailPass = #the input
 return EmailPass

Next, you have two options to get the values ready for sendmail. You can either call each method from within the Send_Email method and store the returned values for each one. That would look like this:

def Send_Email(self):
 emailaddr = self.Get_For_Email()
 recipaddr = self.Get_Email_Address()
 ...

Or you can store the values, instead of returning them, as instance variables. So, you would have something like this:

 def Get_Email_Password(self):
 #code to open up window and get email password
 EmailPass = #the input
 self.emailaddr = EmailPass

And then, in your Send_Email method, you would reference the instance variables you have saved:

def Send_Email(self):
 ...
 server.sendmail(self.emailaddr, self.recipaddr, self.message)

How you choose to do it is up to you, but I prefer the first way. I also suggest you read up on PEP8

answered Dec 28, 2013 at 3:45

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.