Re: auth.messages.verify_email html template

452 views
Skip to first unread message

Lisandro

unread,
Oct 25, 2013, 10:57:40 AM10/25/13
to web...@googlegroups.com
I'm having the same problem. 
I have a general email template wich receives some context variables. I want to use that same template for sending the "verify_email", but can't figure it out.
Any example? Thanks in advance.

El miércoles, 24 de abril de 2013 13:39:10 UTC-3, Cristoffer Fairweather escribió:
First off, thanks to the community for a well written framework. The documentation is very nice and full of examples.

I thought I'd ask here, as I couldn't be the only want who didn't want a text "verify email" to be sent out in lieu of nice, colorful, css heavy html emails.
I want do the following to set an email template for auth.messages.verify_email. 
message = 'Click on the link https://' +request.env.http_host +URL(r=request,c='default',f='user',args=['verify_email']) + '/%(key)s to verify your email'
context = dict(message=message)
auth.messages.verify_email  = response.render('emailtemplates/standard_email.html', context)

The problem is, because I have CSS in the template, I get the following error.
<type 'exceptions.ValueError'> unsupported format character ';' (0x3b) at index 316

Anyone else have this problem? I thought about modifying tool.py to use
self.messages.verify_email_context['key'] = key
message = response.render(self.message.verify_email_template,self.messages.verify_email_context) #and put {{=key}} in the template

instead of
message=self.messages.verify_email  % dict(key=key)




A similar question was asked here, but I didn't get any resolution from the answer, as I'm having issues.

Ykä Marjanen

unread,
Oct 27, 2013, 1:23:37 PM10/27/13
to web...@googlegroups.com
I have replaced the standard verification with my own. The standard authentication is pretty simple as it creates a unique key, which it stores in the database and sends as a link to the registrant. When the link is clicked it will match the registration details with the unique id and stores (accepts the user) them.

This way you can customize the template and use email API (json), which is much more powerful than smtp method (I use mailgun).

Ykä

Lisandro Rostagno

unread,
Oct 29, 2013, 5:46:51 PM10/29/13
to web...@googlegroups.com
Thanks for the tip! I thought it would be more difficult, but apparently nothing is too difficult with web2py ;)

For those interested in doing that, is just as simple as instantiating Auth class and overwriting wanted methods, for example, I overwrited "register" and "email_reset_password" methods in Auth class, that is, to send my custom emails on register and request reset password respectively.

Regards, Lisandro.


2013/10/27 Ykä Marjanen <yka.ma...@gmail.com>
I have replaced the standard verification with my own. The standard authentication is pretty simple as it creates a unique key, which it stores in the database and sends as a link to the registrant. When the link is clicked it will match the registration details with the unique id and stores (accepts the user) them.

This way you can customize the template and use email API (json), which is much more powerful than smtp method (I use mailgun).

Ykä

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/NbLduKZR_4k/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Jay Martin

unread,
Nov 22, 2014, 8:39:12 AM11/22/14
to web...@googlegroups.com
@Lisandro, would happen to have these code snippets handy to share? I'm interested in using the mailgun api too. Either way, thanks for checking!

My best,
Jay

On Tuesday, October 29, 2013 5:46:51 PM UTC-4, Lisandro wrote:
 ...

Lisandro

unread,
Nov 22, 2014, 8:56:06 AM11/22/14
to web...@googlegroups.com
Hi Jay Martin. Actually, I ended up coding my own functions instead of instantiating the Auth class. That is, I coded my own login, register, reset_password, etc functions.

For example, for the login, this is the code in the controller:

# This code should run when the users posts the form with all required data, and after doing some validation to the data provided
from gluon.tools import web2py_uuid
registration_key
= web2py_uuid()
db.auth_user.insert(\
    first_name=form.vars.first_name, \
    last_name=form.vars.last_name, \
    email=form.vars.email, \
    password=db.auth_user.password.validate(form.vars.password)[0], \
    registration_key=registration_key)
mail.send(to=form.vars.email, subject='Confirm your registration', \
  message='Hi %s, thanks for registering in our website!\n\n' %first_name + \
    'To finish the registration, please click on the next link:\n' + \
    '%s\n\n' %URL('default', 'user/verify_email/%s' %registration_key, host=request.env.http_host))


Then, in the default/user/verify_email I use this code:

registration_key = request.args(1)
user = db(db.auth_user.registration_key==
registration_key).select().first()
if not user:
    redirect(URL('default', 'index'))
user.update_record(registration_key='')
auth.login_user(user)
session.flash = 'Your registration has been confirmed. Thanks!'
redirect(URL('init', 'default', 'index'))


I hope it helps!

Jay Martin

unread,
Nov 22, 2014, 12:01:22 PM11/22/14
to web...@googlegroups.com
Extremely helpful! Thanks Lisandro. I'll be sure to share any tweaks...

On Saturday, November 22, 2014 8:56:06 AM UTC-5, Lisandro wrote:
...

I hope it helps!

Yi Liu

unread,
Feb 2, 2018, 8:40:01 PM2/2/18
to web2py-users
Thanks for sharing. I really hope web2py can officially update the native function to allow modern html email for verification. I understand the contributors have priorities.

Yi

Yi Liu

unread,
Feb 3, 2018, 8:38:12 PM2/3/18
to web...@googlegroups.com
For other people's reference, I ended up modifying gluon/tools.py. It is probably not a good practise. But it is the easiest solution.

I edit register() and email_reset_password()

modifying something like (depends on which one)

message=self.messages.verify_email  % d

to

message=('',response.render('email/template.html', d))

Be careful with the ending parentheses, the two functions need different number of them due to context codes.

it is important to use a 2-tuple or 2-list to pass the second item to html in the email, the empty '' will go to text. Otherwise, it will likely be send as plain text, especially if you have <!DOCTYPE as start in your template.

for email_reset_password(), you also need:

response = current.response

Then you need to delete tools.pyc and restart web2py to take effect.

Make sure you modify tools.py and delete tools.pyc every time you update web2py.

Hope this save some other people's time.

--Yi
Reply all
Reply to author
Forward
0 new messages