-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_a_kitten
executable file
·56 lines (51 loc) · 1.71 KB
/
send_a_kitten
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
#!/usr/bin/env python
import sys
import smtplib
import argparse
from subprocess import call
from urllib import urlretrieve
from email.MIMEText import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
def main():
parser = argparse.ArgumentParser(description="Let's send a kitten!")
parser.add_argument('to',
help='The email address to send the kitten to',
type=str)
parser.add_argument('sender',
help='The email address to send the kitten from',
type=str)
parser.add_argument('-n',
'--name',
help='The name of the sender',
type=str)
parser.add_argument('-m',
'--message',
help='A nice message to send to the recipient',
type=str)
args = parser.parse_args()
msg = MIMEMultipart()
msg['From'] = args.sender
msg['To'] = args.to
if args.name:
sender_name = args.name
else:
sender_name = 'Someone'
msg['Subject'] = "%s send you a kitten from Send-a-Kitten!"\
% sender_name
url = 'http://thecatapi.com/api/images/get?format=src'
response = urlretrieve(url, 'kitten.jpeg')
fp = open(response[0], 'rb')
img = MIMEImage(fp.read())
if args.message:
msg.attach(MIMEText(args.message + '\n', 'plain'))
msg.attach(img)
fp.close()
server = 'localhost'
s = smtplib.SMTP(server)
response = s.sendmail(args.sender, args.to, msg.as_string())
s.quit()
call(['rm', '-rf', 'kitten.jpeg'])
if __name__ == '__main__':
main()
sys.exit(0)