Last active
December 20, 2015 01:49
-
-
Save tnuki10/6052244 to your computer and use it in GitHub Desktop.
kindleへメールマガジンを転送するためのプログラムをGoogle App Engine for Pythonで作ってみた。
GAEでのメールの取り扱いは、こちらを参照。
https://developers.google.com/appengine/docs/python/mail/ Google Cloud Storageは、こちらを参照。
https://developers.google.com/appengine/docs/python/googlecloudstorageclient/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 以下のパスを追加 | |
# 受信用メールアドレスは、こちらを参照。 | |
# https://developers.google.com/appengine/docs/python/mail/receivingmail | |
url: /_ah/mail/受信用メールアドレス | |
script: handle_sendkindle.app | |
login: admin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from __future__ import unicode_literals | |
import logging | |
import webapp2 | |
import os | |
from google.appengine.api import mail | |
from google.appengine.ext.webapp.mail_handlers import InboundMailHandler | |
import cloudstorage as gcs | |
__author__ = 't_nuki' | |
#GAEでの開発環境判定 | |
debug = os.environ.get('SERVER_SOFTWARE', '').startswith('Dev') | |
class SendMailMagHandler(InboundMailHandler): | |
""" メールを受信し、kindleへ送信する。 | |
受信メールのBodyをtxtファイルに変換し、件名をファイル名にする。 | |
ファイルは、Cloud Storageにファイルを作成し、メールに添付送信後、ファイルを削除する。 | |
""" | |
my_default_retry_params = gcs.RetryParams(initial_delay=0.2, | |
max_delay=5.0, | |
backoff_factor=2, | |
max_retry_period=15) | |
def receive(self, mail_message): | |
logging.info("Received a message from: " + mail_message.sender) | |
plaintext_bodies = mail_message.bodies('text/plain') | |
decoded_text = '' | |
for content_type, body in plaintext_bodies: | |
decoded_text = body.decode() | |
# logging.info(decoded_text) | |
# cloud storageにtextファイルを保存 | |
# kindle送信用のファイル名を設定一応短くする。 | |
send_file_name = mail_message.subject + '.txt' | |
# cloud storage保存用のファイル名。最後に削除するので、適当に。 | |
write_file_name = 'mailmag.txt' | |
write_path_name = "/storage.nukipon.info/mailtemp/" + write_file_name | |
gcs_file = gcs.open(write_path_name, | |
'w', | |
content_type='text/plain', | |
retry_params=self.my_default_retry_params | |
) | |
gcs_file.write(decoded_text.encode('utf8')) | |
gcs_file.close() | |
# 保存したファイル読み込み | |
gcs_file_read = gcs.open(write_path_name, 'r') | |
# mailに添付して、xxxx@kindle.comに送信 | |
# senderは、amazon側にも登録が必要。 | |
# subjectとbodyはなくても良いが、自分に送られたメールを判別しやすくするために、適当に入れる。 | |
mail.send_mail(sender="GAEで管理者権限のあるメールアドレス、もしくは、APP-ID.appspotmail.comドメイン", | |
to="xxxx@kindle.com", | |
cc="自分のアドレス", # 一応、自分のアドレスにも送る | |
subject="sendkindle", | |
body="""appengineから。""", | |
attachments=[(send_file_name, gcs_file_read.read())]) | |
gcs_file_read.close() | |
#ファイルを削除する。 | |
gcs.delete(write_path_name) | |
app = webapp2.WSGIApplication([SendMailMagHandler.mapping()], debug=debug) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment