Error in my code? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Error in my code? (/thread-32937.html) |
Error in my code? - ebolisa - Mar-17-2021 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)
RE: Error in my code? - Larz60+ - Mar-17-2021 error line numbers don't match presented code. RE: Error in my code? - ebolisa - Mar-17-2021 (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. RE: Error in my code? - deanhystad - Mar-17-2021 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? RE: Error in my code? - ebolisa - Mar-17-2021 (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. RE: Error in my code? - deanhystad - Mar-17-2021 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 / aNever mind that it fails when a == 0. That is a problem with a, not the code. RE: Error in my code? - ebolisa - Mar-17-2021 (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. 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] RE: Error in my code? - ebolisa - Mar-17-2021 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 |