I am developing a website in Python Flask. What I have to do is to get uploaded file in one of the forms and attach it to mail. I tried solution given in similar question but it gives following error:
AttributeError: 'str' object has no attribute 'filename'
What am I doing wrong? I am beginner in python and flask. Here's my code:
class UploadForm(Form):
branch = StringField('branch', validators = [DataRequired()])
year = StringField('year', validators = [DataRequired()])
sub = StringField('sub', validators = [DataRequired()])
paper = FileField('Logo', validators = [Required()])
def uploadPaper(form):
msg = Message('New upload request', sender = ADMINS[0], recipients = ADMINS)
msg.body = 'text body'
msg.html = 'Branch: ' + form.branch.data + '<br />' + 'Year: ' + form.year.data + '<br />' + 'Subject: ' + form.sub.data
msg.attach(form.paper.data.filename, 'application/octect-stream', form.paper.data.read())
with app.app_context():
mail.send(msg)
return "success"
I am calling uploadPaper() function on validating the form.