-3

I am writing a program in Golang that will use Mozilla's Thunderbird email client to send email. The Windows command that should be executed is:

 start "" "C:\Program Files (x86)\Mozilla Thunderbird\thunderbird.exe" -compose "to='CloudCoin@Protonmail.com',subject='Subject1',body='Hello'" -offline

My Go code looks like this (command is the one listed above) :

    var command string
    command = `start "" "C:\Program Files (x86)\Mozilla Thunderbird\thunderbird.exe"`
    command += ` -compose "to='` + toAddress + `',`
    command += `subject='Subject1',`
    command += `body='Hello'"`
    command += ` -offline`

    cmd := exec.Command("cmd.exe", "/C", command)

But I get an error:

Windows cannot find '\\'. Make sure you typed the name correctly, and then try again. 

If I change the code to this (moving the word start):

    var command string
    command = ` "" "C:\Program Files (x86)\Mozilla Thunderbird\thunderbird.exe"`
    command += ` -compose "to='` + toAddress + `',`
    command += `subject='Subject1',`
    command += `body='Hello'"`
    command += ` -offline`

    fmt.Println("Command: " + command)

    cmd := exec.Command("cmd.exe", "/C", "start", command)

Then I get another error:

Windows cannot find 'Files'. Make sure you typed the name correctly, and then try again. 

It seems that instead of trying to start "" it is trying to start \\. How can I keep my double-quotes?

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
Sean H. Worthington
  • 1,701
  • 15
  • 9

1 Answers1

1

Your problem likely is that every separate string passed to exec.Command is passed on (without parsing it) as a single argument to cmd.exe, which does probably not split given strings either, so you have to do it yourself.

See this example where the argument is being split up too. You should be able to leave " out since you split it up manually anyway, or write a program for it or run it with an interpreter which does the splitting.

func do() {
    args := []string{
        "/C",
        "start",
        "",
        `C:\Program Files (x86)\Mozilla Thunderbird\thunderbird.exe`,
        "-compose",
        "to=" + toAddress + ",subject=Subject1,body=Hello",
        "-offline",
    }
    cmd := exec.Command("cmd.exe", args...)
}
Fabian
  • 5,040
  • 2
  • 23
  • 35