テンプレートシステムを用いたEメール生成
□未翻訳
□翻訳中
■翻訳完了(細田謙二)
■レビュー(Omi Chiba)
テンプレートシステムを用いたEメール生成
Eメールを生成するためにテンプレートシステムを利用することは可能です。たとえば、次のようなデータベーステーブルを考えます。
It is possible to use the template system to generate emails. For example, consider the database table
このとき、データベースのすべてpersonに、ビューファイル"message.html"に保存されている次のようなメッセージを送りたいとします:
where you want to send to every person in the database the following message, stored in a view file "message.html":
1.
2.
Dear {{=person.name}},
You have won the second prize, a set of steak knives.
これは次のような方法で実現できます
You can achieve this in the following way
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
>>> from gluon.tool import Mail
>>> mail = Mail(globals())
>>> mail.settings.server = 'smtp.gmail.com:587'
>>> mail.settings.sender = '...@somewhere.com'
>>> mail.settings.login = None or 'username:password'
>>> for person in db(db.person.id>0).select():
>>> context = dict(person=person)
>>> message = response.render('message.html', context)
>>> mail.send(to=['who@example.com'],
>>> subject='None',
>>> message=message)
この作業のほとんどは、次の文で行われます
Most of the work is done in the statement
これは、ビューの"message.html"を辞書の"context"で定義された変数とともにレンダリングします。そして、レンダリングされたEメールのテキストからなる文字列を返します。contextはテンプレートファイルで利用可能な変数が含まれている辞書です。
It renders the view "message.html" with the variables defined in the dictionary "context", and it returns a string with the rendered email text. The context is a dictionary that contains variables that will be visible to the template file.
メッセージが <html>で始まり、</html>で終わる場合、EメールはHTMLのEメールになります。
If the message starts with <html> and ends with </html> the email will be an HTML email.
Eメールを生成に使用したものと同じ仕組は、SMSやテンプレートに基づいた他のタイプのメッセージを生成するために使用することも可能です。
The same mechanism that is used to generate email text can also be used to generate SMS or any other type of message based on a template.