Skip to content

Instantly share code, notes, and snippets.

@pce
Created March 13, 2013 16:18
Show Gist options
  • Select an option

  • Save pce/5153718 to your computer and use it in GitHub Desktop.

Select an option

Save pce/5153718 to your computer and use it in GitHub Desktop.
check days with timedelta 0, -7, -1 days before a birthday and mail notification. contacts.db is assumed to be located in the same directory.
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
remd - remember days and get a mail notification.
contacts.db assumed to be in same directory.
Copyright (C) 2013 pce
# Cronjob everyday at 0'o clock
# crontab -e
0 0 * * * /home/user/cron/remday.py > /dev/null 2>&1
"""
import sqlite3
import datetime
import smtplib
import string
import os
import sys
# Config (draft)
mail_receivers = ['mail@exam.ple']
mail_sender = 'mail@exam.ple'
mail_host = 'localhost'
mail_username = ''
mail_password = ''
mail_subject = "[remd] %(firstname)s %(name)s: %(dob)s"
mail_text = "This is a e-mail reminder.\n%(firstname)s %(name)s: %(dob)s,\n%(email)s\n"
def check_days(deltas):
# d = datetime.date.today()
for delta in deltas:
d = datetime.date.today() - datetime.timedelta(days=delta)
check_day(d.strftime("%m-%d"))
def check_day(day):
db = "%s/contacts.db" % os.path.dirname(os.path.abspath(__file__))
con = sqlite3.connect(db)
cursor = con.cursor()
vals = {"day" : day}
sql = 'SELECT * FROM person WHERE SUBSTR(dayofbirth, 6, 5) == :day'
cursor.execute(sql, vals)
# replace with cursor.fetchall() and columnnames
for row in cursor:
print row
mail_notify(row)
def mail_notify(row):
person = {"name": row[3], "firstname": row[1], "email": row[11], "dob": row[10]}
subject = mail_subject % person
text = mail_text % person
message = string.join((
"From: %s" % mail_sender,
"To: %s" % mail_receivers[0],
"Subject: %s" % subject,
"",
text
), "\n")
smtp = smtplib.SMTP(mail_host)
if len(mail_username):
smtp.login(mail_username, mail_password)
smtp.sendmail(mail_sender, mail_receivers, message)
if __name__ == '__main__':
check_days([0, -7, -1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment