Advanced mailto use in Python
<p>I'm trying to send an email with custom recipient, subject and body in Python.
This means I don't want to use <a href="http://stackoverflow.com/questions/6270782/how-to-send-an-email-with-python">Python's smtp packages</a>, but something like <a href="https://docs.python.org/2.7/library/webbrowser.html?highlight=webbrowser#module-webbrowser" rel="nofollow">webbrowser</a> because I need this to open the default email client (Thunderbird in my case) with said arguments. </p>

<pre><code>import webbrowser
webbrowser.open("mailto:?to=aaa@email.com&amp;subject=mysubject", new=1)
</code></pre>

<p>works but I need it to get the recipient from a string I previously found and body from a text file I previously created.</p>

<p>Is there any "simple" way to do it that I cannot find?
Thanks in advance.</p>

<p>I use the variables <strong>recipient</strong> and <strong>subject</strong> to store the relative values. Simply replace the example text between single quotes with your real value.</p>

<pre><code>recipient = 'emailaddress'
subject = 'mysubject'
</code></pre>

<p>The subject field can't contain white spaces, so they have to be url encoded using %20 ASCII code</p>

<pre><code>subject = subject.replace(' ', '%20')
</code></pre>

<p>the function above replaces the white space with "%20" and assigns the modified subject to the same variable since you can reuse it, you really don't need another one in this case.</p>

<p>It's possible to use also the <strong>urllib</strong> module for url encoding (see <strong>urllib.urlencode()</strong> method), but it can be done simply using the <strong>replace()</strong> method so you can avoid importing another module just for that.</p>

<p>Now you need to load the text from a text file and store it in a variable.
Imagine you have a text file called <strong>body.txt</strong>:</p>

<pre><code>with open('body.txt', 'r') as b:
    body = b.read()
</code></pre>

<p>Note that I assumed <strong>body.txt</strong> is in the same directory of your Python script, otherwise in the filename parameter you have to include the full absolute or relative path to the file, followed by the file name.</p>

<p>I used the <strong>open()</strong> function and I provide 2 parameters: the first one is the <strong>filename</strong>, the second one is the <strong>mode</strong> you want to open the file with. You want to read the file so you have to open it in <strong>read mode</strong> (<strong>'r'</strong>). Once you open the file you need to be able to identify the file with a variable in order to perform some operations on it. This kind of variable is technically called <strong>handle</strong>, in this case I called it <strong>b</strong>.</p>

<p>Now for reading ALL the text you can use <strong>b.read()</strong> and then you can assign it to the variable <strong>body</strong>. (If you wanted to read it line by line, you would have done: <strong>b.readline()</strong> but you don't want this in this case.)</p>

<p>Note that I used the <strong>with</strong> statement, this is the preferred way for opening and working with files, because it will automatically close the file at the end, else you would have to do it manually. Before <strong>with</strong> was available you would have to do something like this:</p>

<pre><code>b = open('body.txt', 'r'):
body = b.read()
b.close()
</code></pre>

<p>Now it's better to url encode also the string contained in the variable <strong>body</strong>, so we do the same thing we did for the <strong>subject</strong>:</p>

<pre><code>body = body.replace(' ', '%20')
</code></pre>

<p>Now it's time to use the <strong>webbrowser</strong> module and provide the data you got so far as parameter, concatenating the strings.</p>

<pre><code>webbrowser.open('mailto:?to=' + recipient + '&amp;subject=' + subject + '&amp;body=' + body, new=1)
</code></pre>

<p>Obviously you also need to import the <strong>webbrowser</strong> module before using it.
I'll rewrite the whole program without comments for clarity:</p>

<pre><code>import webbrowser

recipient = 'emailaddress'
subject = 'mysubject'

with open('body.txt', 'r') as b:
    body = b.read()

body = body.replace(' ', '%20')

webbrowser.open('mailto:?to=' + recipient + '&amp;subject=' + subject + '&amp;body=' + body, new=1)
</code></pre>

python python-2.7 email python-webbrowser