-
Notifications
You must be signed in to change notification settings - Fork 3
/
mail.py
139 lines (119 loc) · 5.25 KB
/
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
# -*- coding: utf-8 -*-
import csv
import os
import glob
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.utils import formatdate
EMAIL_ADDRESS = '[email protected]'
PASSWORD = 'your_password'
##########################################################
###### STEPS:
######
###### 1.) Modify line EMAIL_ADDRESS and PASSWORD above
###### --> account has to be configured on gmail to permit 3rd party access
######
###### 2.) Place the CSV file(s) downloaded from PayPal under MatsuriEmail
######
###### 3.) Edit subject
######
###### 4.) Edit messages (bodyEN in English and bodyJP in Japanese)
######
###### 5.) uncomment run() at the very bottom of this file, then run
###### * TEST WITH YOUR OWN NAME AND EMAIL ADDRESS FIRST!!*
##########################################################
def setup(addressBook, names, emails):
index = 0
for name in names:
addressBook[name] = emails[index]
index += 1
def check_csv():
ext1 = 'CSV'
ext2 = 'csv'
CSV = [i for i in glob.glob('*.{}'.format(ext1))]
csv = [i for i in glob.glob('*.{}'.format(ext2))]
if (len(CSV)==0):
if (len(csv)==0):
return ""
else:
return csv
else:
if (len(csv)==0):
return CSV
else:
return CSV + csv
def organize(fileName):
if (fileName != ""):
with open(fileName, 'rU', encoding='utf8') as info:
reader = csv.DictReader(info)
data = {}
for row in reader:
for header, value in row.items():
try:
data[header].append(value)
except KeyError:
data[header] = [value]
names = data['Name']
emails = data['From Email Address']
addressBook = dict()
setup(addressBook, names, emails)
return addressBook
def create_message(from_addr, to_addr, subject, body, encoding):
msg = MIMEText(body, 'plain', encoding)
msg['Subject'] = Header(subject, encoding)
msg['From'] = from_addr
msg['To'] = to_addr
msg['Date'] = formatdate()
return msg
def send_via_gmail(from_addr, to_addr, msg):
s = smtplib.SMTP('smtp.gmail.com', 587)
s.ehlo()
s.starttls()
s.ehlo()
s.login(EMAIL_ADDRESS, PASSWORD)
s.sendmail([from_addr], [to_addr], msg.as_string())
s.close()
def run():
data = check_csv()
if(data == ""):
print("No file found in directory")
return
for i in data:
book = organize(i)
for recepientName in book:
if recepientName == "":
print("ecountered NONE name!")
continue # for errors
from_addr = EMAIL_ADDRESS
to_addr = book[recepientName]
subject = "Matsuri 2018 Ticket Infromation //「祭」チケットについて"
bodyEN = "Dear %s, \n\n\
Hello!\n\n\
You are receiving this message because you have purchased a ticket for Matsuri last year. \n\n\
We are excited to invite you back to the flagship event of the Japanese Student Association, MATSURI.\n\n\
The event will be held on the 10th of April (Tuesday) this year. We hope to deliver the uniqueness of Japanese culture in the Wiegand Gym this year!\n\n\
With that being said, we would like to announce the release of the online tickets for Matsuri 2018, and offer you the notification of details. \n\n\
- Tickets preordered online will have a 10 percent discount\n\
- Tickets can be picked up at a booth during the event without queueing\n\
- Tickets can be ordered via https://cmujsa.com/matsuri/tickets/\n\n\
For questions or more information about the event, please contact [email protected] (JSA President) \n\n\
We hope you come back and enjoy the event with us! \n\n\
Matsuri website: https://matsuri.cmujsa.com\n\n" % recepientName
textBreak = "---------------------------------------------------------------------------------\n\n"
bodyJP = "%s様、 \n\n\
こんにちは!\n\n\
昨年カーネギーメロン大学で開催された「祭」というイベントでチケットをオンライン予約購入していただいた方にメールを送らせていただいております。 \n\n\
今年度は4月10日(火)に「祭」を開催することになりました。\n\n\
つきましては、昨年大変お世話になりました皆様に2018年度オンラインチケット予約のご案内をさせていただきます。\n\n\
- オンライン予約されたチケットはすべて10パーセント割引とさせて頂きます\n\
- 当日、予約されたチケットは別カウンターにて待ち時間0でお手渡し致します\n\
- チケット予約はこちらから https://cmujsa.com/matsuri/tickets/\n\n\
ご質問等ございましたら [email protected] (JSA会長) までよろしくお願い致します。\n\n\
「祭」でお会いしましょう! \n\n\
祭ウェブサイト:https://matsuri.cmujsa.com\n\n" % recepientName
msg = create_message(from_addr, to_addr, subject,
bodyEN+textBreak+bodyJP, 'ISO-2022-JP')
send_via_gmail(from_addr, to_addr, msg)
print (recepientName + " sent!")
# run()