I had code working that would upload images and then send them to a specific email. When I tried to adjust the client software to include an email of my choice I ran into issues
import requests
# adds dicoms to the POST request then makes the request to server
url = "http://jamesb67.eu.pythonanywhere.com/upload"
test_files = {('dicom', open("Pre_1", "rb")),
('dicom', open("Pre_2", "rb")),
('dicom', open("Post_1", "rb")),
('dicom', open("Post_2", "rb"))}
payload = {'email': 'email@gmail.com'}
response = requests.post("http://jamesb67.eu.pythonanywhere.com/upload", files = test_files, data = payload)
On the server side to deal with the request as I believe it is encoded as bytes I try to decode it to a string
@app.route("/upload", methods=["POST"])
def upload():
uploaded_files = flask.request.files.getlist("dicom")
a = flask.request.data
payload = a.decode('utf8')
I then call my function
def emailSend(payload):
subject = ""
body = ""
sender_email = "email@gmail.com"
receiver_email = payload
password = ''
And get the following error
File "/usr/local/lib/python3.9/smtplib.py", line 896, in sendmail
raise SMTPRecipientsRefused(senderrs) smtplib.SMTPRecipientsRefused: {'': (555, b'5.5.2 Syntax error. b14sm10930199wri.62 - gsmtp')}
I believe this is due to the email string not uploading and decoding correctly. I've spent a few hours trying every variation on stackoverflow and I'm not sure what I'm doing wrong. The code works fine if I define the recipient email on the server. Any help would be greatly appreciated!