Search code examples
pythonemailparsingmailto

Making a Python program handle mailto links


I made a Python program that is capable of sending email. However, I want to make it capable of being a default email client such that it will capture the email address and subject of HTML email links (mailto links) when the user clicks them.

How do I get the email address and subject to my client?

Currently, I can set my program as the default mail client, but I don't know what information, or format of information, it's getting from the web browser; so, I'm not sure how to parse it.


Solution

  • Assuming the complete link is passed in as sys.argv[1], you need to do something like this:

    import urllib.parse
    parsed = urllib.parse.urlparse(sys.argv[1])
    mail_addr = parsed.path
    fields = urllib.parse.parse_qs(parsed.query)
    

    This sets mail_addr to the address to send the email to, while field will be a dictionary of additional parameters.

    The fields that you can expect to be present are specified in RFC 6068