If you benefit from web2py hope you feel encouraged to pay it forward by contributing back to society in whatever form you choose!

1. Add required files

You can download the files from here.

  • put jquery.multiuploads.js in static/js folder
  • put multiuploads.css in static/css folder

2. models/db.py - configure email

Update the setting like below depending on your environment

##configure email 
from gluon.tools import Mail mail=auth.settings.mailer
mail.settings.server =
'xxx.xxx.xxx.xxx' mail.settings.sender
= 'admin@xxx.com' mail.settings.login = None  mail.settings.tls = False

3. model/db.py define table

Add the following code at the bottom

import os
filepath = os.path.join(request.folder,'uploads')

db.define_table('documents',
    Field('subject',requires=IS_NOT_EMPTY()),
    Field('message','text',requires=IS_NOT_EMPTY()),
    Field('attachment','upload',uploadfolder=filepath)
    )

4. controllers/defaut.py

Replace def index() with the following

def index():
    form=SQLFORM.factory(db.documents)

    if form.accepts(request):
        files = []
        for var in request.vars:
             if var.startswith('attachment') and request.vars[var] != '':

                 # Insert
                 element = request.vars[var] 
                 number = db.documents.insert(attachment=db.documents.attachment.store(
                     element.file,element.filename))

                 # Retrieve new file name
                 record = db.documents(db.documents.id==number)      
                 files += [Mail.Attachment(filepath + '/' + record.attachment, 
                            element.filename)]
        response.flash = 'Mail sent !'

        # Send Email

        mail.send('admin@xxx.com',
        request.vars.subject,
        request.vars.message,
        attachments = files
                 )

    return dict(form=form)

5. views/defaut/index.html

Replace with the following code

{{response.files.extend([URL('static','css/multiupload.css'),URL('static','js/jquery.multiupload.js')])}}
{{left_sidebar_enabled,right_sidebar_enabled=False,False}}
{{extend 'layout.html'}}

<h3>Send Mail</h3>
<div>
{{=form}}
<script type="text/javascript" charset="utf-8">
//<!--
jQuery('input[name="attachment"]:not(.processed)').multiUpload({
mw_placeholder:"{{=T('upload files')}}",
mw_text_addBtn:"+",
mw_tooltip_addBtn:"{{=T('add a file')}}",
mw_text_clearBtn:"x",
mw_tooltip_clearBtn:"{{=T('remove all')}}",
mw_tooltip_removeFileBtn:"{{=T('remove this file')}}",
mw_tooltip_removeGroupBtn:"{{=T('remove this file group')}}",
mw_group_title:"{{=T('FILE GROUP')}}",
mw_fileNumber:false,mw_maxElementAllowed:5});
//-->
</script>
</div>

6. Result

You can upload multiple documents.

alt text

And you will receive email with attachment which has original name

alt text

Related slices

Comments (0)


Hosting graciously provided by:
Python Anywhere