Clipboard to Email – Python Code

Why

Very often I have to send lot of stuff (links, texts, comments, notes) to a particular Email Address. Initially I started using www.emailtheweb.com , it has a nice bookmark button (when you press the same, the webpage in the background is emailed). This was not the swiftest method, since it had multiple clicks, few pages to visit, accept button and unfortunately too long pre-processing time.

Then I started using Evernote. It is perfect, works like a charm, has an android app, chrome extension, standalone windows program, but unfortunately this one didn’t suit my exact need. My need was to have them in my email. You may ask, whats the difference, website repository is same as email, but not for me (since my workplace has strict policies of blocking note taking sites (don’t ask)).

Use

So, I built a small tool, a python code which automatically sends the content of clipboard into the email address you specify. Ofcourse it works by using one of your email as the host, but even if you use the same email as To/From, it still works.

Parts of the code is due to help from http://vreugdenhilresearch.nl/ and http://segfault.in

In minimal you only have to change line number 59-60 From-To Email address and 66 for password, i.e if you have a gmail account – From address only (To address need not be gmail). If you decide to use other email hosts, kindly update the line number 63 too (Google to find out the SMTP address and port for your email.

To use this, I copy the content (Ctrl+C), and press the shortcut (place the code in desktop, Right click->key in the keyboard shortcut (Ctrl+Alt+).

*Note you will need Python installed for this to work. (Works with both Python 2x and 3x versions)

Code

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
69
70
71
72
73
74
75
76
import sys
import smtplib
 
from email.mime.text import MIMEText
#Required for email modules
import ctypes
 
#Get required functions, strcpy..
strcpy = ctypes.cdll.msvcrt.strcpy
ocb = ctypes.windll.user32.OpenClipboard    #Basic Clipboard functions
ecb = ctypes.windll.user32.EmptyClipboard
gcd = ctypes.windll.user32.GetClipboardData
scd = ctypes.windll.user32.SetClipboardData
ccb = ctypes.windll.user32.CloseClipboard
ga = ctypes.windll.kernel32.GlobalAlloc    # Global Memory allocation
gl = ctypes.windll.kernel32.GlobalLock     # Global Memory Locking
gul = ctypes.windll.kernel32.GlobalUnlock
GMEM_DDESHARE = 0x2000
 
def Get( ):
  ocb(None) # Open Clip, Default task
 
  pcontents = gcd(1)
 
  data = ctypes.c_char_p(pcontents).value
 
  #gul(pcontents) ?
  ccb()
 
  return data
 
def Paste( data ):
  ocb(None) # Open Clip, Default task
 
  ecb()
 
  hCd = ga( GMEM_DDESHARE, len( bytes(data,"ascii") )+1 )
 
  pchData = gl(hCd)
 
  strcpy(ctypes.c_char_p(pchData),bytes(data,"ascii"))
 
  gul(hCd)
 
  scd(1,hCd)
 
  ccb()
 
clp = Get()
print(clp)
 
 
# Create a text/plain message
msg = MIMEText(str(clp))
msg['Subject'] = 'put the subject you require, I keep it fix to some obsure word'
#idea behind using obsure code is to help me arrange emails
#you could also use sys.argv for subject but I want to keep the process minimal & quick
 
msg['From']	= "YourEmailAddress"
msg['To'] 	= "RecipientAddress"
 
s = smtplib.SMTP('smtp.gmail.com', 587) 
# these values are for gmail, you may want to change it if others
s.ehlo()
s.starttls()
passwordGl = 'your email password here'
 
s.login(msg['From'], passwordGl)
try:
   # Python 3.2.1
   print(s.send_message(msg))
except AttributeError:
   # Python 2.7.2
   s.sendmail(msg['From'], [msg['To']], msg.as_string())
 
s.quit()
Written on August 25, 2012