-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsend_welcome_mail.py
57 lines (42 loc) · 1.23 KB
/
send_welcome_mail.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
from email.message import EmailMessage
from mailconfig import SERVER, PORT, USER, PW, FROM, TO, URL, CC
import calendar
import datetime
import smtplib
import sqlite3
import sys
import transaction
import mailer
def db(func):
def func_wrapper(*args, **kw):
db = sqlite3.connect('db.sqlite3')
db.row_factory = sqlite3.Row
c = db.cursor()
res = func(c, *args, **kw)
db.commit()
db.close()
return res
return func_wrapper
@db
def get_new_registrations(c):
c.execute("SELECT * FROM registration_member where status='exported'");
return c.fetchall()
@db
def change_status_to_mailsent(c, reg_id):
c.execute("UPDATE registration_member SET status='mail_sent' where id=(?)", (reg_id,))
new_regs = get_new_registrations()
if not new_regs:
sys.exit(0)
def send_mail(reg):
with open('mailtemplate.in', 'r') as templatefile:
template = templatefile.readlines()
msg = EmailMessage()
msg.set_content(''.join(template[1:]).format(**reg))
msg['Subject'] = template[0].strip()
msg['From'] = FROM
msg['To'] = reg['email']
msg['CC'] = TO
mailer.send_mail(msg)
for reg in new_regs:
send_mail(reg)
change_status_to_mailsent(reg['id'])