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:\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
			fmt.Println(key, value)


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

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

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.