Skip to content

Commit 8b09ed8

Browse files
Day 9 - Send Email and Read Inbox with Python
1 parent 815ec02 commit 8b09ed8

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
msg_template = """Hello {name},
2+
Thank you for joining {website}. We are very
3+
happy to have you with us.
4+
""" # .format(name="Justin", website='cfe.sh')
5+
6+
def format_msg(my_name="Justin", my_website="cfe.sh"):
7+
my_msg = msg_template.format(name=my_name, website=my_website)
8+
return my_msg

tutorial-reference/Day 9/inbox.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import imaplib
2+
import email
3+
4+
host = 'imap.gmail.com'
5+
username = 'hungrypy@gmail.com'
6+
password = '<your password>'
7+
8+
9+
def get_inbox():
10+
mail = imaplib.IMAP4_SSL(host)
11+
mail.login(username, password)
12+
mail.select("inbox")
13+
_, search_data = mail.search(None, 'UNSEEN')
14+
my_message = []
15+
for num in search_data[0].split():
16+
email_data = {}
17+
_, data = mail.fetch(num, '(RFC822)')
18+
# print(data[0])
19+
_, b = data[0]
20+
email_message = email.message_from_bytes(b)
21+
for header in ['subject', 'to', 'from', 'date']:
22+
print("{}: {}".format(header, email_message[header]))
23+
email_data[header] = email_message[header]
24+
for part in email_message.walk():
25+
if part.get_content_type() == "text/plain":
26+
body = part.get_payload(decode=True)
27+
email_data['body'] = body.decode()
28+
elif part.get_content_type() == "text/html":
29+
html_body = part.get_payload(decode=True)
30+
email_data['html_body'] = html_body.decode()
31+
my_message.append(email_data)
32+
return my_message
33+
34+
35+
if __name__ == "__main__":
36+
my_inbox = get_inbox()
37+
print(my_inbox)
38+
# print(search_data)

tutorial-reference/Day 9/send.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import sys
2+
import requests
3+
from datetime import datetime
4+
5+
from formatting import format_msg
6+
from send_mail import send_mail
7+
8+
def send(name, website=None, to_email=None, verbose=False):
9+
assert to_email != None
10+
if website != None:
11+
msg = format_msg(my_name=name, my_website=website)
12+
else:
13+
msg = format_msg(my_name=name)
14+
if verbose:
15+
print(name, website, to_email)
16+
# send the message
17+
try:
18+
send_mail(text=msg, to_emails=[to_email], html=None)
19+
sent = True
20+
except:
21+
sent = False
22+
return sent
23+
24+
if __name__ == "__main__":
25+
print(sys.argv)
26+
name = "Unknown"
27+
if len(sys.argv) > 1:
28+
name = sys.argv[1]
29+
email = None
30+
if len(sys.argv) > 2:
31+
email = sys.argv[2]
32+
response = send(name, to_email=email, verbose=True)
33+
print(response)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import smtplib
2+
from email.mime.text import MIMEText
3+
from email.mime.multipart import MIMEMultipart
4+
5+
# environment variables
6+
username = 'hungrypy@gmail.com'
7+
password = 'LetsGetItStarted2020'
8+
9+
def send_mail(text='Email Body', subject='Hello World', from_email='Hungry Py <hungrypy@gmail.com>', to_emails=None, html=None):
10+
assert isinstance(to_emails, list)
11+
msg = MIMEMultipart('alternative')
12+
msg['From'] = from_email
13+
msg['To'] = ", ".join(to_emails)
14+
msg['Subject'] = subject
15+
txt_part = MIMEText(text, 'plain')
16+
msg.attach(txt_part)
17+
if html != None:
18+
html_part = MIMEText(html, 'html')
19+
msg.attach(html_part)
20+
msg_str = msg.as_string()
21+
# login to my smtp server
22+
server = smtplib.SMTP(host='smtp.gmail.com', port=587)
23+
server.ehlo()
24+
server.starttls()
25+
server.login(username, password)
26+
server.sendmail(from_email, to_emails, msg_str)
27+
server.quit()
28+
# with smtplib.SMTP() as server:
29+
# server.login()
30+
# pass

0 commit comments

Comments
 (0)