#!/usr/bin/env python import mimetypes import httplib import email import email.Header import urllib import time import base64 def get_parts(msgbody): """ Find out body,attached image, and return them. msgbody : mime message of email """ msg = email.message_from_string(msgbody) subj_l = email.Header.decode_header(msg.get('subject'))[0] enc = 'us-ascii' if subj_l[1]: enc = subj_l[1] subj = unicode(subj_l[0],enc).encode('utf-8') body = "" images = [] for part in msg.walk(): if part.get_content_maintype() == 'multipart': continue tp = part.get_content_type() if not body and tp.find('text') != -1: # body like part was found !! body = part continue if tp.find('image') != -1: # Image found! images.append(part) return body, subj,images def encode_multipart_formdata(fields, images): """ fields is a sequence of (name, value) elements for regular form fields. files is a sequence of (name, filename, value) elements for data to be uploaded as files Return (content_type, body) ready for httplib.HTTP instance """ BOUNDARY = '----------ThIs_Is_tHe_bouNdaRY_$' CRLF = '\r\n' L = [] for (key, value) in fields.iteritems(): L.append('--' + BOUNDARY) L.append('Content-Disposition: form-data; name="%s"' % key) L.append('') L.append(str(value)) for image in images: filename = image.get_filename() content_type = image.get_content_type() L.append('--' + BOUNDARY) L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (filename, filename)) L.append('Content-Type: %s' % content_type) L.append('') L.append(image.get_payload(decode=1)) L.append('--' + BOUNDARY + '--') L.append('') body = CRLF.join(L) content_type = 'multipart/form-data; boundary=%s' % BOUNDARY return content_type, body def get_content_type(filename): return mimetypes.guess_type(filename)[0] or 'application/octet-stream' def add_moblog_entry(msgbody, host, blog, auth, password): """ Add entry from mail msgbody : This argument will be mime message of one email. site : blog : auth : password : blogbase : This argument will be url for blog. Authentication informations(user/pass) must be included. """ body, subj, images = get_parts(msgbody) if not body: # no body was found return body_str = body.get_payload(decode=1) if body_str.find(password) != 0: return else: body_str = body_str.replace(password,'') while body_str[0] == '\n': body_str = body_str[1:] body_str = body_str.split('---')[0] body_str = unicode(body_str,'iso-2022-jp').encode('utf-8') fields = {'body': body_str, 'title': subj} content_type, body = encode_multipart_formdata(fields, images) h = httplib.HTTP(host) h.putrequest('POST', blog + "/add_moblog_entry") h.putheader('content-type', content_type) h.putheader('content-length', str(len(body))) authstr = 'Basic ' + base64.encodestring(auth).strip() h.putheader('Authorization', authstr) h.endheaders() h.send(body) errcode, errmsg, headers = h.getreply() def main(): # # Please set some information,(host of mail server,etc) to use # import poplib s = poplib.POP3('mailhost') s.user('pop3user') s.pass_('pop3pass') l = s.list() if len(l) and l[1]: m = s.retr(1) host = "example.com" blog = "/coreblog" auth = "user:pass" password = "moblog_password" msgbody = '\n'.join(m[1]) add_moblog_entry(msgbody, host, blog, auth, password) s.dele(1) s.quit() return if __name__ == '__main__': main()