Sending a simple email
This recipe will give you a brief description on how to use the standard library to connect to the SMTP server and send an email.
Getting ready
In this recipe, we will use a Google Gmail account to send the email message. With a few configurations, this recipe will be useful for other SMTP servers as well.
How to do it...
- Open the console and create the folder
chapter07/recipe10
. - Navigate to the directory.
- Create the
smtp.go
file with the following content:
package main import ( "crypto/tls" "fmt" "net/smtp" ) func main() { var email string fmt.Println("Enter username for smtp: ") fmt.Scanln(&email) var pass string fmt.Println("Enter password for smtp: ") fmt.Scanln(&pass) auth := smtp.PlainAuth("", email, pass, "smtp.gmail.com") c, err := smtp.Dial("smtp.gmail.com:587") if err != nil { panic(err...