Setting up email
Web2py provides the gluon.tools.Mail
class to make it easy to send emails using web2py. One can define a mailer with
from gluon.tools import Mail
mail = Mail()
mail.settings.server = 'smtp.example.com:25'
mail.settings.sender = 'you@example.com'
mail.settings.login = 'username:password'
Note, if your application uses Auth
(discussed in the next chapter), the auth
object will include its own mailer in auth.settings.mailer
, so you can use that instead as follows:
mail = auth.settings.mailer
mail.settings.server = 'smtp.example.com:25'
mail.settings.sender = 'you@example.com'
mail.settings.login = 'username:password'
You need to replace the mail.settings with the proper parameters for your SMTP server. Set mail.settings.login = None
if the SMTP server does not require authentication. If you don’t want to use TLS, set mail.settings.tls = False
For debugging purposes you can set
mail.settings.server = 'logging'
and emails will not be sent but logged to the console instead.
Configuring email for Google App Engine
For sending emails from Google App Engine account:
mail.settings.server = 'gae'
At the time of writing web2py does not support attachments and encrypted emails on Google App Engine. Notice cron and scheduler do not work on GAE.
x509 and PGP Encryption
It is possible to send x509 (SMIME) encrypted emails using the following settings:
mail.settings.cipher_type = 'x509'
mail.settings.sign = True
mail.settings.sign_passphrase = 'your passphrase'
mail.settings.encrypt = True
mail.settings.x509_sign_keyfile = 'filename.key'
mail.settings.x509_sign_certfile = 'filename.cert'
mail.settings.x509_crypt_certfiles = 'filename.cert'
It is possible to send PGP encrypted emails. First of all you need to install the python-pyme package. Then you can use GnuPG (GPG) to create the key-files for the sender (take the email-address from mail.settings.sender) and put the files pubring.gpg and secring.gpg in a directory (e.g. “/home/www-data/.gnupg”).
Use the following settings:
mail.settings.gpg_home = '/home/www-data/.gnupg/'
mail.settings.cipher_type = 'gpg'
mail.settings.sign = True
mail.settings.sign_passphrase = 'your passphrase'
mail.settings.encrypt = True