web2py send uploaded file like email attachment

2019-08-03 20:38发布

问题:

I i've done an app in web2py to store FAQ and that give me the possibility to send those FAQ via EMAIL, i'd like to send (when i got that) also an attachment that i upload in the FAQ that i create.

I'v got this in my DB:

db.define_table('faq',
   Field('tipo',db.tipo_faq),
   Field('sotto_categoria',db.sotto_categoria_faq),
   Field('oggetto', requires = (IS_UPPER(), IS_NOT_EMPTY())),
   Field('testo_faq',type='text',requires = (IS_UPPER(), IS_NOT_EMPTY())),
   Field('note',type='text',requires = IS_UPPER()),
   Field('faq_ok', type= 'boolean', default = False),
   Field('faq_verifica', type= 'boolean', default = False),
   Field('data_caricamento',type='datetime', writable = False, readable = False, default=request.now, requires=IS_DATETIME(timezone=pytz.timezone("Europe/Gibraltar"))),
   Field('allegato',type='upload', uploadfield = 'nome_file', uploadfolder= allegati),
   Field('nome_file','blob'),
   Field('firma',type='text',readable = False, writable = False,
         default = '<p align="right" style = "font-size:12px">Sistema automatizzato F.A.Q si prega di non rispondere al presente messaggio.<br />Grazie.<br /> </p> <p align="right" style = "font-size:12px"> Per richiedere assistenza <a  href="https://gestionale.porsennasrl.it:81/ASSISTENZA-PA ">CLICCARE QUI</a> </p><p align="right" style = "font-size:12px">HELP DESK PORSENNA SRL</p>'),
    format='%(oggetto)s')

and this in my controller:

@auth.requires_login()
def prova_invio():
    mail = Mail()
    mail = auth.settings.mailer
    mail.settings.server = 'logging' if request.is_local else myconf.get('smtp.server')
    mail.settings.sender = myconf.get('smtp.sender')
    mail.settings.login = myconf.get('smtp.login')
    mail.settings.tls = myconf.get('smtp.tls') or False
    mail.settings.ssl = myconf.get('smtp.ssl') or False
    faq= db.faq(request.vars.id)
    testo= "<html>"+(faq.testo_faq)+"</html>" + "<html>"+(faq.firma)+"</html>"

    if mail.send(to=request.vars.email,
                subject= faq.oggetto,
                message= testo,
                attachments = mail.Attachment("/home/www-data/web2py/applications/DBurbi/allegati/"+faq.allegato)):
                status = 'RIUSCITO'

    else:
                status = 'FALLITO'
    return dict(status=status,indirizzo=request.vars.email,oggetto=faq.oggetto)

when i send the email i got this error:

[Errno 2] No such file or directory: '/home/www-data/web2py/applications/DBurbi/static/faq.allegato.abb2396f642c3279.706172746e65722e6a7067.jpg'

I lerned that the name of the file isn't that one, so how can i store the real name of the attachment so i can send it? And how can i send the attachment only if "there is one" and send email also if there isn't any attachment?

Thank you guys

回答1:

First, you would not specify both uploadfield and uploadfolder -- the first is for storing files in a blob field within the database, and the second is for specifying a filesystem folder in which to store files. Pick one or the other (since you have specified uploadfield, that takes precedence, so files are being stored in the database, not the filesystem).

Note, rather than passing a file path to mail.Attachement, you can instead pass an open file object as the first argument along with a "filename" argument. To get the file object and filename, you can do the following:

if faq.allegato:
    filename, stream = db.faq.allegato.retrieve(faq.allegato)
    attachment = mail.Attachment(stream, filename=filename)
else:
    attachment = None

Then do:

mail.send(..., attachments=attachment)

Note, the above will work whether the files are stored in the database or on the filesystem.