web2py includes functionalities of sending e-mail and SMS to the user. It uses libraries to send emails and sms.
The in-built class namely gluon.tools.Mail class is used to send email in web2py framework. The mailer can be defined with this class.
from gluon.tools import Mail mail = Mail() mail.settings.server = 'smtp.example.com:25' mail.settings.sender = 'abc@example.com' mail.settings.login = 'username:password'
The sender email as mentioned in the above example along with the password will be authenticated each time when an email is sent.
If the user needs to experiment or use for some debugging purpose, this can be achieved using the following code.
mail.settings.server = 'logging'
Now, all the emails will not be sent but it will be logged in the console.
Once we have set the configuration settings for an email using mail object, an email can be sent to many users.
The complete syntax of mail.send() is as follows −
send( to, subject = 'Abc', message = 'None', attachments = [], cc = [], bcc = [], reply_to = [], sender = None, encoding = 'utf-8', raw = True, headers = {} )
The implementation of mail.send() is given below.
mail.send( to = ['sender@example.com'], subject = 'hello', reply_to = 'abc@example.com', message = 'Hello ! How are you?' )
Mail returns a Boolean expression based on the response of the mailing server, that the mail is received by the end user. It returns True if it succeeds in sending an email to the user.
The attributes to, cc and bcc includes the list of valid email addresses for which the mail is intended to be sent.
The implementation for sending SMS messages differs from sending emails in web2py framework as it will require third party service that can relay the messages to the receiver. The third party service is not a free service and will obviously differ based on geographical region (from country to country).
web2py uses a module to help sending SMS with the following process −
from gluon.contrib.sms_utils import SMSCODES, sms_email email = sms_email('1 (111) 111-1111','T-Mobile USA (abc)') mail.send(to = email, subject = 'test', message = 'test')
In the above example, SMSCODES is the dictionary maintained by web2py that maps the names of the major phone companies to the email address postfix.
Telephone companies usually treat emails originating from third party services as spam. A better method is that the phone companies themselves relay the SMS. Every phone company includes a unique email address for every mobile number in its storage and the SMS can be sent directly to the email.
In the above example,
The sms_email function takes a phone number (as a string), which returns the email address of the phone.
The scaffolding app includes several files. One of them is models/db.py, which imports four.
Classes from gluon.tools include mail libraries as well and defines the various global objects.
The scaffolding application also defines tables required by the auth object, such as db.auth_user. The default scaffolding application is designed to minimize the number of files, not to be modular. In particular, the model file, db.py, contains the configuration, which in a production environment, is best kept in separate files.
Here, we suggest creating a configuration file −
from gluon.storage import Storage settings = Storage() settings.production = False if settings.production: settings.db_uri = 'sqlite://production.sqlite' settings.migrate = False else: settings.db_uri = 'sqlite://development.sqlite' settings.migrate = True settings.title = request. settings.subtitle = 'write something here' settings.author = 'you' settings.author_email = 'you@example.come' settings.keywords = '' settings.description = '' settings.layout_theme = 'Default' settings.security_key = 'a098c897-724b-4e05-b2d8-8ee993385ae6' settings.email_server = 'localhost' settings.email_sender = 'you@example.com' settings.email_login = '' settings.login_method = 'local' settings.login_config = ''