Email Reminder – Remind others via email depending on time of day

import csv
import datetime
import time
import smtplib

email_host = "host.email.com"
email_port = 587
email_from = "me@email.com"
email_password = "password"
# unsafe apps allowed?

csv_filename = "addresses_test.csv"

with open(csv_filename) as handle:
    reader = csv.reader(handle, delimiter=";")
    data = list(reader)


def check_time():
    now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    today = datetime.datetime.today()
    if today.weekday() == 0 and today.hour == 7 and today.minute == 0 and today.second > 25:  # Montag 07:00
        print(f"{now} It is time!")
        return True
    print(f"{now} It is not yet time!")
    return False


def send_mail():
    server = smtplib.SMTP(email_host, email_port)
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.login(email_from, email_password)

    subject = "Neue Infos für die Imker-Website?"
    body = f"Hallo liebe Leute,\n\nhabt ihr neue Infos für mich, die auf die imker-gl.de Seite gehören? Falls ja, " \
           f"schreibt mir doch bitte eine nette E-Mail zurück.\n\nDanke und Gruß\nRobin Kaiser"
    msg = f"Subject: {subject}\n\n{body}".encode('utf-8')

    recp = []
    for entry in data:
        recp.append(entry[2])
    recp = ", ".join(recp)
    print(f"email sent to ({recp})")
    server.sendmail(email_from, recp, msg)
    server.quit()


if __name__ == '__main__':
    while True:
        if check_time():
            send_mail()
        else:
            time.sleep(30)

 

Leave a Reply

Your email address will not be published. Required fields are marked *

2 × 5 =