Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Python
if request.files['attachment'].filename == "": # //The error is in this line//
            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
Posted
Updated 24-May-18 21:57pm
v2
Comments
Richard MacCutchan 25-May-18 3:55am    
What is the error?
Member 13843763 25-May-18 4:45am    
if request.files['attachment'].filename == "":
File "/usr/local/lib/python3.4/dist-packages/werkzeug/datastructures.py", line 431, in __getitem__
raise exceptions.BadRequestKeyError(key)
werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: KeyError: 'attachment'
Member 13843763 25-May-18 4:45am    
So, this is the error
if request.files['attachment'].filename == "":
So, what error is in this line?
Richard MacCutchan 25-May-18 4:52am    
The error message is telling you that the index key 'attachment' does not exist.
Member 13843763 25-May-18 5:15am    
give me the solution , not the meaning. I am able to send email with attachment but not able to send email without attachments.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900