Google App Engineでもうちょっとがんばってみる:メール送信プログラム
白石俊平(あゆた)
2008-04-11 18:00:00
Google App Engineの基礎を学んだところで、もうちょっと発展的なプログラムを組んでみよう。
リスト:builder-example2.py
# encoding: utf-8
import os
import logging
import wsgiref.handlers
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.api import mail, users
class MainPage(webapp.RequestHandler):
def get(self):
user = users.get_current_user()
if not user:
self.redirect(users.create_login_url("/"))
else:
# メール送信
nickname = user.nickname()
mailAddr = user.email()
logging.debug("%s<%s>にメールします", nickname, mailAddr)
mail.send_mail(sender="shumpei.shiraishi@gmail.com",
to="%s<%s>" % (nickname, mailAddr),
subject="こんにちは!%sさん" % nickname,
body="""
拝啓、%sさん
お世話になっております。builderです。
そしてさようなら。
""" % nickname)
# 画面表示
path = os.path.join(os.path.dirname(__file__), 'response.html')
self.response.out.write(
template.render(path, {'username':user.nickname()}))
def main():
application = webapp.WSGIApplication([('/', MainPage)],
debug=True)
wsgiref.handlers.CGIHandler().run(application)
if __name__ == "__main__":
main()
リスト:app.yaml
# アプリケーションの名称 application: builder-example2 # アプリケーションのバージョン version: 1 # ランタイムの名称。現時点では「python」のみ runtime: python # アプリケーションが前提としているAPIのバージョン api_version: 1 # URLと、その処理方法の定義 handlers: - url: /.* script: builder-example2.py
- ホワイトペーパー



