Golang net/smtp.SendMail() function example

package net/smtp

Golang net/smtp.SendMail() function usage example

 // authentication configuration
 smtpHost := "smtp.****" // change to your SMTP provider address
 smtpPort := *** // change to your SMTP provider port number
 smtpPass := "yourpassword" // change here
 smtpUser := "yourusername@gmail.com" // change here

 emailConf := &EmailConfig{smtpUser, smtpPass, smtpHost, smtpPort}
 emailauth := smtp.PlainAuth("", emailConf.Username, emailConf.Password, emailConf.Host)
 sender := "fromwho@gmail.com" // change here

 receivers := []string{
 "someone@somedomain.com", 
 "someone@somedomain.com"
 } // change here

 message := []byte("Hello from Go SMTP package!") // your message

 // send out the email
 err := smtp.SendMail(smtpHost+":"+strconv.Itoa(emailConf.Port), //convert port number from int to string
 emailauth,
 sender,
 receivers,
 message,
 )

 if err != nil {
 fmt.Println(err)
 }

See full example at https://www.socketloop.com/tutorials/golang-send-email-and-smtp-configuration-example

References :

http://golang.org/pkg/net/smtp/#SendMail

https://www.socketloop.com/tutorials/golang-send-email-and-smtp-configuration-example

Advertisement