Custom reset-password email message, with username- Plain text, not html

456 views
Skip to first unread message

Rob_McC

unread,
Jun 4, 2013, 11:01:28 PM6/4/13
to web...@googlegroups.com
Hey..

I want to customize the reset-password message, to in include the username,


I get this error:

cannot concatenate 'str' and 'Field' objects

What am I missing?



File: db.py


auth
.messages.reset_password = 'Hey ' + db.auth_user.username +  '  click on the link http://'+request.env.http_host+URL('default','user',args=['reset_password'])+'/%(key)s to reset your password'


Thanks
Rob


Massimo Di Pierro

unread,
Jun 4, 2013, 11:20:45 PM6/4/13
to web...@googlegroups.com
replace 

auth.messages.reset_password = 'Hey ' + db.auth_user.username +  '  click on the link ...'

with

auth.messages.reset_password = 'Hey %s  click on the link ...' % db.auth_user.username

Massimo Di Pierro

unread,
Jun 4, 2013, 11:21:01 PM6/4/13
to web...@googlegroups.com
Or replace

db.auth_user.username

with

db.auth_user.username.name


On Tuesday, 4 June 2013 22:01:28 UTC-5, Rob_McC wrote:

Rob_McC

unread,
Jun 5, 2013, 8:19:06 AM6/5/13
to web...@googlegroups.com
Thanks Massimo...

Something still not right...

I try simply this: in db.py

auth.messages.reset_password = 'Hey ' + db.auth_user.username +  '  click on the link ...'


and email inbox shows

Hey auth_user.username click on the link ...

Also..


Or replace

db.auth_user.username


with

db
.auth_user.username.name

Didn't work for me either.

Any tips would be appreciated.

I tried many times, and now gmail thinks my message is spam....

Rob




Anthony

unread,
Jun 5, 2013, 9:06:29 AM6/5/13
to web...@googlegroups.com
db.auth_user.username is a Field object in the db.auth_user table definition -- it does not contain the username of the currently logged in user. Instead, you want auth.user.username (auth.user contains the db.auth_user record of the currently logged in user).

Anthony

Rob_McC

unread,
Jun 5, 2013, 9:46:16 AM6/5/13
to web...@googlegroups.com

Thanks Anthony.

in db.py

This gives me an error

auth.messages.reset_password = 'Hey ' +   auth.user.username +   ' Click on the link http://' + request.env.http_host+URL('default','user',args=['reset_password'])+'/%(key)s to reset your password'  



Errror:
<type 'exceptions.AttributeError'>('NoneType' object has no attribute 'username')

and so does this:
auth.messages.reset_password = 'Hey %s  click on the link ...' % auth.user.username


---
Question:

Could the trouble be, if the user is NOT logged in, and they wouldn't be if they forgot their password...
but they did type in their email, which could give us their other information, like username, etc.


Ref:

Instead, you want auth.user.username (auth.user contains the db.auth_user record of the
currently logged in user).

I think I'm going to set up logging - logging.conf to keep on testing this, I read the 'logging.example.conf'  file.
 
mail
.settings.server = 'logging'

to keep testing this.

--
Thanks again.
Rob

 

Anthony

