librelist archives

« back to archive

Re: Re: [flask] Mail Flask extension

Re: Re: [flask] Mail Flask extension

From:
Date:
2010-05-30 @ 17:51
I'll be putting latest version of flask-mail up Bitbucket and PyPi on  
Monday/Tuesday as offline for most of the weekend.

The API uses a simple Message class for handling most of the functionality:

from flask import Flask
from flaskext.mail import Message, init_mail

app = Flask(__name__)
init_mail(app)

@app.route("/")
def index():
msg = Message("hello")
msg.add_recipient("to@example.com")
msg.body = "hello"
msg.send()

There is a DEFAULT_MAIL_SENDER configuration to set the default from  
address.

You can also suppress email sending for unit tests, which will also add  
messages to a g.outbox list:

assert g.outbox[0].subject == "hello"

Support for easy handling of attachments and HTML will be included.

On May 29, 2010 8:14am, Dan Jacob <danjac354@gmail.com> wrote:
> Another issue is being able to set logging configuration easily, as

> Lamson has its own logging setup.



> On 29 May 2010 08:07, Dan Jacob danjac354@gmail.com> wrote:

> > Yes, I was considering this point - although I think the reason for

> > the upper case for the attributes was because "To" and "From" are

> > Python keywords, and it was just to be consistent - "sender" and

> > "recipients" would work just as well.

> >

> > The main reason I'd like to wrap MailResponse however is so that you

> > can set a DEFAULT_MAIL_SENDER in your configuration, eg

> > "support@mysite.com". It would also be possible to add further

> > convenience methods eg add_recipient.

> >

> > "MailResponse" also makes sense in a Lamson context, but could be

> > confusing for people who don't know Lamson - "Message" works better.

> >

> > I'm also thinking about having the Relay instance wrapped in the

> > Message object. Therefore instead of this:

> >

> > msg = Message(....)

> > relay.deliver(msg)

> >

> > You can just do this:

> >

> > msg.send()

> >

> > However, if you want to use another Relay instance you can:

> >

> > relay = Relay(....)

> > msg.send(relay)

> >

> >

> > On 29 May 2010 07:59, Stephane Wirtel stephane@wirtel.be> wrote:

> >> Hi Dan,

> >>

> >> Thank you so much for your job.

> >>

> >> But I have a question, do you think it's possible to create a real  
> wrapper over Lamson, because the style of the API of Lamson (UpperCase  
> for the attributes) differs from the api of Flask.

> >>

> >> For example:

> >>

> >> def index():

> >> msg = MailResponse(subject='test', to=recipients, from=from_addr)

> >> msg.body = "Hello"

> >> relay.deliver(msg)

> >>

> >>

> >> I'm waiting for your opinion about this point.

> >>

> >> If you want some helps, I can help you.

> >>

> >> Regards,

> >>

> >> Stephane

> >>

> >> On 29 May 2010, at 01:23, Dan Jacob wrote:

> >>

> >>> Sorry, URL got mangled:

> >>>

> >>> http://bitbucket.org/danjac/flask-mail/

> >>>

> >>> On 29 May 2010 00:22, Dan Jacob danjac354@gmail.com> wrote:

> >>>> I've created an repository on bitbucket, flask-mail:

> >>>>

> >>>> http://bitbuckethttp://bitbucket.org/danjac/flask-mail/

> >>>>

> >>>> The flask-mail extension uses Lamson. It's quite rough at this point

> >>>> and requires documentation, but here's the basic idea:

> >>>>

> >>>> from flask import Flask

> >>>> from flaskext.mail import init_mail, relay, MailResponse

> >>>>

> >>>> MAIL_SERVER = '....'

> >>>> MAIL_USERNAME = '....'

> >>>> MAIL_PASSWORD = '...'

> >>>>

> >>>> app = Flask(__name__)

> >>>> app.config.from_object(__name__)

> >>>> init_mail(app) # relay now initialized, attached to app as mail_relay

> >>>>

> >>>> @app.route("/")

> >>>> def index():

> >>>> msg = MailResponse(Subject="test"

> >>>> To=recipients,

> >>>> From=from_addr)

> >>>> msg.Body = "test"

> >>>> msg.Html = "test"

> >>>>

> >>>> relay.deliver(msg)

> >>>>

> >>>> You can create additional Relay instances if you need to, but the

> >>>> relay object (a LocalProxy) is your default mail relay interface.

> >>>>

> >>>> The MailResponse class is quite versatile - you can easily add

> >>>> attachments for example - but it may require some wrapping, for

> >>>> example to be able to use a default from address set in the

> >>>> configuration.

> >>>> On 28 May 2010 17:47, Dan Jacob danjac354@gmail.com> wrote:

> >>>>> A 5 minute look at Lamson shows there are two classes at least that

> >>>>> might handle most of the work:

> >>>>>

> >>>>> server.Relay - handles the SMTP send stuff

> >>>>> mail.MailResponse - the actual email message

> >>>>>

> >>>>> MailResponse has functionality for encoding, multipart emails,

> >>>>> attachments etc, and we can use the Relay class for the low-level

> >>>>> sending operations.

> >>>>>

> >>>>> The question is how to design the API around these classes - they  
> are

