Substituting an email address with a reference in a web2py emailing form

42 views
Skip to first unread message

mostwanted

unread,
Feb 22, 2020, 4:56:04 PM2/22/20
to web2py-users
I've created an emailing form in my application but have substituted where the email goes with a reference that contains the email, the problem is that now the email is not being sent unless i write a proper explicit email address. Whats the difference between writing an explicit email address & using a variable that supposedly contains the email? I have to use the referencing variable as I currently am because different registered users get to log in & send emails, its not just one user. How can I solve this problem???

CODE
def contact_us():
    details
=db.hotel_profile(request.args(0, cast=int))
    form
= SQLFORM.factory(
   
Field('name', requires=IS_NOT_EMPTY()),
   
Field('cell_number', requires=IS_NOT_EMPTY()),
   
Field('email', requires =[ IS_EMAIL(error_message='invalid email!'), IS_NOT_EMPTY() ]),
   
Field('subject', requires=IS_NOT_EMPTY()),
   
Field('message', requires=IS_NOT_EMPTY(), type='text')
   
)
   
if form.process().accepted:
        mail.send(to='details.email_address',
                 
#subject='contact request from %(your_name)s %(email)s'  % form.vars,
                  subject
=form.vars.subject,
                  message
= '%(message)s \nName: %(name)s \nEmail Address: %(email)s \nCell Number: %(cell_number)s' % form.vars)
        response
.flash = T('Thank you, your form was submitted')
       
#redirect(URL('index'))
   
return dict(form=form)


Regards;

Mostwanted

Scott Hunter

unread,
Feb 23, 2020, 7:59:04 AM2/23/20
to web2py-users
Did you try removing the quotes around the address?

- Scott

mostwanted

unread,
Feb 23, 2020, 8:13:07 AM2/23/20
to web2py-users
I did try  it without the quotes & it still gave me an error!

Scott Hunter

unread,
Feb 23, 2020, 1:03:04 PM2/23/20
to web2py-users
What error did it give you?  Are you sure `details.email_address` is defined?

mostwanted

unread,
Feb 23, 2020, 3:44:07 PM2/23/20
to web...@googlegroups.com
I am sure details.email_address is defined, I double checked.

I redefined my code & added the highlighted lines,  now everytime i send the email response flashes an error message. How can I get it to tell me the exact error??

def contact_us():
    details
=db.hotel_profile(request.args(0, cast=int))
    form
= SQLFORM.factory(
   
Field('name', requires=IS_NOT_EMPTY()),
   
Field('cell_number', requires=IS_NOT_EMPTY()),
   
Field('email', requires =[ IS_EMAIL(error_message='invalid email!'), IS_NOT_EMPTY() ]),
   
Field('subject', requires=IS_NOT_EMPTY()),
   
Field('message', requires=IS_NOT_EMPTY(), type='text')
   
)
   

    sent = False
   
if form.process().accepted:
       
sent = mail.send(to='details.email_address',
                 
#subject='contact request from %(your_name)s %(email)s'  % form.vars,

                  subject
=form.vars.subject,
                  message
= '%(message)s \nName: %(name)s \nEmail Address: %(email)s \nCell Number: %(cell_number)s' % form.vars)

    response.flash = T('Thank you, your form was submitted')
   

    if not sent:
        response.flash = 'Error sending message!!!'        

   
return dict(form=form)

Regards;

villas

unread,
Feb 28, 2020, 4:09:59 PM2/28/20
to web2py-users
If details.email_address is a var,  then it shouldn't be in quotes. To avoid any doubt, just use a str:  'myt...@validemail.com'

General approach: "first get it working,  then improve it".  Maybe this will help...

def contact_us():
    details
=db.hotel_profile(request.args(0, cast=int))
    form
= SQLFORM.factory(
       
Field('name', requires=IS_NOT_EMPTY()),
       
Field('cell_number', requires=IS_NOT_EMPTY()),
       
Field('email', requires =[ IS_EMAIL(error_message='invalid email!'), IS_NOT_EMPTY() ]),
       
Field('subject', requires=IS_NOT_EMPTY()),
       
Field('message', requires=IS_NOT_EMPTY(), type='text')
   
)
    if form.process().accepted:
        if mail.send(to = 'myt...@validemail.com',
                     subject = 'Simple Subject',
                     message = 'My test message!'):

            response.flash = T('Thank you, your form was submitted')
        else:
Reply all
Reply to author
Forward
0 new messages