-
Notifications
You must be signed in to change notification settings - Fork 1
/
emails.py
executable file
·115 lines (85 loc) · 3.74 KB
/
emails.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python
from __future__ import print_function
import sys
from datetime import date
#from dateutil.relativedelta import relativedelta
import calendar
import gnucashxml
import getopt
import re
import imaplib
import email.message
import time
import getpass
DATE = 'Date'
MEMBERS = 'Members'
DONATING_MEMBERS = 'Donating members'
def main(argv):
now = date.today()
try:
opts, args = getopt.getopt(argv, "a:b:c:")
except getopt.GetoptError:
print("argument error")
sys.exit(2)
email_user = raw_input("Gmail username: ")
email_pass = getpass.getpass("Gmail password: ")
filename = args[0]
book = gnucashxml.from_filename(filename)
# today = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
# if today.day < MONTH_START_DAY:
# today -= relativedelta(months=+1)
active_members = []
active_member_accounts = book.find_account("Active Members")
for account in active_member_accounts.find_account("Full Members").children:
member = Member(account)
active_members.append(member)
for account in active_member_accounts.find_account("Student Members").children:
member = StudentMember(account)
active_members.append(member)
for member in active_members:
if member.effective_balance() < 0:
print(member.name(), "has a balance of", member.effective_balance(), " ", member.email())
if member.email() == None:
print("ERROR:", member.name(), "does not have an email address on record")
else:
gmail = imaplib.IMAP4_SSL('imap.gmail.com', port = 993)
gmail.login(email_user, email_pass)
gmail.select('[Gmail]/Drafts')
msg = email.message.Message()
msg['Subject'] = 'SkullSpace Dues'
msg['To'] = member.email()
msg['CC'] = '[email protected]'
msg.set_payload('Hello ' + member.name() + ',\n\nAccording to our records, your account balance is currently $' + str(member.effective_balance()) + '. Dues for the month of ' + calendar.month_name[now.month % 12 + 1] + ' were due on ' + calendar.month_name[now.month] + ' 15th. If you believe there is an issue with this record, please let us know.\n\nThank you,\n\n- Your SkullSpace Board of Directors')
# extra_late_warning = "Note that since you are more than 3 months behind, you are at risk of losing your membership. Please contact us to make arrangements as soon as possible."
gmail.append("[Gmail]/Drafts",
'',
imaplib.Time2Internaldate(time.time()),
str(msg))
#balance = member.balance()
#spacer1 = " " * (34 - len(member.name()))
#spacer2 = " " * (6 - len(str(balance)))
#print("Account", member.type(), ":", member.name(), spacer1, "Balance:", balance, spacer2, "Effective bal:", member.effective_balance(), "email:", member.email())
class Member(object):
monthy_dues = 40
membership_type = "Regular"
def __init__(self, account):
self.account = account
def type(self):
return self.membership_type
def name(self):
return self.account.name
def balance(self):
return sum((-1 * split.value) for split in self.account.splits)
def effective_balance(self):
return self.balance() - self.monthy_dues
def email(self):
email = re.search("[^@ ]+@[^@ ]+\.[^@ ]+", self.account.description)
if email is None:
return None
else:
return email.group()
class StudentMember(Member):
monthy_dues = 20
membership_type = "Student"
if __name__ == "__main__":
main(sys.argv[1:])