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

  1. from gluon.tools import Mail
  2. mail = Mail()
  3. mail.settings.server = 'smtp.example.com:25'
  4. mail.settings.sender = 'you@example.com'
  5. 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:

  1. mail = auth.settings.mailer
  2. mail.settings.server = 'smtp.example.com:25'
  3. mail.settings.sender = 'you@example.com'
  4. 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

  1. 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:

  1. 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:

  1. mail.settings.cipher_type = 'x509'
  2. mail.settings.sign = True
  3. mail.settings.sign_passphrase = 'your passphrase'
  4. mail.settings.encrypt = True
  5. mail.settings.x509_sign_keyfile = 'filename.key'
  6. mail.settings.x509_sign_certfile = 'filename.cert'
  7. 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:

  1. mail.settings.gpg_home = '/home/www-data/.gnupg/'
  2. mail.settings.cipher_type = 'gpg'
  3. mail.settings.sign = True
  4. mail.settings.sign_passphrase = 'your passphrase'
  5. mail.settings.encrypt = True