Search code examples
pythonmailtourl-parsing

Parse mailto urls in Python


I'm trying to parse mailto URLs into a nice object or dictionary which includes subject, body, etc. I can't seem to find a library or class that achieves this- Do you know of any?

mailto:[email protected]?subject=mysubject&body=mybody

Solution

  • Seems like you might just want to write your own function to do this.

    Edit: Here is a sample function (written by a python noob).

    Edit 2, cleanup do to feedback:

    from urllib import unquote
    test_mailto = 'mailto:[email protected]?subject=mysubject&body=mybody'
    
    def parse_mailto(mailto):
       result = dict()
       colon_split = mailto.split(':',1)
       quest_split = colon_split[1].split('?',1)
       result['email'] = quest_split[0]
    
       for pair in quest_split[1].split('&'):
          name = unquote(pair.split('=')[0])
          value = unquote(pair.split('=')[1])
          result[name] = value
    
       return result
    
    print parse_mailto(test_mailto)