-
Notifications
You must be signed in to change notification settings - Fork 0
/
Email.py
125 lines (100 loc) · 3.68 KB
/
Email.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
import imaplib
import time
import email
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from mailer import Mailer
from mailer import Message
from icalendar import Calendar, Event
from datetime import datetime
imap_host = 'imap.gmail.com'
imap_user = ""
imap_pass = ""
config_file = "account.config" #Read this file for the username and password of email you are sending from
def create_calendar():
cal = Calendar()
cal.add('prodid', '-//My calendar product//mxm.dk//')
cal.add('version', '2.0')
event = Event()
event.add('summary', 'Python meeting about calendaring')
event.add('dtstart', datetime(2005, 4, 4, 8, 0, 0))
event.add('dtend', datetime(2005, 4, 4, 10, 0, 0))
event.add('dtstamp', datetime(2005, 4, 4, 0, 10, 0))
event['uid'] = '20050115T101010/27346262376@mxm.dk'
event.add('priority', 5)
cal.add_component(event)
f = open('example.ics', 'wb')
f.write(cal.to_ical())
f.close()
def read_calendar():
g = open('test.ics', 'rb')
gcal = Calendar.from_ical(g.read())
list_events = []
for component in gcal.walk():
if component.name == "VEVENT":
list_events.append(component)
g.close()
return list_events
def open_config():
global imap_pass, imap_user
file = open(config_file, 'r')
imap_user = file.readline().rstrip() #read in username, get rid of trailing whitespaces and newlines
imap_pass = file.readline().rstrip() #read in password
def parse_command(command, sender):
if command.lower() == 'schedule':
send_email(imap_user, imap_pass, sender, "Test", "Yo")
def send_email(user, pwd, recipient, subject, body):
gmail_user = user
gmail_pwd = pwd
FROM = user
TO = recipient if type(recipient) is list else [recipient]
SUBJECT = subject
TEXT = body
message = Message(From=FROM,
To=TO)
message.Subject = SUBJECT
message.Html = """
<html>
<head></head>
<body>
<p>Hi! You've requested the UTNC practice schedule<br>
How are you?<br>
Here is the <a href="http://www.python.org">link</a> you wanted.
</p>
</body>
</html>
"""
sender = Mailer('smtp.gmail.com',port=587, use_tls=True, usr=gmail_user, pwd=gmail_pwd)
sender.send(message)
def check_email():
## open a connection
open_config()
mail = imaplib.IMAP4_SSL(imap_host)
## login
mail.login(imap_user, imap_pass)
mail.select("inbox")
## get status for the mailbox (folder) INBOX
n = 0
(retcode, messages) = mail.search(None, '(UNSEEN)')
print(retcode, messages)
if retcode == 'OK':
for num in messages[0].split():
n = n + 1
typ, data = mail.fetch(num, '(RFC822)')
for response_part in data:
if isinstance(response_part, tuple):
original = email.message_from_bytes(response_part[1])
print(original['From'])
print(original['Subject'])
parse_command(original['Subject'], original['From'])
typ, data = mail.store(num, '-FLAGS', '\Seen') #Mark the email as unread, this should be added later so it only does it if it is an invalid command
#check_email()
#create_calendar()
list_results = read_calendar()
for component in list_results:
print(component.get('summary'))
start = component.get('dtstart')
end = component.get('dtend')
print(start.dt)
print(end.dt)