【问题标题】:web2py send uploaded file like email attachmentweb2py 发送上传的文件,如电子邮件附件
【发布时间】:2017-06-23 09:57:08
【问题描述】:

我 我已经在 web2py 中完成了一个应用程序来存储常见问题解答,这使我可以通过 EMAIL 发送这些常见问题解答,我想发送(当我收到时)我在我创建的常见问题解答中上传的附件。

我的数据库中有这个:

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')

这在我的控制器中:

@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)

当我发送电子邮件时,我收到了这个错误:

[Errno 2] 没有这样的文件或目录: '/home/www-data/web2py/applications/DBurbi/static/faq.allegato.abb2396f642c3279.706172746e65722e6a7067.jpg'

我知道文件名不是那个,那我如何存储附件的真实名称以便发送它? 以及如何仅在“有一个”的情况下发送附件,并且在没有任何附件的情况下也发送电子邮件?

谢谢大家

【问题讨论】:

    标签: python email web2py


    【解决方案1】:

    首先,您不能同时指定uploadfielduploadfolder - 第一个用于将文件存储在数据库中的blob 字段中,第二个用于指定用于存储文件的文件系统文件夹。选择一个或另一个(因为您已指定 uploadfield,因此优先,因此文件存储在数据库中,而不是文件系统中)。

    注意,除了将文件路径传递给mail.Attachement,您还可以将打开的文件对象作为第一个参数与“文件名”参数一起传递。要获取文件对象和文件名,您可以执行以下操作:

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

    然后做:

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

    请注意,无论文件是存储在数据库中还是文件系统中,上述方法都适用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-30
      • 1970-01-01
      • 2020-04-19
      • 2021-03-06
      相关资源
      最近更新 更多