forked from tdamdouni/Pythonista
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2012-11-14_221033-ImageMail.py
68 lines (60 loc) · 2.04 KB
/
2012-11-14_221033-ImageMail.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
61
62
63
64
65
66
67
68
from __future__ import print_function
# http://www.macstories.net/links/quickly-email-a-picture-on-ios-using-pythonista/
# http://6d1f0d2e5a9f9c27cec8-28b934f7b0292a7dfd0ff5946ebc82f1.r53.cf1.rackcdn.com/2012-11-14_221033-ImageMail.py
# Example for sending an email with an attached image using smtplib
#
# IMPORTANT: You need to enter your email login in the main() function.
# The example is prepared for GMail, but other providers
# should be possible by changing the mail server.
import smtplib
import sound
sound.load_effect('Bleep')
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email import encoders
import Image
import clipboard
import console
import keychain
from io import BytesIO
def get_attachment(img):
bytes = BytesIO()
img.save(bytes, format='JPEG')
msg = MIMEBase('image', 'jpeg')
msg.set_payload(bytes.getvalue())
encoders.encode_base64(msg)
msg.add_header('Content-Disposition', 'attachment',
filename='image.jpeg')
return msg
def main():
### CHANGE THESE VALUES:
to = console.input_alert('Send Email To', 'Enter an email address below')
subject = console.input_alert('Subject', 'Enter the subject of the email below')
gmail_pwd = keychain.get_password('Gmail','[email protected]')
gmail_user = '[email protected]'
#Load a sample image, modify as needed:
image = clipboard.get_image()
print('Connecting...')
smtpserver = smtplib.SMTP("smtp.gmail.com", 587)
console.show_activity()
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_user, gmail_pwd)
print('Preparing message...')
outer = MIMEMultipart()
outer['Subject'] = subject
outer['To'] = to
outer['From'] = gmail_user
outer.preamble = 'You will not see this in a MIME-aware email reader.\n'
attachment = get_attachment(image)
outer.attach(attachment)
composed = outer.as_string()
print('Sending...')
smtpserver.sendmail(gmail_user, to, composed)
smtpserver.close()
console.hide_activity()
sound.play_effect('Bleep')
print('Done.')
if __name__ == '__main__':
main()