0

I tried something from here: Python: Open Thunderbird to write new mail with attached file This code snippet opens a new email to be written in Thunderbird, but it doesn't include any of the specifications from the code below (email address, subject, body, attachment), it's just a blank new email:

import os
os.system("/Applications/Thunderbird.app/Contents/MacOS/thunderbird -compose 
to='abc@abc.com',subject='hello',body='attached is txt 
file',attachment='Users/Username/Desktop/test.txt'")

How do I write it so that the arguments I passed it are included?

UPDATE: Ok, it is mostly working with this format, however the attachment is not getting attached:

os.system("/Applications/Thunderbird.app/Contents/MacOS/thunderbird -compose
'to=abc@abc.edu','subject=this subject','body=this is the
body','attachment=/Users/Username/Desktop/test.txt'")

Any ideas on how to change the attachment format so that it successfully attaches? It's not throwing any errors with this format, it's simply not attaching the file.

UPDATE: it's working now, I missed one slash, the above format now works for me.

Community
  • 1
  • 1
Kemba777
  • 1
  • 1
  • what do you want exaclty? that you make your own function that do that and pass the arguments using command line? – Abdelouahab Dec 08 '14 at 23:55

2 Answers2

0

as you can see, this

/Applications/Thunderbird.app/Contents/MacOS/thunderbird -compose to='abc@abc.com',subject='hello',body='attached is txt file',attachment='Users/Username/Desktop/test.txt'

is just an str, so remove the variables, use format(), and then, use argparse to catch console arguments:

import os
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('app', help='some help')
parser.add_argument('to', help='some help')
parser.add_argument('subject', help='some help')
parser.add_argument('body', help='some help')
parser.add_argument('attachement', help='some help')

args = parser.parse_args()

os.system("{0} -compose to={1},subject={2},body={3},attachment={4}".format(args.app, args.to, args.subject, args.body, args.attachement))

then, name it something like mailer.py and run it with to see the help if it is not you who will use it.

python mailer.py --help

now, if you want to use it inside a python program (for example Django), what you do, is just replace where there is args.* with a normal variable.

Abdelouahab
  • 7,331
  • 11
  • 52
  • 82
  • I tried this and it gives the same, just a blank new email. In parentheses for app I put Thunderbird path, for "to" I put destination email address in double quotes and it still doesn't work. I need a new email to pop up in Thunderbird with the address, subject, body and attachment to all be there. The only thing I can successfully do is open up a new blank email in Thunderbird. – Kemba777 Dec 09 '14 at 00:09
  • have you used `r'string'` maybe, it is because of `/t` – Abdelouahab Dec 09 '14 at 00:10
  • I think using r'string' just changes how backslashes are interpreted. – Kemba777 Dec 09 '14 at 00:14
  • oops, sorry, since i come from windows.... it seems that i am too tired to think logicaly! sorry again – Abdelouahab Dec 09 '14 at 00:15
  • why not using `smtplib` here is a simple example that works https://github.com/abdelouahabb/essog/blob/master/maill.py – Abdelouahab Dec 09 '14 at 00:17
  • Thanks for your help, it's working almost, see my original post for what worked, but the attachment won't attach, any ideas? – Kemba777 Dec 09 '14 at 04:48
0

It works for me on Windows, but maybe you're missing the double quotes. Try using:

import os
os.system("/Applications/Thunderbird.app/Contents/MacOS/thunderbird -compose 
\"to='abc@abc.com',subject='hello',body='attached is txt 
file',attachment='Users/Username/Desktop/test.txt'\"")

Since the documentation says:

Watch out for the somewhat complex syntax of the "-compose" command-line option. The double-quotes enclose the full comma-separated list of arguments passed to "-compose", whereas single quotes are used to group items for the same argument.

Hope this helps!

cdonts
  • 9,304
  • 4
  • 46
  • 72
  • Thanks, I'm putting the exact same thing in and it only gets as far as creating a new empty email, I'm not sure where the formatting is going wrong for the rest of the arguments. There's a lot of documentation for Windows on this subject, not a lot on osx though. – Kemba777 Dec 09 '14 at 02:47