Architecture question: can I use Web2py template engine to generate emails?

254 views
Skip to first unread message

Carl

unread,
May 27, 2009, 5:28:49 AM5/27/09
to web2py Web Framework
web2py's templating for HTML pages makes managing page structure
populated with dynamic content very straightforward and scalable.

What approach is recommended to use this power to manage emails/email
templates?

My application sends out emails populated with a lot of dynamic data
and before I compose a String for the body text in Python I wondered
if the existing template engine could be harnessed (and if so, what's
the recommended way to leverage it)

mdipierro

unread,
May 27, 2009, 7:16:07 AM5/27/09
to web2py Web Framework
You can use the web2py template language to generate emails.

from gluon.template import parse_template
from gluon.tool import Mail

mail=Mail
mail.settings.server='smtp.gmail.com:
587'
mail.settings.sender='y...@somewhere.com'
mail.settings.login=None or
'username:password'

path=os.path.join(request.folder,"views")
context=dict(a=1,b=2,c=3,etc="etc")
message=parse_template('file.html',path=path,context=context)
mail.send(to=['y...@whatever.com'],subject='None',message=message)

Massimo

Carl

unread,
May 27, 2009, 7:44:02 AM5/27/09
to web2py Web Framework
thanks M; knew there's be a concise way to leverage the existing
functionality.

On May 27, 12:16 pm, mdipierro <mdipie...@cs.depaul.edu> wrote:
> You can use the web2py template language to generate emails.
>
> from gluon.template import parse_template
> from gluon.tool import Mail
>
> mail=Mail
> mail.settings.server='smtp.gmail.com:
> 587'
> mail.settings.sender='...@somewhere.com'
> mail.settings.login=None or
> 'username:password'
>
> path=os.path.join(request.folder,"views")
> context=dict(a=1,b=2,c=3,etc="etc")
> message=parse_template('file.html',path=path,context=context)
> mail.send(to=['...@whatever.com'],subject='None',message=message)

annet....@gmail.com

unread,
May 27, 2009, 12:05:23 PM5/27/09
to web2py Web Framework
In one of my applications I have an e-mail form, which for a lack of
knowledge is sent to my mail box using the following code in the
function:


mail.send(to=['annet.ve..@gmail.com'], subject=form.vars.onderwerp,
message=form.vars)


In db.py I have got:

from gluon.tools import Mail

mail=Mail()
mail.settings.server='smtp.gmail.com:587'
mail.settings.sender='annet.ve..@gmail.com'
mail.settings.login='annet.ve..@gmail.com:**********'


When I want to use a template to layout the mail, where do these lines
of code go:

from gluon.template import parse_template

path=os.path.join(request.folder,"views")
context=dict(a=1,b=2,c=3,etc="etc")
message=parse_template('file.html',path=path,context=context)
mail.send(to=['...@whatever.com'],subject='None',message=message)


... and what is the meaning of context=dict(...)


Kind regards,

Annet

mdipierro

unread,
May 27, 2009, 12:28:11 PM5/27/09
to web2py Web Framework
It depends on who sends the email. If an action triggered by a web
request does, then the code goes in the action. If you want a
backrgound script to send the emails then you put the code in
script.py and run it with

web2py.py -S yourapp -M -R /path/to/your/script.py

the context is dictionary with the variable you want to be visible.
For example

context=dict(name="Annet")
message=parse_template('email.txt',path=path,context=context)


--- email.txt ----
Dear {{=name}},
how are you today?
--- end -----


On May 27, 11:05 am, annet.verm...@gmail.com wrote:
> In one of my applications I have an e-mail form, which for a lack of
> knowledge is sent to my mail box using the following code in the
> function:
>
> mail.send(to=['annet.v...@gmail.com'], subject=form.vars.onderwerp,
> message=form.vars)
>
> In db.py I have got:
>
> from gluon.tools import Mail
>
> mail=Mail()
> mail.settings.server='smtp.gmail.com:587'
> mail.settings.sender='annet.v...@gmail.com'
> mail.settings.login='annet.v...@gmail.com:**********'

