|
| 1 | +#! python3 |
| 2 | +# sendDuesReminders.py - Sends emails based on their status in spreadsheet. |
| 3 | + |
| 4 | +import openpyxl, smtplib, sys |
| 5 | + |
| 6 | +# Open the spreadsheet and get the latest dues status. |
| 7 | +wb = openpyxl.load_workbook('duesRecords.xlsx') |
| 8 | +sheet = wb.get_sheet_by_name('Sheet1') |
| 9 | + |
| 10 | +lastCol = sheet.get_highest_column() |
| 11 | +latestMonth = sheet.cell(row=1, column=lastCol).value |
| 12 | + |
| 13 | +unpaidMembers = {} |
| 14 | +# Check each member's payment status |
| 15 | +for r in range(2, sheet.get_highest_row() + 1): |
| 16 | + payment = sheet.cell(row=r, column=lastCol).value |
| 17 | + if payment != 'paid': |
| 18 | + name = sheet.cell(row=r, column=1).value |
| 19 | + email = sheet.cell(row=r, column=2).value |
| 20 | + unpaidMembers[name] = email |
| 21 | + |
| 22 | +# Log in to email account. |
| 23 | +smtpObj = smtplib.SMTP('smtp.gmail.com', 587) |
| 24 | +smtpObj.ehlo() |
| 25 | +smtpObj.starttls() |
| 26 | +smtpObj.login('my_email_address@gmail.com', sys.argv[1]) |
| 27 | + |
| 28 | +# Send out reminder emails. |
| 29 | +for name, email in unpaidMembers.items(): |
| 30 | + body = 'Subject: %s dues unpaid.\nDear %s,\nRecords show that you have not paid dues for %s. Please make this payment as soon as possible. Thank you!' % (latestMonth, name, latestMonth) |
| 31 | + print('Sending email to %s...' % email) |
| 32 | + sendmailStatus = smtpObj.sendmail('my_email_address@gmail.com', email, body) |
| 33 | + |
| 34 | + if sendmailStatus != {}: |
| 35 | + print('There was a problem sending email to %s: %s' % (email, sendmailStatus)) |
| 36 | +smtpObj.quit() |
0 commit comments