Forums

Uploading email Address

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!

It looks like you're trying to use the flask request data as an email address. The string representation of a flask request dictionary starts with a '{' character and that is what the SMTP server is complaining about.

Thanks Glenn! Is there a simple fix that I can do to the above code? Or do I need to use a different method?

You'll need to get the email address from the request. It's a dictionary-like object.

Working now! email = request.form['email']

So simple, but I was looking in the wrong place. Thank you for pointing me in the right direction

Glad to hear that you made it work!