-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy path5_6_send_email_from_gmail.py
60 lines (53 loc) · 1.91 KB
/
5_6_send_email_from_gmail.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
#!/usr/bin/env python
# Python Network Programming Cookbook, Second Edition -- Chapter - 5
# This program is optimized for Python 2.7.12 and Python 3.5.2.
# It may run on any other version with/without modifications.
import argparse
import os
import getpass
import re
import sys
import smtplib
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 587
def send_email(sender, recipient):
""" Send email message """
msg = MIMEMultipart()
msg['Subject'] = 'Python Emaill Test'
msg['To'] = recipient
msg['From'] = sender
subject = 'Python email Test'
message = 'Images attached.'
# attach imgae files
files = os.listdir(os.getcwd())
gifsearch = re.compile(".gif", re.IGNORECASE)
files = filter(gifsearch.search, files)
for filename in files:
path = os.path.join(os.getcwd(), filename)
if not os.path.isfile(path):
continue
img = MIMEImage(open(path, 'rb').read(), _subtype="gif")
img.add_header('Content-Disposition', 'attachment', filename=filename)
msg.attach(img)
part = MIMEText('text', "plain")
part.set_payload(message)
msg.attach(part)
# create smtp session
session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
session.ehlo()
session.starttls()
session.ehlo
password = getpass.getpass(prompt="Enter your Google password: ")
session.login(sender, password)
session.sendmail(sender, recipient, msg.as_string())
print ("Email sent.")
session.quit()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Email Sending Example')
parser.add_argument('--sender', action="store", dest="sender")
parser.add_argument('--recipient', action="store", dest="recipient")
given_args = parser.parse_args()
send_email(given_args.sender, given_args.recipient)