Add variable in smtp.SendMail(

I have managed to send a post from web to go. All works well except for to incorporate the senders mail.

Typing it manually there is no problem:

msg := []byte(“From: sender@example.com\r\n” +

But trying to add a variable it causes error:

msg := []byte("From: " + (mail) + “\r\n” +

func Send(form url.Values) {

var mail string

for key, values := range form {
	for _, value := range values {
		switch key {
		case "mail":
			mail = value
		default:
			fmt.Println(key, value)
		}

	}
}

fmt.Println(mail) <----- correct value printed

auth := smtp.PlainAuth("", "sibert@gmail.com", "password", "smtp@gmail.com")
from := (mail)
to := []string{"support@example.com"}
msg := []byte((mail) + <----------------- causing an error
	"To: support@company.com\r\n" +
	"Subject: subject\r\n" +
	"\r\n" +
	(mail)) <----- transmitted correct
err := smtp.SendMail("smtp.gmail.com:587", auth, from, to, msg)
if err != nil {
	log.Fatal(err)
}
}

Any tip how to use the variable “mail” to send form values?

1 Like

What is the value of from ? and the error message is ?

1 Like

url.Values. Golang crashes,

1 Like

url.Values is the type, not the value of form.

And “Golang crashes” would mean that go the compiler crashes even before you are able to run the program. But probably you can run your program and it crashes. The runtime then probably provides you a stack trace, how das that look like?

1 Like

This syntax solved the actual problem - i e NO parentheses:

msg := []byte("From: " + mail + “\r\n” +

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.