2011-06-06 220 views
5

我需要從金字塔應用程序發送電子郵件的方法。我知道pyramid_mailer,但它似乎有一個相當有限的消息類。我不明白是否可以使用模板編寫pyramid_mailer中的消息來生成電子郵件的正文。此外,我還沒有看到任何關於是否支持富文本的內容,或者它是否只是簡單的純文本。Turbomail與金字塔集成

以前,我在使用Turbomail與Pylons框架。不幸的是,似乎沒有適用於TurboMail for Pyramid的適配器。我知道TurboMail可以擴展爲額外的框架,但不知道我會在哪裏開始這樣的任務。有沒有人爲金字塔寫過一個適配器,或者可以指出我需要這樣做的正確方向?

+1

There現在是金字塔集成包稱爲http://pypi.python.org/pypi/pyramid_marrowmailer – iElectric 2012-12-07 17:33:35

回答

4

我不能回答你的Turbomail問題,而不是說我聽說它可以和Pyramid一起工作。

關於pyramid_mailer,完全可以使用相同的子系統呈現您的電子郵件,讓金字塔呈現您的所有模板。

from pyramid.renderers import render 

opts = {} # a dictionary of globals to send to your template 
body = render('email.mako', opts, request) 

此外,pyramid_mailer消息對象所基於的拉姆森MailResponse對象,它是穩定和良好測試上。

通過爲Message類指定bodyhtml的構造函數參數,您可以創建一個包含純文本正文和html的郵件。

plain_body = render('plain_email.mako', opts, request) 
html_body = render('html_email.mako', opts, request) 
msg = Message(body=plain_body, html=html_body) 
+0

謝謝這正是我正在尋找..乾杯 – h0st1le 2011-06-07 18:23:01

3

安裝turbomail

easy_install turbomail 

在金字塔的項目創建一個文件(我把我的LIB)像這樣的東西:

import turbomail 

    def send_mail(body, author,subject, to): 
    """ 
    parameters: 
    - body content 'body' 
    - author's email 'author' 
    - subject 'subject' 
    - recv email 'to' 

    """ 
    conf = { 
      'mail.on': True, 
      'mail.transport': 'smtp', 
      'mail.smtp.server': 'MAIL-SERVER:25', 
     } 

    turbomail.interface.start(conf) 
    message = turbomail.Message(
      author = author, 
      to = to, 
      subject = subject, 
      plain = 'This is HTML email', 
      rich = body, 
      encoding = "utf-8" 
     ) 

    message.send() 
    turbomail.interface.stop() 

,然後在你的控制器,你只是請撥打此功能:

#first import this function 
from myproject.lib.mymail import send_mail 

#some code... 

    body = "<html><head></head><body>Hello World</body></html>" 
    author = "[email protected]" 
    subject = "testing turbomail" 
    to = "[email protected]" 
    send_mail(body, author, subject, to)