开发者

Turbomail Integration with Pyramid

开发者 https://www.devze.com 2023-03-11 08:03 出处:网络
I am in need 开发者_如何学编程of a method to send an email from a Pyramid application. I know of pyramid_mailer, but it seems to have a fairly limited message class. I don\'t understand if it\'s possi

I am in need 开发者_如何学编程of a method to send an email from a Pyramid application. I know of pyramid_mailer, but it seems to have a fairly limited message class. I don't understand if it's possible to write the messages from pyramid_mailer using templates to generate the body of the email. Further, I haven't seen anything regarding whether rich-text is supported, or if it's just simple plain-text.

Previously, I was using Turbomail with the Pylons framework. Unfortunately there doesn't appear to be any adapters available for TurboMail for Pyramid. I know that TurboMail can be extended for additional frameworks, but have no idea where I would even start such a task. Has anyone written an adapter for Pyramid or can point me in the right direction of what would be required to do so?


I can't answer your Turbomail questions other than to say that I've heard it works fine with Pyramid.

Regarding pyramid_mailer, it's entirely possible to render your emails using the same subsystem that lets pyramid render all of your templates.

from pyramid.renderers import render

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

Also, the pyramid_mailer Message object is based on the lamson MailResponse object, which is stable and well-tested.

You can create a mail that consists of both a plain text body as well as html, by specifying either the body or html constructor parameters to the Message class.

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


you install turbomail

easy_install turbomail

create a file in your pyramid project (i put my in lib) with something like this:

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()

and then in your controller you just call this function like this:

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

#some code...

    body = "<html><head></head><body>Hello World</body></html>"
    author = "mymail@example.com"
    subject = "testing turbomail"
    to = "mysecondmail@example.com"
    send_mail(body, author, subject, to)
0

精彩评论

暂无评论...
验证码 换一张
取 消