if request.files['attachment'].filename == "":
message = api.create_message(sender, to, subject, message_text)
else:
NOT_ALLOWED_EXTENSION =["ade","adp","bat","chm","cmd","com","cpl","dll","dmg","exe",
"hta","ins","isp","jar","js","jse","lib","lnk","mde","msc","msi","msp","mst","nsh",
"pif","scr","sct","shb","sys","vb","vbe","vbs","vxd","wsc","wsf","wsh"]
file_extension = request.files['attachment'].filename.split(".")[-1]
if file_extension in NOT_ALLOWED_EXTENSION:
n = notify2.Notification('Email Client', "Message sending failed due to invalid file extension" )
n.show()
return redirect(url_for('sent'))
else:
file_obj = request.files['attachment']
file_obj.save(os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(file_obj.filename)))
file_dir = app.config['UPLOAD_FOLDER']
filename = secure_filename(file_obj.filename)
message = api.CreateMessageWithAttachment(sender, to, subject, message_text, file_dir, filename)
result = api.send_message(message)
if result == "error":
n = notify2.Notification('Email Client', "Message is not sent" )
n.show()
else:
n = notify2.Notification('Email Client', "Message sent" )
n.show()
return redirect(url_for('task', query="SENT"))
What I have tried:
I tried removing the error line and the part where there is attachment to be sent. But the email is going fine without attachments, but with attachments there is error