andrewdesantis.com : Sending mail

Sending mail

Problem

How to send mails from web.py

Solution

web.sendmail function can be used to send mail from web.py.

web.sendmail('[email protected]', '[email protected]', 'subject', 'message')

If any mail server is specified in web.config it uses that to send the mail or it uses the traditional sendmail from /usr/lib/sendmail.

web.config.smtp_server = 'mail.mydomain.com'

To send a mail to multiple recipients, a list can be passed for to_address.

web.sendmail('[email protected]', ['[email protected]', '[email protected]'], 'subject', 'message')

Optinal cc and bcc keyword arguments can be passed to web.sendmail to add Cc and Bcc recipients. Values of cc and bcc can be list as well.

web.sendmail('[email protected]', '[email protected]', 'subject', 'message', cc='[email protected]', bcc='[email protected]')

Addition headers can be passed to web.sendmail via headers tuple.

web.sendmail('[email protected]', '[email protected]', 'subject', 'message',
        cc='[email protected]', bcc='[email protected]',
        headers=({'User-Agent': 'webpy.sendmail', 'X-Mailer': 'webpy.sendmail',})
        )