Google App Engineでもうちょっとがんばってみる:メール送信プログラム

白石俊平
2008/04/11 18: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
記事の感想やご意見をコメントでお寄せください(CNET_IDログインが必要です)
ログイン パスワードを忘れた方  |  新規登録
  • 新着記事
  • 人気記事
  • 特集
  • ブログ
  • 読者投票
    いよいよ7月11日に売り出される「iPhone 3G」。今のところの購入予定は?

    投票受付期間:2008年7月7日 〜 2008年7月11日
  • » 投票しないで結果だけ見る