Field not accessible

I am not a go programmer, but I need to modify some code. In this code:

// sendmail invokes go's sendmail method
func sendmail(payload []byte, sendTo []string) (err error) {

	if cfg.Mail.SMTPAuth {
		auth := smtp.PlainAuth(
		"",
		cfg.Mail.Username,
		cfg.Mail.Password,		
		cfg.Mail.SMTPRelay)	//not thru tor
		} else {
		auth := smtp.PlainAuth(
		"",
		cfg.Mail.Username,
		cfg.Mail.Password,
		"")					//thru tor
		}

   relay := fmt.Sprintf("%s:%d", cfg.Mail.SMTPRelay, cfg.Mail.SMTPPort)
   err = smtp.SendMail(relay, auth, cfg.Remailer.Address, sendTo, payload)
   if err != nil {
      Warn.Println(err)
      return
   }
   return
}

I am getting this error:
$ go build
.\mail.go:450:31: undefined: auth

Line 450 = smtp.SendMail… line.

The auth := was not wrapped in the if statement before, but I need to change the auth according to the if test. If you can show how to just change the last line in the auth only (cfg.Mail.SMTPRelay) to “” and back), that would work also.
Can some show how to get around this error?

Thanks

I tried this also, but got errors:

func sendmail(payload []byte, sendTo []string) (err error) {
	if cfg.Mail.SMTPAuth {
		goto L2
		}
		auth := smtp.PlainAuth(
		"",
		cfg.Mail.Username,
		cfg.Mail.Password,		
		cfg.Mail.SMTPRelay)	//not thru tor
		
		goto L3

L2:
		auth := smtp.PlainAuth(
		"",
		cfg.Mail.Username,
		cfg.Mail.Password,
		"")					//thru tor

L3:

   relay := fmt.Sprintf("%s:%d", cfg.Mail.SMTPRelay, cfg.Mail.SMTPPort)
   err = smtp.SendMail(relay, auth, cfg.Remailer.Address, sendTo, payload)
   if err != nil {
      Warn.Println(err)
      return
   }
   return
}

Error messages:
go build
.\mail.go:481:8: goto L2 jumps over declaration of auth at .\mail.go:483:8
.\mail.go:489:8: goto L3 jumps over declaration of auth at .\mail.go:492:8
.\mail.go:492:8: no new variables on left side of :=

Jumping over is exactly what I want to do. :frowning:

Declare the auth variable and then assign to it in the if statement.

var auth whatever

if cond {
  auth = foo
} else {
  auth = bar
}

Try this:

// sendmail invokes go's sendmail method
func sendmail(payload []byte, sendTo []string) (err error) {
	var auth smtp.Auth
	if cfg.Mail.SMTPAuth {
		auth = smtp.PlainAuth(
			"",
			cfg.Mail.Username,
			cfg.Mail.Password,
			cfg.Mail.SMTPRelay) //not thru tor
	} else {
		auth = smtp.PlainAuth(
			"",
			cfg.Mail.Username,
			cfg.Mail.Password,
			"") //thru tor
	}

	relay := fmt.Sprintf("%s:%d", cfg.Mail.SMTPRelay, cfg.Mail.SMTPPort)
	err = smtp.SendMail(relay, auth, cfg.Remailer.Address, sendTo, payload)
	if err != nil {
		Warn.Println(err)
		return
	}
	return
}

Try this:

// sendmail invokes go's sendmail method
func sendmail(payload []byte, sendTo []string) (err error) {
	host := "" // thru tor
	if cfg.Mail.SMTPAuth {
		host = cfg.Mail.SMTPRelay // not thru tor
	}
	auth := smtp.PlainAuth(
		"",
		cfg.Mail.Username,
		cfg.Mail.Password,
		host,
	)

	relay := fmt.Sprintf("%s:%d", cfg.Mail.SMTPRelay, cfg.Mail.SMTPPort)
	err = smtp.SendMail(relay, auth, cfg.Remailer.Address, sendTo, payload)
	if err != nil {
		Warn.Println(err)
		return
	}
	return
}

Getting this error on this one:

.\mail.go:449:5: syntax error: non-declaration statement outside function body
line 449 =

host := ""

This one worked /petrus - Thanks!

// sendmail invokes go's sendmail method
func sendmail(payload []byte, sendTo []string) (err error) {
	var auth smtp.Auth
	if cfg.Mail.SMTPAuth {
		auth = smtp.PlainAuth(
			"",
			cfg.Mail.Username,
			cfg.Mail.Password,
			cfg.Mail.SMTPRelay) //not thru tor
	} else {
		auth = smtp.PlainAuth(
			"",
			cfg.Mail.Username,
			cfg.Mail.Password,
			"") //thru tor
	}

	relay := fmt.Sprintf("%s:%d", cfg.Mail.SMTPRelay, cfg.Mail.SMTPPort)
	err = smtp.SendMail(relay, auth, cfg.Remailer.Address, sendTo, payload)
	if err != nil {
		Warn.Println(err)
		return
	}
	return
}