Skip to content

Instantly share code, notes, and snippets.

@rochacbruno
Created March 16, 2011 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rochacbruno/872690 to your computer and use it in GitHub Desktop.
Save rochacbruno/872690 to your computer and use it in GitHub Desktop.
db.py web2py com sobrecarga da classe Mail
# -*- coding: utf-8 -*-
# this file is released under public domain and you can use without limitations
#########################################################################
## This scaffolding model makes your app work on Google App Engine too
#########################################################################
if request.env.web2py_runtime_gae: # if running on Google App Engine
db = DAL('gae') # connect to Google BigTable
session.connect(request, response, db = db) # and store sessions and tickets there
### or use the following lines to store sessions in Memcache
# from gluon.contrib.memdb import MEMDB
# from google.appengine.api.memcache import Client
# session.connect(request, response, db = MEMDB(Client()))
else: # else use a normal relational database
db = DAL('sqlite://storage.sqlite') # if not, use SQLite or other DB
## if no need for session
# session.forget()
#########################################################################
## Here is sample code if you need for
## - email capabilities
## - authentication (registration, login, logout, ... )
## - authorization (role based authorization)
## - services (xml, csv, json, xmlrpc, jsonrpc, amf, rss)
## - crud actions
## (more options discussed in gluon/tools.py)
#########################################################################
from gluon.tools import *
########################################################################
# Sobrecarga do método send da classe Mail
########################################################################
class Mail(Mail):
def send(
self,
to,
subject='None',
message='None',
):
import smtplib
fromaddr = self.settings.sender
username = self.settings.login.split(':')[0]
password = self.settings.login.split(':')[1]
server = smtplib.SMTP(self.settings.server)
if self.settings.tls:
server.ehlo()
server.starttls()
server.ehlo()
header = 'To:' + to + '\n' + 'From: ' + fromaddr + '\n' + 'Subject: '+ subject +'\n'
fullmsg = header + '\n' + message
server.login(username,password)
server.sendmail(fromaddr,to, fullmsg)
server.quit()
########################################################################
# Fim da Sobrecarga do método send da classe Mail
########################################################################
mail = Mail() # mailer
auth = Auth(globals(),db) # authentication/authorization
crud = Crud(globals(),db) # for CRUD helpers using auth
service = Service(globals()) # for json, xml, jsonrpc, xmlrpc, amfrpc
plugins = PluginManager()
mail.settings.server = 'smtp.gmail.com:587' # your SMTP server
mail.settings.sender = 'xxxxxxx@gmail.com' # your email
mail.settings.login = 'usuario:senha' # your credentials or None
auth.settings.hmac_key = 'sha512:57f35c49-4c23-4a8f-ae8d-d1e7021493cf' # before define_tables()
auth.define_tables() # creates all needed tables
auth.settings.mailer = mail # for user email verification
auth.settings.registration_requires_verification = False
auth.settings.registration_requires_approval = False
auth.messages.verify_email = 'Click on the link http://'+request.env.http_host+URL(r=request,c='default',f='user',args=['verify_email'])+'/%(key)s to verify your email'
auth.settings.reset_password_requires_verification = True
auth.messages.reset_password = 'Click on the link http://'+request.env.http_host+URL(r=request,c='default',f='user',args=['reset_password'])+'/%(key)s to reset your password'
#########################################################################
## If you need to use OpenID, Facebook, MySpace, Twitter, Linkedin, etc.
## register with janrain.com, uncomment and customize following
# from gluon.contrib.login_methods.rpx_account import RPXAccount
# auth.settings.actions_disabled=['register','change_password','request_reset_password']
# auth.settings.login_form = RPXAccount(request, api_key='...',domain='...',
# url = "http://localhost:8000/%s/default/user/login" % request.application)
## other login methods are in gluon/contrib/login_methods
#########################################################################
crud.settings.auth = None # =auth to enforce authorization on crud
#########################################################################
## Define your tables below (or better in another model file) for example
##
## >>> db.define_table('mytable',Field('myfield','string'))
##
## Fields can be 'string','text','password','integer','double','boolean'
## 'date','time','datetime','blob','upload', 'reference TABLENAME'
## There is an implicit 'id integer autoincrement' field
## Consult manual for more options, validators, etc.
##
## More API examples for controllers:
##
## >>> db.mytable.insert(myfield='value')
## >>> rows=db(db.mytable.myfield=='value').select(db.mytable.ALL)
## >>> for row in rows: print row.id, row.myfield
#########################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment