-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew.py
More file actions
51 lines (40 loc) · 1.23 KB
/
new.py
File metadata and controls
51 lines (40 loc) · 1.23 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
#!/usr/bin/env python
import smtplib
import getpass
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText # Added
from email.mime.image import MIMEImage
import urllib
import lxml.html
email = "adityamalu1@gmail.com"
password = getpass.getpass()
recipient = 'maluaditya12@gmail.com'
subject = 'Hi.'
Body = 'Todays xkcd.'
attachment = 'bob.png'
url = 'http://xkcd.com'
king = urllib.urlopen(url).read()
tree = lxml.html.fromstring(king)
image_url = tree.cssselect('img')[1].get('src')
image_url = 'http:' + image_url
image = urllib.urlopen(image_url).read()
fd = open('bob.png','w')
fd.write(image)
fd.close()
msg = MIMEMultipart()
msg["To"] = recipient
msg["From"] = email
msg["Subject"] = subject
msgText = MIMEText('<b>%s</b><br><img src="cid:%s"><br>' % (Body, attachment), 'html')
msg.attach(msgText) # Added, and edited the previous line
fp = open(attachment, 'rb')
img = MIMEImage(fp.read())
fp.close()
img.add_header('Content-ID', '<{}>'.format(attachment))
msg.attach(img)
print msg.as_string()
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(email, password)
server.sendmail(email, recipient, msg.as_string())