-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwelcome e-mail.py
145 lines (116 loc) · 5.83 KB
/
welcome e-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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python3
"""Produces and sends welcome e-mails to newly registered patrons
Author: Jeremy Goldstein, based in part on code provided by Gem Stone-Logan
Contact Info: jgoldstein@minlib.net
"""
import psycopg2
import smtplib
import csv
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import formatdate
from email import encoders
from datetime import date
#use file to gather stats for how many messages are generated each day
csvFile = 'Stats\\New_Patrons.csv'
# These are variables for the email that will be sent.
# Make sure to use your own library's email server (emaihost)
#
emailhost = ''
emailuser = ''
emailpass = ''
emailport = ''
# Enter your own email information
emailfrom= ''
#Connecting to Sierra PostgreSQL database
#Enter your username, password and host
conn = psycopg2.connect("dbname='iii' user='' host='' port='1032' password='' sslmode='require'")
#Opening a session and querying the database for weekly new items
cursor = conn.cursor()
cursor.execute(open("welcome e-mail.sql","r").read())
#For now, just storing the data in a variable. We'll use it later.
rows = cursor.fetchall()
conn.close()
#Track # of generated messages count
with open(csvFile, 'a') as tempFile:
newRow = '\n' + str(len(rows)) + ',' + str(date.today())
tempFile.write(newRow)
tempFile.close()
for rownum, row in enumerate(rows):
#Sending the email message
smtp = smtplib.SMTP(emailhost, emailport)
#for Google connection
smtp.ehlo()
smtp.starttls()
smtp.login(emailuser, emailpass)
# emailto can send to multiple addresses by separating emails with commas
emailto = [str(row[2])]
emailsubject = 'Enjoy your new library card!'
#Creating the email message
#plain text version
text = '''Dear {} {},
Welcome! You have been registered for a library card from {}.
With your new library card you can:
-Borrow and request items from all Minuteman member libraries. Most materials can be returned at any Minuteman location.
-Visit the shared catalog at find.minlib.net to discover books, movies, music, audiobooks, and much more.
-Access digital ebooks, audiobooks, and streaming video from OverDrive.
-Renew items, pay fines online, and track your reading history with MyAccount. To get started, go to find.minlib.net/iii/encore/myaccount and follow the instructions to set-up your login.
-You can also save time by adding the Minuteman App and Text Message Notifications to your mobile phone or tablet.
-Explore Minuteman libraries’ ever-expanding collections of toys and games, telescopes, household tools, musical instruments, and more.
'''.format(str(row[0]),str(row[1]),str(row[5]))
if str(row[4]) != 'the Minuteman Library Network':
text += '''
Visit your home library's website ({}) or talk to your librarian to find out about even more online collections, services, and events offered by your local library.<br><br>
'''.format(str(row[6]))
text += '''Enjoy your new library card!
***This is an automated email. Do not reply.***'''
#html version
html = '''
<html>
<head></head>
<body style="background-color:#202941;">
<table style="width: 70%; margin-left: 15%; margin-right: 15%; border: 0; cellspacing: 0; cellpadding: 0; background-color: #FFFFFF;">
<tr>
<font face="Scala Sans, Calibri, Arial"; size="3">
<p>Dear {} {},<br><br>
Welcome! You have been registered for a library card from <a href="{}">{}.<br><br>
With your new library card you can:<br><br>
<ul>
<li>Borrow and request items from all <a href="http://www.mln.lib.ma.us">Minuteman member libraries</a>. Most materials can be returned at any Minuteman location.</li>
<li>Visit the shared catalog at <a href="http://find.minlib.net">find.minlib.net</a> to discover books, movies, music, audiobooks, and much more.</li>
<li>Access digital ebooks, audiobooks, and streaming video from <a href="minuteman.overdrive.com">OverDrive.</a></li>
<li>Renew items, pay fines online, and track your reading history with MyAccount. To get started, go to <a href="http://find.minlib.net/iii/encore/myaccount">find.minlib.net/iii/encore/myaccount</a> and follow the instructions to set-up your login.</li>
<li>You can also save time by adding the <a href="http://www.mln.lib.ma.us/catalog/faq_app.htm">Minuteman App</a> and <a href="http://www.mln.lib.ma.us/shoutbomb.htm">Text Message Notifications</a> to your mobile phone or tablet.</li>
<li>Explore Minuteman libraries’ ever-expanding collections of toys and games, telescopes, household tools, musical instruments, and more.</li>
</ul><br>
'''.format(str(row[0]),str(row[1]),str(row[6]),str(row[5]))
if str(row[4]) != 'the Minuteman Library Network':
html += '''
Visit <a href="{}">your home library’s website</a> or talk to your librarian to find out about even more online collections, services, and events offered by your local library.<br><br>
'''.format(str(row[6]))
html += '''Enjoy your new library card!
***This is an automated email. Do not reply.***<br><br>
</font>
</p>
<img src="http://www.mln.lib.ma.us/graphics/logo-print-small.jpg" alt="Minuteman logo" height="32" width="188">
</tr>
</table>
</body>
</html>
'''
msg = MIMEMultipart('alternative')
part1 = MIMEText(text,'plain')
part2 = MIMEText(html, 'html')
msg['From'] = emailfrom
if type(emailto) is list:
msg['To'] = ', '.join(emailto)
else:
msg['To'] = emailto
msg['Date'] = formatdate(localtime = True)
msg['Subject'] = emailsubject
msg.attach (part1)
msg.attach (part2)
smtp.sendmail(emailfrom, emailto, msg.as_string())
rownum+1
smtp.quit()