Carl

unread,
May 27, 2009, 1:13:52 PM5/27/09
to web2py Web Framework
I've implemented the code but am one step away from a working
solution.

The variable message contains text "response.write( ... escape=False)"
for each section of text and for each dictionary item I use in my
view.

any thoughts?

C

On May 27, 12:16 pm, mdipierro <mdipie...@cs.depaul.edu> wrote:
> You can use the web2py template language to generate emails.
>
> from gluon.template import parse_template
> from gluon.tool import Mail
>
> mail=Mail
> mail.settings.server='smtp.gmail.com:
> 587'
> mail.settings.sender='...@somewhere.com'
> mail.settings.login=None or
> 'username:password'
>
> path=os.path.join(request.folder,"views")
> context=dict(a=1,b=2,c=3,etc="etc")
> message=parse_template('file.html',path=path,context=context)
> mail.send(to=['...@whatever.com'],subject='None',message=message)

mdipierro

unread,
May 27, 2009, 2:16:58 PM5/27/09
to web2py Web Framework
?

why response.write( ... escape=False) ?

Carl

unread,
May 27, 2009, 6:15:25 PM5/27/09
to web2py Web Framework
it's got me puzzled too.

the text I have in my view is used but it's prefixed response.write(
and carriage returns are converted to \r\n

a dict in my view is displayed in emails as
response.write(absurl)

C

mdipierro

unread,
May 27, 2009, 7:42:20 PM5/27/09
to web2py Web Framework
can you post an example? I am confused.

annet....@gmail.com

unread,
May 28, 2009, 2:40:48 AM5/28/09
to web2py Web Framework
Massimo,

Thanks for your explanation.


Annet.

Carl

unread,
May 28, 2009, 6:17:04 AM5/28/09
to web2py Web Framework
thanks, that's very good of you.

I've implemented a zero-fat implementation to reproduce the behaviour
and remove my app's details...

In controllers\default.py I have:

