I'm having trouble with how would I get the file chosen from forms to be sent in the email attachment. I am a beginner with this so I'm not familiar with how I could properly translate the input to the directory.
I do not how to properly translate this code to be able to get the uploaded file of the user:
file_attachments = [r.\somedirectory\some.jpg]
This is my code on trying to get the users uploaded file:
if request.method == "POST":
file_attachments = os.path.realpath(request.files['attfile']),
emailMsg = request.form['body']
mimeMessage= MIMEMultipart()
mimeMessage['to']=request.form['email']
mimeMessage['subject']=request.form['subject']
mimeMessage.attach(MIMEText(emailMsg,'plain'))
for attachment in file_attachments:
content_type, encoding= mimetypes.guess_type(attachment)
main_type,sub_type = content_type.split('/',1)
file_name = os.path.basename(attachment)
f = open(attachment,'rb')
myFile = MIMEBase(main_type, sub_type)
myFile.set_payload(f.read())
myFile.add_header('Content-Disposition', 'attachment', filename=file_name)
encoders.encode_base64(myFile)
f.close()
mimeMessage.attach(myFile)
raw_string = base64.urlsafe_b64encode(mimeMessage.as_bytes()).decode()
message = service.users().messages().send(
userId='me',
body = {'raw':raw_string}).execute()
return render_template('email.html')
The error message:
Traceback (most recent call last)
File "C:\Users\Shandon\Anaconda3\lib\site-packages\flask\app.py", line 2463, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\Shandon\Anaconda3\lib\site-packages\flask\app.py", line 2449, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\Shandon\Anaconda3\lib\site-packages\flask\app.py", line 1866, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Shandon\Anaconda3\lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\Shandon\Anaconda3\lib\site-packages\flask\app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\Shandon\Anaconda3\lib\site-packages\flask\app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\Shandon\Anaconda3\lib\site-packages\flask\app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Shandon\Anaconda3\lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\Shandon\Anaconda3\lib\site-packages\flask\app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\Shandon\Anaconda3\lib\site-packages\flask\app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "c:\Users\Shandon\Desktop\50\Capstone\website\auth.py", line 295, in send_message
Open an interactive python shell in this framefile_attachments = os.path.realpath(request.files['attfile']),
File "C:\Users\Shandon\Anaconda3\lib\ntpath.py", line 526, in abspath
return normpath(_getfullpathname(path))