views:

123

answers:

2

When I run my program core.py (http://pastebin.com/kbzbBUYd) it returns:

File "core.py", line 47, in texto core.mail(numbersendlist, messagetext) NameError: global name 'core' is not defined

Can anyone tell me what is going on and how I can stop this error?

If it helps, the "import carrier" line in core.py refers to carrier.py (http://pastebin.com/zP2RHbnr)

+1  A: 

core is not a name that you've defined, yet. I expect that you intended to write something like

core = Core('username', 'password')

before calling texto?

ephemient
should it just be "mail" instead of "core.mail" then? Because I tried that but got:File "core.py", line 47, in texto mail(numbersendlist, messagetext)NameError: global name 'mail' is not definedI just want to run the "mail" function from within my texto function.
ErikT
+2  A: 

You're getting NameError Because there's no such name core defined in your code in either local or global scope. Create a Core object first before calling it's methods.

Also the indentation of texto() is probably wrong. You won't be able to use this function from rest of the module. If you want to use it from other parts of current module or from other modules, declare the function at module level or use the @staticmethod decorator to if you want to make it a static method of the class.

This should work.

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
import carrier

class Core:    
    def __init__(self, username, password):
        # code could be added here to auto load these from a file
        self.gmail_user = username
        self.gmail_pwd = password

# Send one text to one number
# TODO: send to multiple addresses

    def mail(self, to, text):
       msg = MIMEMultipart()
       msg['From'] = self.gmail_user
       msg['To'] = to
       msg.attach(MIMEText(text))

       mailServer = smtplib.SMTP("smtp.gmail.com", 587)
       mailServer.ehlo()
       mailServer.starttls()
       mailServer.ehlo()
       mailServer.login(self.gmail_user, self.gmail_pwd)
       mailServer.sendmail(self.gmail_user, to, msg.as_string())
       # Should be mailServer.quit(), but that crashes...
       mailServer.close()


def texto(sendtoaddress, messagetext):
    numbersendlist = []
    for number in sendtoaddress:
        numbersendlist.append(carrier.carriercheck(number))

    core = Core('username', 'password')
    for number in numbersendlist:
        core.mail(number, messagetext)

texto(['1112223333'], 'hi. this better work.')
Imran
That returns: Traceback (most recent call last): File "new.py", line 42, in <module> texto(['1112223333'], 'hi. this better work.') File "new.py", line 40, in texto core.mail(numbersendlist, messagetext)AttributeError: Core instance has no attribute 'mail'
ErikT
Oh wait, some of my indentation was wrong. It instead produces: http://pastebin.com/npx3d8ci
ErikT
Because you were sending the whole list to `mail()` in every iteration instead of a string. Fixed the code in my reply.
Imran
Thank you. That works perfectly.
ErikT