def example():
path=os.path.join(request.folder, 'views')
context= dict(one="1", two="2")
message= parse_template('default/
example.html',path=path,context=context)
print message
return True

In views\example.html I have 7 lines:
An example view for emails.

One = {{=one}}

Two = {{two}}

The end.

so I call app/example
and in my console I see this (from the print message statement):
response.write('An example view for emails.\r\n\r\nOne =
',escape=False)
response.write(one)
response.write('\r\n\r\nTwo = ',escape=False)
two
response.write('\r\n\r\nThe end.',escape=False)

I'm running on WinXP but I don't think that's relevant here.

Hopefully you can either see the issue in my code or at least
reproduce the behaviour using the code supplied.

Carl

Carl

unread,
May 28, 2009, 6:18:09 AM5/28/09
to web2py Web Framework
small typo: I'm calling app/default/example rather than app/example

mdipierro

unread,
May 28, 2009, 9:42:47 AM5/28/09
to web2py Web Framework
I see the problem. There is something missing in my example. I will
fix this and resend it tomorrow.

Massimo

Carl

unread,
May 28, 2009, 9:47:41 AM5/28/09
to web2py Web Framework
thanks for your time Massimo.

mdipierro

unread,
May 28, 2009, 10:22:13 AM5/28/09
to web2py Web Framework
Please try this:

def example():
context= dict(one="1", two="2")
message= response.render('default/example.html',context=context)
print message
return True

Carl

unread,
May 28, 2009, 2:21:14 PM5/28/09
to web2py Web Framework
nearly there.

I get this error:
File "G:\My Documents\Carl\projects\workspace\project\applications
\NineBirthdays/views/default/example.html", line 2, in <module>
NameError: name 'one' is not defined

Carl

mdipierro

unread,
May 28, 2009, 3:08:23 PM5/28/09
to web2py Web Framework
hmmm.. will look intot his tonight.

mdipierro

unread,
May 29, 2009, 12:40:48 PM5/29/09
to web2py Web Framework
Please use

message= response.render('default/example.html',context)

or

message= response.render('default/example.html',one="1", two="2")

Massimo

Carl

unread,
May 29, 2009, 1:26:03 PM5/29/09
to web2py Web Framework
super!
Always satisfying when a solution requires less code (and nice to get
it working on a Friday too)

Would you mind explaining the reasoning behind the final solution and
the previous suggestion?
I'm learning both Python and Web2py and it'd help yell this in my head

have a good weekend

C

mdipierro

unread,
May 29, 2009, 1:47:38 PM5/29/09
to web2py Web Framework
becaue

message= response.render('default/example.html',context=dict(one="1",
two="2")) would have passed

dict(context=dict(one="1", two="2")))

and {{=one}} would have had to be {{=context['one']}}.

easier to change the argument passing the the view.

Carl

unread,
Jun 1, 2009, 10:00:59 AM6/1/09
to web2py Web Framework
thanks M.

Greg

unread,
Jun 23, 2009, 12:14:34 PM6/23/09
to web2py Web Framework
I'm trying to do this from the web2py shell, and I get the following
error:

>>> response.render('default/index.html2',context=dict(message="hello"))
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/gwarner/web2py/gluon/globals.py", line 128, in render
run_view_in(self._view_environment)
File "/home/gwarner/web2py/gluon/compileapp.py", line 338, in
run_view_in
restricted(ccode, environment, layer)
File "/home/gwarner/web2py/gluon/restricted.py", line 115, in
restricted
raise RestrictedError(layer, code, '', environment)
RestrictedError: <gluon.restricted.RestrictedError instance at
0x7f632b8c>

As you can see, I'm trying to use the template "index.html2", which is
the same as the default index.html, except I've moved the {{=message}}
outside of the try statement.

Is it possible to do what I'm trying to do from the shell?
> > > > > > > > > > from gluon.templateimport parse_template
> > > > > > > > > > from gluon.tool import Mail
>
> > > > > > > > > > mail=Mail
> > > > > > > > > > mail.settings.server='smtp.gmail.com:
> > > > > > > > > > 587'
> > > > > > > > > > mail.settings.sender='...@somewhere.com'
> > > > > > > > > > mail.settings.login=None or
> > > > > > > > > > 'username:password'
>
> > > > > > > > > > path=os.path.join(request.folder,"views")
> > > > > > > > > > context=dict(a=1,b=2,c=3,etc="etc")
> > > > > > > > > > message=parse_template('file.html',path=path,context=context)
> > > > > > > > > > mail.send(to=['...@whatever.com'],subject='None',message=message)
>
> > > > > > > > > > Massimo
>
> > > > > > > > > > On May 27, 4:28 am, Carl <carl.ro...@gmail.com> wrote:
>
> > > > > > > > > > > web2py's templating for HTML pages makes managing page structure
> > > > > > > > > > > populated with dynamic content very straightforward and scalable.
>
> > > > > > > > > > > What approach is recommended to use this power to manage emails/email
> > > > > > > > > > > templates?
>
> > > > > > > > > > > My application sends out emails populated with a lot of dynamic data
> > > > > > > > > > > and before I compose a String for the body text in Python I wondered
> > > > > > > > > > > if the existingtemplateengine could be harnessed (and if so, what's

mdipierro

unread,
Jun 23, 2009, 12:41:33 PM6/23/09
to web2py Web Framework
My mistake. Should have been

>>> response.render('default/index.html2',dict(message="hello"))

or

>>> response.render('default/index.html2',message="hello")

No "context=dict(...)". read more here

http://www.web2py.com/AlterEgo/default/show/225

Peter O

unread,
Jan 2, 2012, 10:07:03 AM1/2/12
to web...@googlegroups.com
I am glad that I found this answer, after printing all the intermediary variables for a couple of hours.

Is this considered a Gotcha, or better be documented if it is the design? :)

Reply all
Reply to author
Forward
0 new messages