Turbomail Integration with Pyramid

邮差的信 提交于 2019-12-07 06:43:08

问题


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?


回答1:


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)



回答2:


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)


来源:https://stackoverflow.com/questions/6258850/turbomail-integration-with-pyramid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!