Add configurable SMTP server
This commit is contained in:
		@@ -41,7 +41,7 @@ func main() {
 | 
				
			|||||||
        os.Exit(1)
 | 
					        os.Exit(1)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    scsusers.Init(db, "test", "Example Test", "nobody@nowhere.com")
 | 
					    scsusers.Init(db, "test", "Example Test", "nobody@nowhere.com", "localhost:25")
 | 
				
			||||||
    a:=scsusers.UsernameAvailable("testuser")
 | 
					    a:=scsusers.UsernameAvailable("testuser")
 | 
				
			||||||
    fmt.Printf("Initial test of username available: %v\n", a)
 | 
					    fmt.Printf("Initial test of username available: %v\n", a)
 | 
				
			||||||
    a=scsusers.Register("testuser", email, "127.0.0.1")
 | 
					    a=scsusers.Register("testuser", email, "127.0.0.1")
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										10
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								main.go
									
									
									
									
									
								
							@@ -26,6 +26,7 @@ type config struct {
 | 
				
			|||||||
	SiteName    string
 | 
						SiteName    string
 | 
				
			||||||
	FromEmail   string
 | 
						FromEmail   string
 | 
				
			||||||
	Templates   templates
 | 
						Templates   templates
 | 
				
			||||||
 | 
						SMTPServer string
 | 
				
			||||||
	db          *sqlx.DB
 | 
						db          *sqlx.DB
 | 
				
			||||||
	TablePrefix string
 | 
						TablePrefix string
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -38,11 +39,12 @@ type UserData struct {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
var c config
 | 
					var c config
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func Init(dbin *sqlx.DB, tp, sitename, fromaddr string) {
 | 
					func Init(dbin *sqlx.DB, tp, sitename, fromaddr, smtpserver string) {
 | 
				
			||||||
	c.db = dbin
 | 
						c.db = dbin
 | 
				
			||||||
	c.TablePrefix = tp
 | 
						c.TablePrefix = tp
 | 
				
			||||||
	c.SiteName = sitename
 | 
						c.SiteName = sitename
 | 
				
			||||||
	c.FromEmail = fromaddr
 | 
						c.FromEmail = fromaddr
 | 
				
			||||||
 | 
						c.SMTPServer=smtpserver
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	SetRegistrationTemplate("")
 | 
						SetRegistrationTemplate("")
 | 
				
			||||||
	SetAlertTemplate("")
 | 
						SetAlertTemplate("")
 | 
				
			||||||
@@ -236,7 +238,7 @@ func sendRegistrationEmail(recipient, username, password string) bool {
 | 
				
			|||||||
		return false
 | 
							return false
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	subject := fmt.Sprintf("Welcome to %s", c.SiteName)
 | 
						subject := fmt.Sprintf("Welcome to %s", c.SiteName)
 | 
				
			||||||
	err = SendMail("localhost:25", c.FromEmail, subject, body.String(), recipient)
 | 
						err = SendMail(c.SMTPServer, c.FromEmail, subject, body.String(), recipient)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Printf("scsusers.SendRegistrationEmail: Error sending mail to %s: %s\n", recipient, err.Error())
 | 
							log.Printf("scsusers.SendRegistrationEmail: Error sending mail to %s: %s\n", recipient, err.Error())
 | 
				
			||||||
		return false
 | 
							return false
 | 
				
			||||||
@@ -265,7 +267,7 @@ func sendAlertEmail(username, recipient, message string) bool {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
	subject := fmt.Sprintf("New activity on %s", c.SiteName)
 | 
						subject := fmt.Sprintf("New activity on %s", c.SiteName)
 | 
				
			||||||
	err = SendMail("localhost:25", c.FromEmail, subject, body.String(), recipient)
 | 
						err = SendMail(c.SMTPServer,  c.FromEmail, subject, body.String(), recipient)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Printf("scsusers.sendAlertEmail: Error sending mail to %s: %s\n", recipient, err.Error())
 | 
							log.Printf("scsusers.sendAlertEmail: Error sending mail to %s: %s\n", recipient, err.Error())
 | 
				
			||||||
		return false
 | 
							return false
 | 
				
			||||||
@@ -292,7 +294,7 @@ func sendRecoveryEmail(recipient, username, code string) bool {
 | 
				
			|||||||
		return false
 | 
							return false
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	subject := fmt.Sprintf("Account recovery at %s", c.SiteName)
 | 
						subject := fmt.Sprintf("Account recovery at %s", c.SiteName)
 | 
				
			||||||
	err = SendMail("localhost:25", c.FromEmail, subject, body.String(), recipient)
 | 
						err = SendMail(c.SMTPServer, c.FromEmail, subject, body.String(), recipient)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Printf("scsusers.sendRecoveryEmail: Error sending mail to %s: %s\n", recipient, err.Error())
 | 
							log.Printf("scsusers.sendRecoveryEmail: Error sending mail to %s: %s\n", recipient, err.Error())
 | 
				
			||||||
		return false
 | 
							return false
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user