Home > Net >  python flask isn't passing on variable
python flask isn't passing on variable

Time:03-10

I'm trying to build a python tool that sends an email to a helpdesk. it does this using flask, javascript, jQueryUI and python. I have set up a dialogue box to capture information needed for a ticket. this information (as well as some information about the user's browser) is then saved as a variable called emailinfo which is passed through a fetch request to flask, and then to a function in python that sends an email. I know that the function I've written will send an email because I've tested it. I also know that flask is receiving the emailInfo variable and that it has information in it, because flask prints the variable it receives and the JS console also prints the information it receives. both of these are fine, but when it tries to send the email, it doesn't send anything.

JavaScript/jQueryUI:


        emailInfo = JSON.stringify("User's Name: "   name.val()   "     " 
          "User's E-Mail: "     email.val()   "     " 
          "Reported Issue: "   issue.val()   "     " 
          "More Details: "   "    " 
          start.val(), null, 2)
        console.log(emailInfo)
        
//get IP/browser information
        $.getJSON('http://ip-api.com/json', function(data) {
        
//put together email info
        emailInfo = emailInfo   " "   JSON.stringify(data, null, 2)
        emailInfo = JSON.stringify({emailInfo})
        console.log(emailInfo)
        });

//fetch helpdesk function 
//note: this is functionally the same as another fetch request I have written that works perfectly fine
        fetch(`${scriptRoot}/helpdesk`, {
                method: 'POST',
                body: JSON.stringify({ mailInfo: emailInfo }),
                headers: {
                  'Content-Type': 'application/json'
                },
            })
      }
      return valid;
    }

Flask

(all functions from other file have been imported)
@app.post("/helpdesk")
def sendtodesk():
    mailInfo = request.get_json().get("mailInfo") 
    mailInfo = str(mailInfo)
#extra info - this prints only the first line of mailInfo and not the scraped IP. this doesn't really matter to me, though, because I think I can fix it
    print(mailInfo)
    SendEmail(mailInfo)
    return("Sent! Thank you.")

Python Email Script

note: I have tested this separately and it works

def SendEmail(emailInfo):
    port = 465  # For SSL
    smtp_server = "smtp.gmail.com"
    #sender and reciever address. here its the helpbot's email and the address of the testing environment helpdesk
    senderemail = "(email sender goes here)"  
    password = "(password to email sender goes here)"
    recieveremail = "(email reciever goes here)"  
    #message
    message = emailInfo

    context = ssl.create_default_context()
    with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
        server.login(senderemail, password)
        server.sendmail(senderemail, recieveremail, message)

I've tried a lot of things to get this to work. the best success I've had has been in making it similar to another flask function I've made that works perfectly fine, which has allowed me to get as far as printing the variable. I can't tell whether the variable gets through to the function, however I know that the SendEmail function is called because the sender sends an email. This is the email that is sent, there is nothing in there, which makes me think that flask never passes the variable on properly.

thanks for any and all help you can give me. I've already done a lot for this project (it is also an NLTK chatbot that works perfectly fine, which is how I've done flask before - the code here is very similar to the code used there) and I've really hit a brick wall with this after doing everything in my programming power.

CodePudding user response:

turns out it was due to the format of the emailinfo variable. the smtp and ssl method of sending mail is a string, but requires a certain format. having colons broke it. I need to have colons because that's the format that the IP API returns the information I need in. switching to another email package (I used yagmail) has worked fine :)

CodePudding user response:

First improvment, use string formatting:

let email_info = `User's Name: ${name.val()}\n User's E-Mail: ${email.val()}\n Reported Issue: ${issue.val()}\n More Details: ${start.val()}\n`;

Second improvment, create a second entry in json and seperate the data, also use fetch api just to get data, and combine this data together on python side.

{ mailInfo: emailInfo,  ipapiInfo: <your json data from ip-api>}

then u can better debug wich or where date get lost, maybe this helps u a little bit

  • Related