Sending email does not work when I run the script, but line by line, it works, why?

Trausti Thor

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 the sent folder at Google. But if I copy and paste every line by line into python in a shell, it does send email and works. There are no errors anywhere. I even get accepted status from Google.

UPDATE: I might have cut to much code out of the sample, and the i variable was accidentally taken out. I have added it again. When I copy paste each line into python cmd line, the script works. When I just run the script, it reports no errors, but does not send the email.

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

            %s
    """ % (i, text)

    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)
    print "returned : ", ret
    print "message sent"
    i += 1
except:
    print "Now some crazy **** happened"
Vipul

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does this set of apt commands work when run line by line, but not when run as a script?

From Dev

Why does this shell script fail, if it works when I enter each line into a terminal?

From Dev

Why does this script work from the command line, but not when I put it in a windows batch file?

From Dev

TestNG: Retry works when xml is run from eclipse, does not work when xml is run from command line

From Dev

Why does RewriteRule only work when I change the line position?

From Dev

why does this IF statement work on the command line but not in a script?

From Dev

Shell script called from PHP does not work, but it works in command line

From Dev

shopt works in command line, not found when run in a script

From Dev

shopt works in command line, not found when run in a script

From Dev

code works in script editor, but not when run from command line

From Dev

Why does the same sed regex (after grep) fail when run in a bash script vs bash command line?

From Dev

Why does the Python script to send data to Slack web hook not work when variable is pulled from a line?

From Dev

this BASH code works on a command line, but not when i move it into a script

From Dev

OSX: Why does exit work when written manually in the terminal, but not with a shell script I run from the terminal?

From Dev

Subversion post-commit hook will not run on commit, script works because I can run it on the command line

From Dev

command line grep * does not work but grep *. works

From Dev

I want to fill between two line: why it does not work

From Dev

currency converter- why does my for loop in switch wont work? when i run my code only the first for loop works

From Dev

Why does this script fail when run from cron, but works when run manually?

From Dev

Multi-line email body when sending emails from excel

From Dev

java string new line not working when sending with email

From Dev

Sending email does not work when I use my helper in Yii php

From Dev

Cannot run bash script from crontab when it works from command line bash

From Dev

daemon /etc/init.d/ error when run as daemon but script works fine from command line

From Dev

Regular expression works when tested with online tools, but does not work when executed in command line

From Dev

Why does my output go to next line when there is no next line character in the script

From Dev

Why does my script not run the if statement when I type 5?

From Dev

How does this line from a Bash Script work?

From Dev

How does this line from a Bash Script work?

Related Related

  1. 1

    Why does this set of apt commands work when run line by line, but not when run as a script?

  2. 2

    Why does this shell script fail, if it works when I enter each line into a terminal?

  3. 3

    Why does this script work from the command line, but not when I put it in a windows batch file?

  4. 4

    TestNG: Retry works when xml is run from eclipse, does not work when xml is run from command line

  5. 5

    Why does RewriteRule only work when I change the line position?

  6. 6

    why does this IF statement work on the command line but not in a script?

  7. 7

    Shell script called from PHP does not work, but it works in command line

  8. 8

    shopt works in command line, not found when run in a script

  9. 9

    shopt works in command line, not found when run in a script

  10. 10

    code works in script editor, but not when run from command line

  11. 11

    Why does the same sed regex (after grep) fail when run in a bash script vs bash command line?

  12. 12

    Why does the Python script to send data to Slack web hook not work when variable is pulled from a line?

  13. 13

    this BASH code works on a command line, but not when i move it into a script

  14. 14

    OSX: Why does exit work when written manually in the terminal, but not with a shell script I run from the terminal?

  15. 15

    Subversion post-commit hook will not run on commit, script works because I can run it on the command line

  16. 16

    command line grep * does not work but grep *. works

  17. 17

    I want to fill between two line: why it does not work

  18. 18

    currency converter- why does my for loop in switch wont work? when i run my code only the first for loop works

  19. 19

    Why does this script fail when run from cron, but works when run manually?

  20. 20

    Multi-line email body when sending emails from excel

  21. 21

    java string new line not working when sending with email

  22. 22

    Sending email does not work when I use my helper in Yii php

  23. 23

    Cannot run bash script from crontab when it works from command line bash

  24. 24

    daemon /etc/init.d/ error when run as daemon but script works fine from command line

  25. 25

    Regular expression works when tested with online tools, but does not work when executed in command line

  26. 26

    Why does my output go to next line when there is no next line character in the script

  27. 27

    Why does my script not run the if statement when I type 5?

  28. 28

    How does this line from a Bash Script work?

  29. 29

    How does this line from a Bash Script work?

HotTag

Archive