çµç·¯
ãã¾ããã ãããã¼ã«ã«ã«ããã¦ãã¡ã¼ã«ãGmailã«ã¢ãããã¼ããã¦ã¿ããæåã¯Thunderbirdã§IMAP4ã®è¨å®ããã¦ãThunderbirdã§é¸æï¼ç§»åã§ã¢ãããã¼ããã¦ããã®ã ãã(1)ã¨ãã©ãConnectionãåããã(2)æã
Gmailãã¨ã©ã¼ãè¿ããããã«ç§»åãæ¢ã¾ã£ã¦ãã¾ããããããè¤æ°é¸æããç¶æ
ã§ç§»åã«å¤±æããã¨ãã¢ãããã¼ãã«æåããã¡ã¼ã«ã移åå
ã«æ®ã£ã¦ãã¾ããã©ã®ã¡ã¼ã«ãã¢ãããã¼ãããã®ãããããªããªã£ã¦ãã¾ãã
ãªã®ã§ãä¸åãããæ°åéã ããé¸æãã¦ããããã¢ãããã¼ããã¦ããã®ã ããä¸ã¤ç®ã®ãã£ã¬ã¯ããªãã¢ãããã¼ãããæç¹æ®ãã®ã¡ã¼ã«ã®éã«çµ¶æããã
ä¸èª¿ã¹
ã¨ãããããimapã¨mboxãæ±ãã©ã¤ãã©ãªãç¡ããã¨cheese shopãç©è²ãã¦ã¿ãããæ®ã©ç¡ããããããã°æ¨æºã©ã¤ãã©ãªã«ãã£ãæ°ãããã¨æãåºãã¦ãæ¨æºã©ã¤ãã©ãªã®æ³ã調ã¹ã¦ã¿ãã¨ãimaplib, mailbox ã¨ãã£ãã¢ã¸ã¥ã¼ã«ããã£ãããããPythonãããããªå梱ãç¹å¾´ã«æããã ãã®ãã¨ã¯ããã
Pythonã®ã¤ã³ã¿ã©ã¯ãã£ãã·ã§ã«ã§ãã°ãã試ãã¦ã¿ãæããããªãç°¡åã«ã¢ãããã¼ãã§ãããç°¡åã«ä½¿ãæ¹ã§è¨ãã¨ããããªæãã
>> mb = mailbox.mbox('mboxãã¡ã¤ã«å') >> gmail = imaplib.IMAP4_SSL('imap.gmail.com') >> gmail.login('ã¢ã«ã¦ã³ãå@gmail.com', 'ãã¹ã¯ã¼ã') >> for msg in mb: .. gmail.append('ãã©ã«ãå', [], None, str(msg)) ..
å®è·µ
試è¡é¯èª¤ããªãããä¸è¨ã®ãããªãã¨ãããªããã¢ãããã¼ãããã¹ã¯ãªãããçµãã§ã¿ãã
- éä¿¡ã«å¤±æããã¡ã¼ã«ã¯å¥ã¡ã¼ã«ããã¯ã¹ãä½ã£ã¦ããã«ä¿å
- imap.append()ã®ç¬¬3å¼æ°ã«ãã¡ãã»ã¼ã¸ã®Dateãã£ã¼ã«ãããæã£ã¦ããDateTimeãå ¥ãããï¼From - ã§å§ã¾ãè¡ã®æ¥ä»ãå ¥ããã»ããè¯ãã¨å¾ã§æ°ã¥ããããç¡è¦ï¼
- ã³ãã¯ã·ã§ã³ãåãããåæ¥ç¶
ã³ãã¯ã·ã§ã³åæ¥ç¶ããã¨ãã«ãã¡ãã»ã¼ã¸ãåéä¿¡ããªãã§å¤±æã«å ¥ãã¡ããã¨ãåé¡ãããã©ãèªåã®ã¡ã¼ã«ã§éä¿¡ã§ãããã®ã¯å ¨é¨éä¿¡ããã®ã§ããããããç¾æç¹ã§ã®ã½ã¼ã¹ã¯ãããªæãã
from time import sleep, mktime from email.utils import parsedate from email.header import decode_header from imaplib import IMAP4_SSL from mailbox import mbox import sys # user setting host = 'imap.gmail.com' user = 'username@gmail.com' passwd = 'password' folder = 'tagname' class Gmail: def __init__(self): self.conn = None def __prepare(self): if not self.conn: sleep(3) self.conn = IMAP4_SSL(host) self.conn.login(user, passwd) print "Connect" def create_mailbox(self, name): self.__prepare() try: return self.conn.create(name) except: self.conn = None return None def append(self, msg): datetime_tuple = parsedate(msg['Date']) if len(datetime_tuple) >= 9: dt = mktime(datetime_tuple) else: print >>sys.stderr, "Error - Date: " + msg['Date'] return None self.__prepare() try: return self.conn.append(folder, [], dt, str(msg)) except: self.conn = None return None def main(to_send, sent, fail): gmail = Gmail() gmail.create_mailbox(folder) mb_rest = mbox(to_send) mb_sent = mbox(sent) mb_fail = mbox(fail) try: for key, msg in mb_rest.iteritems(): if key not in mb_sent: res1, res2 = gmail.append(msg) if res1 == 'OK': print res2, msg['Date'], msg['Subject'] mb_sent.add(msg) else: mb_fail.add(msg) # What's wrong? print >>sys.stderr, res1, res2 else: print "No more messege in %s." % to_send except: mb_fail.add(msg) finally: mb_rest.close() mb_sent.close() mb_fail.close() if __name__ == '__main__': main(sys.argv[1], sys.argv[2], sys.argv[3])
æ°ã«ãªã£ãç¹
Pythonã®dictã§ã¯ãif key in dict: ã§keyãdictã®ãã¼ã«å«ã¾ãã¦ãããã©ãããçå½å¤ã§è¿ãã for key in dict: ã§dictããä¸ã¤ä¸ã¤ãã¼ãåãåºãã¦ã«ã¼ããããfor key, val in dict: ã«ãªã£ã¦ããªãã®ã¯ ifæã¨ã®å¯¾è±¡æ§ãåãããã§ãããkey, valã欲ãããªã for key, val in dict.items(): ã使ãã
ããã«å¯¾ãã¦ãmessageboxãªãã¸ã§ã¯ãã¯ãif key in mbox: for msg in mbox: ã¨ãªã£ã¦ãããdictã§ã¯ç¢ºä¿ããã¦ãã対象æ§ãä¿ããã¦ããªããããã¥ã¡ã³ããèªãã°ä¸å¿å¤ãããPythonã«æ
£ãã人㯠for key in msg: do_something(msg[key]) ã¨ããã£ã¡ããã®ã§ãããã¯dictã¨åãä»æ§ã«ããã¹ãã ã¨æãã