battles  
                (William Peterson)
               
                 
              
                  
                    September 21, 2020,  2:28pm
                   
                   
              1 
               
             
            
              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
             
            
               
               
               
            
            
           
          
            
              
                battles  
                (William Peterson)
               
              
                  
                    September 21, 2020,  3:50pm
                   
                   
              2 
               
             
            
              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. 
             
            
               
               
               
            
            
           
          
            
              
                NobbZ  
                (Norbert Melzer)
               
              
                  
                    September 21, 2020,  4:40pm
                   
                   
              3 
               
             
            
              Declare the auth variable and then assign to it in the if statement.
var auth whatever
if cond {
  auth = foo
} else {
  auth = bar
}
 
             
            
               
               
               
            
            
           
          
            
              
                petrus  
                (petrus)
               
              
                  
                    September 21, 2020,  4:47pm
                   
                   
              4 
               
             
            
              
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
}
 
             
            
               
               
               
            
            
           
          
            
              
                petrus  
                (petrus)
               
              
                  
                    September 21, 2020,  5:09pm
                   
                   
              5 
               
             
            
              
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
}
 
             
            
               
               
               
            
            
           
          
            
              
                battles  
                (William Peterson)
               
              
                  
                    September 21, 2020,  5:56pm
                   
                   
              6 
               
             
            
              Getting this error on this one:
.\mail.go:449:5: syntax error: non-declaration statement outside function body 
line 449 =
host := ""
 
             
            
               
               
               
            
            
           
          
            
              
                battles  
                (William Peterson)
               
              
                  
                    September 21, 2020,  5:59pm
                   
                   
              7 
               
             
            
              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
}
 
             
            
               
               
               
            
            
           
          
            
              
                system  
                (system)
                  Closed 
               
              
                  
                    December 20, 2020,  5:59pm
                   
                   
              8 
               
             
            
              This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.