> >>>>> pretty well designed so there is no reason not to be able to expose

> >>>>> them directly, once configured through Flask.

> >>>>>

> >>>>> Something like this (very rough code):

> >>>>>

> >>>>> app = Flask(__name__)

> >>>>> init_mail(app, Relay(....))

> >>>>>

> >>>>> you can then do:

> >>>>>

> >>>>> app.mail_relay.send(subj, to_, from_)

> >>>>> app.mail_relay.deliver(msg)

> >>>>>

> >>>>> The relay object could also be added to a LocalProxy.

> >>>>>

> >>>>> Anyway I'll look more closely at Lamson over the weekend and put

> >>>>> something up on Bitbucket at some point. In the meantime, any more

> >>>>> suggestions welcome.

> >>>>>

> >>>>> On 28 May 2010 17:29, Stephane Wirtel stephane@wirtel.be> wrote:

> >>>>>> ok, it's an example because in the case of OpenERP, we use the  
> BeautifulSoup library to convert the html to txt for the mailgateway.

> >>>>>>

> >>>>>> Regards,

> >>>>>>

> >>>>>> Stephane

> >>>>>> On 05/28/2010 06:24 PM, Dan Jacob wrote:

> >>>>>>> It would be better to manage the plain and html content  
> separately.

> >>>>>>>

> >>>>>>> On 28 May 2010 17:21, Stephane Wirtel stephane@wirtel.be> wrote:

> >>>>>>>> You can use a html2plain function to convert the html string to  
> a plain text ?

> >>>>>>>>

> >>>>>>>> """

> >>>>>>>> Title

> >>>>>>>> Hello,

> >>>>>>>>

> >>>>>>>> How are you ?

> >>>>>>>>

> >>>>>>>> http://example.com">Example.com

> >>>>>>>>

> >>>>>>>> """

> >>>>>>>>

> >>>>>>>> The result should be:

> >>>>>>>>

> >>>>>>>> """

> >>>>>>>> Title

> >>>>>>>> =====

> >>>>>>>>

> >>>>>>>> Hello,

> >>>>>>>>

> >>>>>>>> How are you ?

> >>>>>>>>

> >>>>>>>> Example.com [1]

> >>>>>>>>

> >>>>>>>> Links:

> >>>>>>>> [1] http://example.com

> >>>>>>>> """

> >>>>>>>>

> >>>>>>>> Regards,

> >>>>>>>>

> >>>>>>>> Stephane

> >>>>>>>>

> >>>>>>>>

> >>>>>>>> On 05/28/2010 06:09 PM, Dan Jacob wrote:

> >>>>>>>>> I'll have a look at Lamson (actually looking at it already, for  
> an

> >>>>>>>>> unrelated work project).

> >>>>>>>>>

> >>>>>>>>> The message object should allow for easy handling of HTML and  
> plain

> >>>>>>>>> text messages:

> >>>>>>>>>

> >>>>>>>>> message = Message(...)

> >>>>>>>>> message.plain = "hello"

> >>>>>>>>> message.html = "hello"

> >>>>>>>>> message.send()

> >>>>>>>>>

> >>>>>>>>> Attachments should be easy as well; something like:

> >>>>>>>>>

> >>>>>>>>> message.attach(fp, mimetype)

> >>>>>>>>>

> >>>>>>>>> Any thoughts on this ?

> >>>>>>>>>

> >>>>>>>>> On 28 May 2010 17:02, Armin Ronacher  
> armin.ronacher@active-4.com> wrote:

> >>>>>>>>>> Hi,

> >>>>>>>>>>

> >>>>>>>>>> On 5/28/10 5:57 PM, Dan Jacob wrote:

> >>>>>>>>>>> Lamson, as I understand it, is more of a complete framework -  
> perhaps

> >>>>>>>>>>> overkill if all you need is a few sendmail-type operations.

> >>>>>>>>>> In terms of complexity turbomail is probably more a framework  
> than

> >>>>>>>>>> lamson is.

> >>>>>>>>>>

> >>>>>>>>>>> If there are some functions available from lamson that can be  
> used

> >>>>>>>>>>> separately of that framework however that might be an option.

> >>>>>>>>>> As far as I know lamson has a utility module with all kinds of  
> lower

> >>>>>>>>>> level mail operations. And you can also use it to start an  
> SMTP server

> >>>>>>>>>> locally for development usage which rocks. So lamson >  
> turbomail for me.

> >>>>>>>>>>

> >>>>>>>>>>> OK. Probably the way to go is something from standard lib (ie  
> email

> >>>>>>>>>>> and smtplib) - the mail package in Django is a possible  
> starting point

> >>>>>>>>>>> in terms of what functionality to cover.

> >>>>>>>>>> I would try lamson first though. If that does not work good  
> enough,

> >>>>>>>>>> steal individual functions from there and base it on that +  
> smtplib.

> >>>>>>>>>>

> >>>>>>>>>>

> >>>>>>>>>> Regards,

> >>>>>>>>>> Armin

> >>>>>>>>>>

> >>>>>>>>

> >>>>>>>>

> >>>>>>

> >>>>>>

> >>>>>

> >>>>

> >>

> >>

> >