0

I am trying to make a bot which sends mails. I got the following code. I am wondering, if it's possible to place the mails slice into this field seperated by a , ?

&bcc=

If my test.txt contains

test1@mail.com
test2@mail.com

I'd like the part of the link to contain &bcc=test1@mail.com,test2@mail.com Is this doable with Go?

package main

import (
    "bufio"
    "fmt"
    "log"
    "os"
    "os/exec"
)

func main() {
    file, err := os.Open("test.txt")
    if err != nil {
        log.Fatal(err)
    }
    var mails []string

    scanner := bufio.NewScanner(file)
    for scanner.Scan() {
        mails = append(mails, scanner.Text())
    }

    fmt.Println(mails)
    exec.Command("xdg-open", "https://mail.google.com/mail/u/0/?fs=1&tf=cm&to=contact@test.com,&bcc=test1@mail.com,test2@mail.com&su=Hello+World!&body=This+Is+Just+An+Example").Run()

}

Dave
  • 1
  • 1
  • 9
  • 38

1 Answers1

0

You can use "fmt.Sprintf()".It solves your problem.

package main

import (
    "bufio"
    "fmt"
    "log"
    "os"
)

func main() {
    file, err := os.Open("test.txt")
    if err != nil {
        log.Fatal(err)
    }
    var mails []string

    scanner := bufio.NewScanner(file)
    for scanner.Scan() {
        mails = append(mails, scanner.Text())
    }

    sendMails := ""
    for _, m := range mails {
        sendMails += fmt.Sprintf("%s", m)
    }
    command := fmt.Sprintf("https://mail.google.com/mail/u/0/?fs=1&tf=cm&to=contact@test.com,&bcc=%s&su=Hello+World!&body=This+Is+Just+An+Example", sendMails)
    fmt.Println(mails)
    exec.Command("xdg-open", command).Run()

}
Tabriz Atayi
  • 5,880
  • 7
  • 28
  • 33
  • it is in the answer. – Tabriz Atayi Apr 02 '21 at 18:56
  • I have one more question, is it possible to stimulate keyboard click with Go? – Dave Apr 02 '21 at 19:42
  • @FriedRiceEaterz maybe check out [this question](https://stackoverflow.com/questions/27198193/how-to-react-to-keypress-events-in-go). If you are trying to press a key in order to send an email or perform some action (I see the mail.google.com link in your initial code), maybe you should use the Gmail API instead. – grantslape Apr 02 '21 at 21:34