Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error in my code?
#1
Hi,

The following code works, but if I define it, it fails. Where's my error?
TIA

import sys

mailScript = "C:\SharedFiles\Python\practice\mailTest.py"

password = "mypass"

script_descriptor = open(mailScript)
a_script = script_descriptor.read()

sys.argv = ["", password, "[email protected]", "subject", "hello body_text"]
exec(a_script)

script_descriptor.close()
import sys


def main():
    # password mailto subject bodyText
    mailScript = "C:\SharedFiles\Python\practice\mailTest.py"

    PASSWORD = "mypass"

    script_descriptor = open(mailScript)
    a_script = script_descriptor.read()

    sys.argv = ["", PASSWORD, "[email protected]", "subject", "hello body_text"]
    exec(a_script)

    script_descriptor.close()


main()
import smtplib, sys
from email.mime.text import MIMEText


def main(a, b, c, d):
    USERNAME = "[email protected]"
    PASSWORD = sys.argv[1]
    MAILTO = sys.argv[2]

    subject = sys.argv[3]
    bodyText = sys.argv[4]

    msg = MIMEText(bodyText)
    msg['Subject'] = subject
    msg['From'] = USERNAME
    msg['To'] = MAILTO

    server = smtplib.SMTP('smtp.gmail.com:587')
    server.ehlo_or_helo_if_needed()
    server.starttls()
    server.ehlo_or_helo_if_needed()
    server.login(USERNAME, PASSWORD)
    server.sendmail(USERNAME, MAILTO, msg.as_string())
    server.quit()


if __name__ == "__main__":
    a = sys.argv[1]
    b = sys.argv[2]
    c = sys.argv[3]
    d = sys.argv[4]

    main(a, b, c, d)
Error:
Traceback (most recent call last): File "C:\SharedFiles\Python\practice\test.py", line 19, in <module> main() File "C:\SharedFiles\Python\practice\test.py", line 14, in main exec(a_script) File "<string>", line 33, in <module> File "<string>", line 13, in main NameError: name 'MIMEText' is not defined Process finished with exit code 1
Reply
#2
error line numbers don't match presented code.
Reply
#3
(Mar-17-2021, 07:13 PM)Larz60+ Wrote: error line numbers don't match presented code.

Edited line numbers. The reason why didn't match is because I omitted to list the commented lines.
Reply
#4
After editing run the code again to get correct line numbers. Since most of your code is in mail.py (at least 125 lines long) why didn't you provide that?
Reply
#5
(Mar-17-2021, 08:37 PM)deanhystad Wrote: After editing run the code again to get correct line numbers. Since most of your code is in mail.py (at least 125 lines long) why didn't you provide that?
Post updated. I didn't think the problem was with the receiving code as the sending code works when not used in a function. Thanks.
Reply
#6
There obviously is a problem with the calling code that appears when it is called from inside a function. Without seeing where the error is appearing it is difficult to see where the error occurred.

This code has no errors because it works when a == 1.
x = 5 / a
Never mind that it fails when a == 0. That is a problem with a, not the code.
Reply
#7
(Mar-17-2021, 10:37 PM)deanhystad Wrote: There obviously is a problem with the calling code that appears when it is called from inside a function. Without seeing where the error is appearing it is difficult to see where the error occurred.

This code has no errors because it works when a == 1.
x = 5 / a
Never mind that it fails when a == 0. That is a problem with a, not the code.

Not sure I follow you. I'm passing 4 variables outside or within a function. x (0 position) is not used.

 x = sys.argv[0]
    a = sys.argv[1]
    b = sys.argv[2]
    c = sys.argv[3]
    d = sys.argv[4]
Reply
#8
I found out that if add these libs in the sending code, it solves the problem but not sure I understand why.

import smtplib, sys
from email.mime.text import MIMEText
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020