Yes, we are assuming an environment where you cannot access twitter even if you send an email to the outside or to any destination during work, although it is Sef Sef. Emails will be deleted automatically after tweeting. The purpose is to prevent duplicate postings with linked evidence destruction. Yes, you need to install it somewhere at home or rent it and run it regularly. http://symfoware.blog68.fc2.com/blog-entry-891.html We are indebted to you for getting the subject of Gmail. Or rather, it's a copy. I just removed the elements other than the subject acquisition as much as possible and added a posting function to delete the email. Thank you for your support.
gmail_twitter.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import imaplib, email, email.Header
import sys
from urllib import urlencode
#I have my own Oauth authentication token.
#If you don't set it aside, you're afraid of disclosure due to an accident.
import oat
"""
About Gmail acquisition
http://symfoware.blog68.fc2.com/blog-entry-891.html
We have received more.
"""
class imap4mail(object):
def __init__(self, data):
"""
Perform parsing of the mail data given in the constructor
"""
self.files = {}
#Parse message
msg = email.message_from_string(data)
#Title acquisition
self.title = self.decode(msg.get('Subject'))
def decode(self, dec_target):
"""
Email title, sender decoding
"""
decodefrag = email.Header.decode_header(dec_target)
title = ''
for frag, enc in decodefrag:
if enc:
title += unicode(frag, enc)
else:
title += unicode(frag)
return title
def analize_mail(mail):
#Post the subject of the retrieved email
oat.client.request('https://api.twitter.com/1.1/statuses/update.json', 'POST', urlencode({'status': mail.title.encode('utf-8')}))
if __name__ == "__main__":
host = 'imap.gmail.com'
user = '[email protected]'
password = 'password'
mailbox = 'INBOX'
#Mail server specification
M = imaplib.IMAP4_SSL(host=host)
#Login
M.login(user, password)
#Mailbox selection
M.select(mailbox)
typ, data = M.search(None, 'ALL')
for num in data[0].split():
typ, data = M.fetch(num, '(RFC822)')
mail = imap4mail(data[0][1])
#Post to twitter
analize_mail(mail)
#Delete email after posting
M.store(num, '+FLAGS', '\\Deleted')
M.expunge()
M.close()
M.logout()
Yes, comments etc. are almost as they are. If I change the comment, that's it.
Recommended Posts