fetchmailを使ってgmail上のメールを取得する方法
fetchmailを用いて、gmailにあるメールを自分のマシンに保管?的な事をするっていうメモ。まぁありきたりなのでググれば他にも事案は見つかりそうな気もしますが
でやるにしてもこっちでもSMTPサーバーが必要になる(必須じゃないかどうかに関しては不明)。とりあえずググってきて見つかったPythonを使ってSMTPサーバー的なのを提供するプログラムが以下
import smtpd
import asyncore
class CustomSMTPServer(smtpd.SMTPServer):
def process_message(self, peer, mailfrom, rcpttos, data):
print "Receiving message from:", peer
print "Message addressed from:", mailfrom
print "Message addressed to :", rcpttos
print "Message length :", len(data)
print data
return
server = CustomSMTPServer(("127.0.0.1", 25), None)
asyncore.loop()
であとは$HOME/.fetchmailrcを作る
set postmaster kinjouj
set nobouncemail
set syslog
# interval 60 seconds
set daemon 60
defaults
protocol pop3
# keepにするとサーバー上のメールが消えないんだはず
keep
no mimedecode
smtphost localhost
poll pop.gmail.com
user gmailのユーザー名(@gmail.comは除く)
pass パスワード
ssl
※作成後はパーミッションを0700にしておかないとエラーが出る
でfetchmailを起動しておくと
Receiving message from: ("127.0.0.1", 41213)
Message addressed from: メールアドレス
Message addressed to : [省略]
Message length : 796
--- ここからdata ---
MIME-Version: 1.0
Date: Sun, 5 Jan 2014 17:08:23 +0900
Subject: test
From: メールアドレス
To: メールアドレス
Content-Type: multipart/alternative; boundary=bcaec54863e8aefb3704ef34a5e1
--bcaec54863e8aefb3704ef34a5e1
Content-Type: text/plain; charset=UTF-8
test
--bcaec54863e8aefb3704ef34a5e1
Content-Type: text/html; charset=UTF-8
<div dir="ltr">test</div>
--bcaec54863e8aefb3704ef34a5e1--
的な感じで出力される(今回はSMTPサーバーが上記のPythonなやつで出力するだけなので)
んまぁそういう要件があれば使えるんじゃねってことで
ドキュメントの日本語訳: http://linuxjm.sourceforge.jp/html/fetchmail/man1/fetchmail.1.html