unread,
Jun 5, 2013, 9:52:27 AM6/5/13
to web...@googlegroups.com
You can't set it there -- if the user isn't logged in, auth.user is None. You have to set it at some point when you know the user is logged in (or set an alternative message when the user isn't logged in).

Anthony

Niphlod

unread,
Jun 5, 2013, 9:57:20 AM6/5/13
to web...@googlegroups.com
uhm. the user requesting the password is not logged-in. that message is passed on to the email_reset_password() function, that interpolates that message with just dict(key=reset_password_key, link=link) 

Now, the issue is that there's no way for the message to calculate any username cause it's not given ANY reference to the user's row.

Anthony

unread,
Jun 5, 2013, 10:57:33 AM6/5/13
to web...@googlegroups.com
Oops, good point -- wasn't paying attention.

Anthony

unread,
Jun 5, 2013, 11:37:01 AM6/5/13
to web...@googlegroups.com
Actually, the funny thing is that the user record from the db is in fact passed to the email_reset_password() function, so in theory one could include the username in the message, but as you noted, the only items that can be interpolated into the message are the key and the link. Here's a workaround:

def set_reset_message(form):
    user
= db.auth_user(email=form.vars.email)
    username
= user.username if user else ''
    auth
.messages.reset_password = 'Hey %s, click on the link %%(link)s to reset your password' % username

auth
.settings.reset_password_onvalidation = set_reset_message

That will change the message right after the reset form is submitted and validated, but before the email is sent.

Anthony


On Wednesday, June 5, 2013 9:57:20 AM UTC-4, Niphlod wrote:

Rob_McC

unread,
Jun 5, 2013, 11:00:03 PM6/5/13
to
Thanks a lot Anthony and Niphlod and Massimo...

    That was a big help - I'll give that a go.

I did customize the password reset message, and thought I would post it here if it would be helpful to anyone.
Any suggestions / optimizations are welcome.

The default settings email send an email like this:

- - - - - - - - - - - - - - - - - - -
Date:     Wed, 05 Jun 2013 11:26:04 +0000
From:     sup...@yoursite.com
To:     xx...@gmail.com
Subject:     Password reset

Click on the link http://yoursite.com/default/user/reset_password/1370431564-d3290509-814f-40ef-a04b-7f766d328c78 to reset your password
- - - - - - - - - - - - - - - - - - -

Here is the customization version.


From: sup...@yoursite.com
To: bsm...@gmail.com
Subject: [Yoursite.com] Your password reset

- - - - - - - - - - - - - - - - - - -
 Yoursite.com
 Your company's slogan.
- - - - - - - - - - - - - - - - - - -

PASSWORD RESET

A request has been submitted to recover a lost password. To reset your password, click on the link:
http://yoursite.com/1370475612-4b0a1881-43df-480d-8920-623a8825f7b4

If you did not specifically request this password change, please disregard this notice.

Thank you.

  ~Yoursite.com Team
   http://yoursite.com




I researched dozens of such emails from github, 37 signals etc.
and decided on this format. - plain text.

I know I could use HTML email...

If the string body starts with <html and ends with </html>, it will be sent as a HTML email.

but, I decided to go with plain text...
 
See Research:
http://kb.mailchimp.com/article/why-bother-with-plain-text-emails
http://www.blog.analyticsinspector.com/tag/plain-vs-html-email-ab-test/

Also, I got logging to work on my Mac, and that was very helpful for testing.
i.e.

mail.settings.server = 'logging'



in db.py

## -  START CUSTOMIZATION  FOR CUSTOM RESET Password EMAIL MESSAGES - - - - - - - - - - - - - - - - - - - - - - ##

# build the email message


# the new line for spacing
nl
='\n'

ln1
= '- - - - - - - - - - - - - - - - - - - \n'
ln2
= ' Yoursite.com'
ln3
= ' ' + T("Your company's logan.")

ln4
= T('PASSWORD RESET')
ln5
= T('A request has been submitted to recover a lost password. To reset your password, click on the link:')
ln6
= 'http://'+request.env.http_host+URL('default','user',args=['reset_password'])+'/%(key)s'

ln7
= T('If you did not specifically request this password change, please disregard this notice.')
ln8
= T('Thank you.')
ln9
='  ~' + T('Yoursite.com Team')

ln10
= '   http://yoursite.com '

# concatenate   the email message
msgpass
= ln1 + ln2 + nl + ln3 + nl + ln1 + nl + ln4 + nl*2 + ln5 + nl + ln6 + nl*2 + ln7 +  nl*2 + ln8 + nl*2 + ln9 + nl + ln10


# first, change the subject from the default
auth
.messages.reset_password_subject = T('[yoursite.com] Your password reset')

# concatenate the message from variable
auth
.messages.reset_password = msgpass


'''
This will yield

From: sup...@yoursite.com
To: bsm...@gmail.com
Subject: [Yoursite.com] Your password reset

- - - - - - - - - - - - - - - - - - -
 Yoursite.com
 Your company'
s slogan.
- - - - - - - - - - - - - - - - - - -

PASSWORD RESET

A request has been submitted to recover a lost password
. To reset your password, click on the link:
http
://yoursite.com/1370475612-4b0a1881-43df-480d-8920-623a8825f7b4

If you did not specifically request this password change, please disregard this notice.

Thank you.

 
~Yoursite.com Team
   http
://yoursite.com

       
'''        

## -  END CUSTOMIZATION  FOR CUSTOM RESET EMAIL MESSAGES - - - - - - - - - - - - - - - - - - - - - - ##


Reply all
Reply to author
Forward
0 new messages