web2py发送上传的文件,如电子邮件附件 [英] web2py send uploaded file like email attachment
问题描述
我我已经在web2py中完成了一个应用程序来存储常见问题解答,这使我可以通过EMAIL发送这些常见问题解答,我想发送(当我收到该问题时)还可以将附件上传到我创建的常见问题解答中./p>
我已经在数据库中找到了这个
db.define_table('faq',栏位('tipo',db.tipo_faq),栏位('sotto_categoria',db.sotto_categoria_faq),字段('oggetto',要求=(IS_UPPER(),IS_NOT_EMPTY())),字段('testo_faq',type ='text',requires =(IS_UPPER(),IS_NOT_EMPTY())),字段('note',type ='text',requires = IS_UPPER()),Field('faq_ok',type ='boolean',默认= False),Field('faq_verifica',type ='boolean',默认= False),Field('data_caricamento',type ='datetime',可写= False,可读= False,默认= request.now,requires = IS_DATETIME(timezone = pytz.timezone("Europe/Gibraltar"))),Field('allegato',type ='upload',uploadfield ='nome_file',uploadfolder = allegati),栏位('nome_file','blob'),字段('firma',type ='text',可读= False,可写= False,默认值='< p align ="right" style ="font-size:12px"> Sistema automatizzato F.A.Q siprega di non respondere al presente messaggio.</p>< p align ="right" style ="font-size:12px">每个Richiedere协助< a href ="https://gestionale.porsennasrl.it:81/ASSISTENZA-PA"&C; CLICCARE QUI</a></p>< p align ="right" style ="font-size:12px">帮助台PORSENNA SRL</p>'),格式='%(oggetto)s')
这在我的控制器中:
@ auth.requires_login()def prova_invio():邮件= Mail()邮件= auth.settings.mailermail.settings.server ='logging'如果request.is_local否则为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')或Falsemail.settings.ssl = myconf.get('smtp.ssl')或False常见问题解答= db.faq(request.vars.id)testo =< html>" +(faq.testo_faq)+</html>"+< html>" +(faq.firma)+</html>"如果mail.send(to = request.vars.email,subject = faq.oggetto,message = testo,附件= mail.Attachment("/home/www-data/web2py/applications/DBurbi/allegati/" + faq.allegato)):状态='RIUSCITO'别的:状态="FALLITO"返回dict(status = status,indirizzo = request.vars.email,oggetto = faq.oggetto)
当我发送电子邮件时,出现此错误:
[Errno 2]没有这样的文件或目录:'/home/www-data/web2py/applications/DBurbi/static/faq.allegato.abb2396f642c3279.706172746e65722e6a7067.jpg'
我认为文件名不是那个,所以我该如何存储附件的真实名称以便发送呢?而我该如何仅在有一个"的情况下发送附件,而在没有附件的情况下也发送电子邮件?
谢谢你们
首先,您将不会同时指定 uploadfield
和 uploadfolder
-第一个是用于将文件存储在数据库中的blob字段,第二个字段用于指定要在其中存储文件的文件系统文件夹.选择一个或另一个(因为您已经指定了 uploadfield
,因此优先),因此文件存储在数据库中,而不是文件系统中.
请注意,您无需将文件路径传递给 mail.Attachement
,而是可以将打开的文件对象作为第一个参数以及"filename"参数传递.要获取文件对象和文件名,您可以执行以下操作:
如果常见问题解答:文件名,流= db.faq.allegato.retrieve(faq.allegato)附件= mail.Attachment(流,文件名=文件名)别的:附件=无
然后做:
mail.send(...,附件=附件)
请注意,无论文件是存储在数据库中还是文件系统中,上述操作均有效.
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
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.
这篇关于web2py发送上传的文件,如电子邮件附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!