I'm trying to send an email with custom recipient, subject and body in Python. This means I don't want to use Python's smtp packages, but something like webbrowser because I need this to open the default email client (Thunderbird in my case) with said arguments.
import webbrowser
webbrowser.open("mailto:[email protected]&subject=mysubject", new=1)
works but I need it to get the recipient from a string I previously found and body from a text file I previously created.
Is there any "simple" way to do it that I cannot find? Thanks in advance.
I use the variables recipient and subject to store the relative values. Simply replace the example text between single quotes with your real value.
recipient = 'emailaddress'
subject = 'mysubject'
The subject field can't contain white spaces, so they have to be url encoded using %20 ASCII code
subject = subject.replace(' ', '%20')
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.
It's possible to use also the urllib module for url encoding (see urllib.urlencode() method), but it can be done simply using the replace() method so you can avoid importing another module just for that.
Now you need to load the text from a text file and store it in a variable. Imagine you have a text file called body.txt:
with open('body.txt', 'r') as b:
body = b.read()
Note that I assumed body.txt 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.
I used the open() function and I provide 2 parameters: the first one is the filename, the second one is the mode you want to open the file with. You want to read the file so you have to open it in read mode ('r'). 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 handle, in this case I called it b.
Now for reading ALL the text you can use b.read() and then you can assign it to the variable body. (If you wanted to read it line by line, you would have done: b.readline() but you don't want this in this case.)
Note that I used the with 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 with was available you would have to do something like this:
b = open('body.txt', 'r'):
body = b.read()
b.close()
Now it's better to url encode also the string contained in the variable body, so we do the same thing we did for the subject:
body = body.replace(' ', '%20')
Now it's time to use the webbrowser module and provide the data you got so far as parameter, concatenating the strings.
webbrowser.open('mailto:?to=' + recipient + '&subject=' + subject + '&body=' + body, new=1)
Obviously you also need to import the webbrowser module before using it. I'll rewrite the whole program without comments for clarity:
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 + '&subject=' + subject + '&body=' + body, new=1)
Use string concatenation.
recipient = '[email protected]'
subject = 'mysubject'
body = 'This is a message'
webbrowser.open("mailto:?to='+ recipient + '&subject=' + subject + "&body=" + body, new=1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With