-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathmyMail_client.py
More file actions
118 lines (108 loc) · 3.71 KB
/
myMail_client.py
File metadata and controls
118 lines (108 loc) · 3.71 KB
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
#!/usr/bin/env python3
# Requires a secret.pyc file containing email credentials for running
"""
Manage your gmail right from your terminal!
Ensure that you have a secret.pyc file for your credentials, with format:
###
MAILBOX = "your_gmail_username"
PASSWD = "your gmail password"
###
You might need to enable access to less secure apps in your gmail settings.
"""
from imaplib import IMAP4_SSL
from smtplib import SMTP_SSL, SMTPAuthenticationError
from poplib import POP3_SSL, error_proto
import pydoc
try:
from secret import * # Gets credentials for gmail account.
except ImportError:
print("Please create a secret.pyc file for your credentials.")
exit()
from socket import gaierror
import subprocess
from time import sleep
SMTPSVR = 'smtp.gmail.com'
IMAPSVR = 'imap.gmail.com'
who = MAILBOX + '@gmail.com'
def send_msg():
with open('email', 'w') as msg:
msg.write('From: YOUR_NAME_HERE <%s>\n' % who)
msg.write('To: RECIPIENTS_HERE\n')
msg.write('Subject: YOUR_SUBJECT_HERE\n\n')
subprocess.call(['nano', 'email'])
with open('email', 'rb') as msg:
body = msg.read().decode('unicode_escape') # INCLUDES HEADERS
body_list = body.split('\n')
breaker = body_list.index('')
origHeaders = body_list[:breaker] # List
recipients = origHeaders[1].split(', ')
origBody = '\n'.join(body_list[breaker+1:])
try:
sendSvr = SMTP_SSL(SMTPSVR, 465)
except gaierror:
print("Can't connect to %s." % SMTPSVR)
exit()
sendSvr.ehlo()
#sendSvr.starttls()
try:
sendSvr.login(who, PASSWD)
except SMTPAuthenticationError:
print("Invalid SMTP credentials.")
exit()
errs = sendSvr.sendmail(who, recipients, body)
sendSvr.quit()
assert len(errs) == 0, errs
print("Email sent!")
wanna_mail = input("Welcome to your email client. Would you like to compose an email? (Y/N) ")
if wanna_mail == 'Y':
send_msg()
else:
print("Alright. Now your inbox will be displayed.")
# IMAP stuff below.
try:
recvSvr = IMAP4_SSL(IMAPSVR, 993)
except gaierror:
print("Can't connect to %s." % IMAPSVR)
exit()
try:
recvSvr.login(who, PASSWD)
except Exception:
print("Can't login. Check your credentials and try again.")
exit()
rsp, msgs = recvSvr.select('INBOX', True)
length = input("How many latest email would you like to be displayed? ")
low = int(msgs[0]) - int(length)
if low <= 0:
low = 1
#try:
rsp, data = recvSvr.fetch("%s:%s" % (low, int(msgs[0])), '(BODY[HEADER])')
#except Exception:
# print("Can't fetch email.")
# exit()
titles = []
for item in data:
if isinstance(item, tuple):
mid = item[0].decode('unicode_escape').split()[0]
for line in item[1].decode('unicode_escape').split('\r\n'):
if line.startswith('Subject:'):
sub = line
break
titles.append(mid + ' - ' + sub)
string = '\n'.join(titles[::-1])
string = 'Use message id to access a specific email.\n\n' + string
print(string)
while 1:
mid = input("Enter the mid of the message you want to read, or skip to quit: ")
if not mid:
break
rsp, data = recvSvr.fetch(mid, '(RFC822)')
stuff = data[0][1].decode('unicode_escape').split('\r\n')
for i in range(len(stuff)):
if stuff[i].startswith('From:'):
break
headers = stuff[i:i+3]
breaker = stuff.index('')
body = stuff[breaker:] #includes the empty string
msg_to_be_displayed = '\n'.join(headers + body)
pydoc.pager(msg_to_be_displayed)
print("Thanks for using the client. Exit done.")