Forums

Send mail with attachment on schedule using Web2Py

  1. Assuming the Web2Py application has the mail object defined already...Create python script sendmail.py :

    import datetime EmailSubject = 'Backup from PythonAnywhere sent at '+ str(datetime.datetime.now()) mail.send(to='myemail@gmail.com', subject=EmailSubject, message='Find the attached', attachments = [mail.Attachment('/path/to/file1.file'), mail.Attachment('/path/to/file2.file')])

  2. Upload python script to web2py/applications/

  3. Create a bash script to run the python script as part of the app named app - as per the web2py book:

python /home/app/web2py/web2py.py -S app -M -R applications/sendmail.py

  1. Give permissions to the bash file: chmod 700 /path/to/file.sh
  2. Add the script to the scheduler at xx mins past or other frequency

That's it ! An email with attachment will be sent automatically.