0

I am consuming a web mail asmx service with python as client. However I do not understand how to upload a file with the same service.

The only information I have from the how to use specifications of the server is the placeholders:

GET /mailservice/mailservice.asmx/SendMailWithAttachment?MailFrom=string&NameFrom=string&ListMailTo=string&Subject=string&Body=string&FileName=string&FileContent=string&FileContent=string&FileType=string HTTP/1.1

and this:

HTTP POST
HTTP/1.1 200 OK
Content-Type: application/x-www-form-urlencoded
Content-Length: length

HTTP GET
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

This is what I have tried

filename = "status.csv"
filecontent = open(os.path.basename(filename), 'r').read()
sendmail_service_with_attachment(HOST, URL, email, subject, content, toemail, fromname, filename, filecontent, 'text/csv')
def sendmail_service_with_attachment(host, url, tomail, mailtitle, mailbody, 
                     mailfrom, namefrom, filename, filecontent, filetype):
    """
    Email Notification by Web Service
    MailFrom=string&NameFrom=string&ListMailTo=string&Subject=string&Body=string&FileName=string&FileContent=string&FileContent=string&FileType=string
    """
    try:
        payload = {
                'MailFrom': mailfrom, 
                'NameFrom': namefrom, 
                'ListMailTo': tomail, 
                'NameTo': '', 
                'Body': mailbody, 
                'FileName':filename, 
                'FileContent': filecontent,
                'FileType':filetype
                }

        data = json.dumps(payload)
        response = requests.post(host + url, data)

        return response

I also tried to send the request as a string as follow:

      request_string = host + url + 'MailFrom=' + mailfrom +\
        '&NameFrom=' + namefrom + '&ListMailTo=' +\
        tomail + '&NameTo=' + "" + '&Subject=' +\
        mailtitle + '&body=' + mailbody +\
        'FileName=' + filename +\
        'FileContent=' + filecontent +\
        'FileType=' +filetype

     response = requests.get(request_string)

I expect the asmx service send the file as attachment Response [200], but I receive a Response [500] for the first one and a Response [414] as response for the last one, which I think is because my request is not well constructed. Based on that information, What could be the way to send the file content of a csv file via get/post to a asmxl webmail service?

Sora
  • 95
  • 1
  • 10

1 Answers1

0

Some characters in URLs have special meanings (like '/' or '&') so if these appear in your file, they will make it impossible for the webserver to understand your request.

Try encoding the parameters on your URL

import urllib.parse
complete_url = host + url + urllib.parse.encode(payload)

https://stackoverflow.com/a/5607708

I assume that your .csv file is containing text, if it contains binary you may have to base64 encode that data manually before adding it to the URL.

nitzel
  • 1,565
  • 14
  • 14