how to generate a standard email form by default OS mail client?

Thomas Jollans tjol at tjol.eu
Tue Jul 30 06:31:00 EDT 2019


On 30/07/2019 10.06, dmitrey15 at gmail.com wrote:
> Hello,
>
> is Python capable of generating a email form (from standard data - address, topic, string message) with raising it by a default OS email client, e.g. Thunderbird? User would like to have a possibility to review/modify email content inside the OS email client window before sending it.
>
> Raising gmail.com GUI form also could be a solution.
>
> Thank you in advance, D.

You could construct a mailto URI

https://en.wikipedia.org/wiki/Mailto

>>>> subject = 'This is the subject'
>>> body = 'This is the text'
>>> to = 'god at heaven.example.com'
>>> cc = 'friedrich.nietzsche at unibas.ch'
>> mailto_url = urllib.parse.quote(f'mailto:{to}?') +
urllib.parse.urlencode({'cc': cc, 'subject': subject, 'body': body},
quote_via=urllib.parse.quote)

If you open that URI with the webbrowser module, it should work. Well,
it might. On my system, Chrome refuses to open a mailto URI like this,
but Firefox plays along.

The better option would be to call the mail program directly (such as
using the subprocess module), but how you find out what to call will
depend on your OS. If this is just for one PC and you use Thunderbird,
then you might as well hard-code the Thunderbird executable, of course...






More information about the Python-list mailing list