Skip to content Skip to sidebar Skip to footer

Sending Email Does Not Work When I Run The Script, But Line By Line, It Works, Why?

I have this simple script that I use to send myself email with a status on a server. It runs, and it gives me no errors, but I do not receive any email, and there is no email in th

Solution 1:

i is not defined. You would see a NameError if you hadn't wrapped try and a bare except around everything. You should have as little as possible in the try block, at least. Also you can catch the specific error as e. This will print the error also so that you can understand what is the problem.

So the code should be:

import smtplib

i = 1
text = "This is remarkable"
fromaddr = "<gmail address>"
toaddr = "<email address>"
msg = """\
        From: <gmail address>
        To: <email address>
        Subject: Message number %i

        %s""" % (i, text)

try: 
    server = smtplib.SMTP("smtp.gmail.com:587")
    server.set_debuglevel(1)
    server.ehlo()
    server.starttls()
    server.login("<email>", "<password>")
    ret = server.sendmail(fromaddr, toaddr, msg)
except Exception as e:
    print'some error occured'print e
else:
    print"returned : ", ret
    print"message sent"
    i += 1

Solution 2:

Your counter variable i is undefined. A try-except is not a loop. You should be more conservative with what you put inside a try block. Only wrap statements that you think could throw an exception that you need to catch. Certainly your msg string should be built outside the exception handler, since it shouldn't fail and if it does your code is wrong. The calls to the mail server could be wrapped:

server = smtplib.SMTP(host="smtp.gmail.com", port="587")
server.set_debuglevel(1)
try:
    server.ehlo()
    server.starttls()
    server.login("<email>", "<password>")
    server.sendmail(fromaddr, toaddr, msg)
    print"message sent"except smtplib.SMTPException as e:
    print"Something went wrong: %s" %str(e)

finally:
    server.close()

In this way you've reduced the amount of code in the try-except block.

When catching exceptions take care not to catch more than you expect. We can define what type of exceptions we expect and then take appropriate action. Here we simply print it. The finally: block will be executed even if an unknown exception is thrown, one that we don't catch. Typically we close a server connection or a file handler here, since an exception thrown in the try block could leave the server connection open. Creating the server instance outside of the try block is necessary here otherwise we are trying to access a variable that would be out of scope if an exception is thrown.

The smtplib.SMTP methods all throw different variants of smtplib.SMTPException so you should look at the definition of the methods and then only catch the right exception type. They all inherit from smtplib.SMTPException so we can catch that type if we are lazy.

Solution 3:

I would suspect the very sensitive SPAM filtering systems which Google has, as you are able to get the alerts when you run the code line by line without any issues. May be introducing a delay by using time.sleep at some places could give the SPAM filter a feel that the disposition of an email is more human than automated.

Adding to that I would also suggest using an alternate SMTP server by any other provider too and I am sure that you would have already check your spam folder....

Post a Comment for "Sending Email Does Not Work When I Run The Script, But Line By Line, It Works, Why?"