-1

I am working on a simple program to email me the weather in my city each morning. At the moment, it works, but only once. Using a while loop works, but since I have it set to,

while time == 0600:
    send my mail etc

Now obviously, that makes it so for the entirety of that minute, I get spammed with mail. So I need to figure out a way for something to happen once, every 24 hours.

Here is my full code (currently only working once, until I restart it).

import smtplib, pywapi, datetime
weather = True
loopmsg = True
loopmsg1 = True
def send():
    loopmsg = True
    loopmsg1 = True
    global weather
    while weather == True:
        if loopmsg == True:
            print('Initial Loop Initiated')
            loopmsg = False
        time = datetime.datetime.now()
        time = str(time)
        time = time[11:]
        time = time[:-10]
        time = time.replace(":", "")
        time = int(time)
        fromaddr = 'xxx'
        toaddrs  = 'xxx'
        while time == 0600:
            print('Time is correct')
            weather_com_result = pywapi.get_weather_from_weather_com('ASXX0075')
            msg = "It is " + weather_com_result['current_conditions']['text'].lower() + " and " + weather_com_result['current_conditions']['temperature'] + "°C in Your City."
            msg = msg.encode('utf-8')

            # Credentials (if needed)
            username = 'xxx'
            password = 'xxx'

            # The actual mail send
            server = smtplib.SMTP('smtp.gmail.com:587')
            server.starttls()
            server.login(username,password)
            server.sendmail(fromaddr, toaddrs, msg)
            server.quit()
            print('Sent')
            #weather = False

    #while weather == False:
      #  if loopmsg1 == True:
         #   print('Second Loop Initiated')
         #   loopmsg1 = False
       # while time > 0600:
           # send()

send()
Will
  • 67
  • 7
  • 1
    Why `while`, when an `if` will only run it once? Anyway: 1. add a flag `sentMail` and set it to `True` inside the send routine. 2. Don't send when `sentMail` is `True`. 3. Add a new `if time == 0601` that resets `sentMail` to `False`. – Jongware Apr 24 '16 at 13:12

2 Answers2

2

First of all, you're running a script all day long for it to do something only once a day. This is illogical. You should schedule a task on your OS (Win, Linux, Mac - they all have a way to schedule tasks) so that your script is activated at 6h every day; and remove the time condition inside your script.

If you want to get fancy, create a Telegram bot and have it send you a message any time you want, on your phone, for the location you specify right then.

The script however is easy to fix. You're using that while loop as an if. Just add a variable that will make it send an e-mail only once.

if time == 0500:
    send_email = True
if send_email and time == 0600:
    print('Time is correct')
    send_email = False
    weather_com_result = pywapi.get_weather_from_weather_com('ASXX0075')
    ....
Roberto
  • 2,696
  • 18
  • 31
0

Why not just have a break statement right after you send the email? This just causes you to break out of the loop. Then it will execute the rest of the program.

while time == 0600:
        print('Time is correct')
        weather_com_result = pywapi.get_weather_from_weather_com('ASXX0075')
        msg = "It is " + weather_com_result['current_conditions']['text'].lower() + " and " + weather_com_result['current_conditions']['temperature'] + "°C in Your City."
        msg = msg.encode('utf-8')

        # Credentials (if needed)
        username = 'xxx'
        password = 'xxx'

        # The actual mail send
        server = smtplib.SMTP('smtp.gmail.com:587')
        server.starttls()
        server.login(username,password)
        server.sendmail(fromaddr, toaddrs, msg)
        server.quit()
        print('Sent')
        break
Trey50Daniel
  • 179
  • 3
  • 14