我在我的程序中使用以下函数来发送电子邮件:
def send_email(subject, sender, recipients, text_body):
FILE_TYPES = set(['txt', 'doc', 'docx', 'odt', 'pdf', 'rtf', 'text', 'wks', 'wps', 'wpd'])
form = ApplicationForm (request.files)
submit_name = form.file_upload.data.filename
mail = Mail(app)
msg = Message(subject, sender=sender, recipients=recipients)
msg.body = text_body
if '.' in submit_name and submit_name.rsplit('.', 1)[1] in FILE_TYPES:
filename = secure_filename(submit_name)
form.file_upload.data.save('uploads/' + filename)
with app.open_resource(filename) as fp:
msg.attach(filename, fp.read())
mail.send(msg)
电子邮件工作正常并发送给正确的用户,但附件没有,我相信我可能会错误地引用它,因为文件附件来自表单。
我已经使用下面的功能来保存附件,并且工作正常,所以我不确定为什么上面的方法不起作用,有人可以帮忙吗?
if '.' in submit_name and submit_name.rsplit('.', 1)[1] in FILE_TYPES:
filename = secure_filename(submit_name)
form.file_upload.data.save('uploads/' + filename)
return redirect('home')
编辑:尝试提交时收到的错误消息是:
[Errno 2] No such file or directory: 'C:\\Users\\richard.danvers\\application\\answer.docx'
看起来好像“上传”不包含在路径中,有人知道如何包含这个吗?