Send email


Send email using gomail library

Gomail library

This example uses the popular gomail.v2 library available @ https://gopkg.in/gomail.v2

Source Code

The full source code is available @ https://github.com/premprakashp/go-smtp-email

Code is pretty simple as shown below

// Dial to required smtp server
d := gomail.NewDialer("smtp.gmail.com", 587, "username", "password")

// configure TLS insecure if using local tls servers without valid certs for testing
//d.TLSConfig = &tls.Config{InsecureSkipVerify: true}

// construct a new message
m := gomail.NewMessage()


m.SetHeader("From", "[email protected]") // set header

m.SetHeader("To", "[email protected]", "[email protected]") // set to header

m.SetHeader("Cc", "[email protected]", "[email protected]") // set cc header can set bcc similary

m.SetHeader("Subject", "Email Subject text") // email subject

/*
	Use text/plain for text email
*/
m.SetBody("text/html", "Hello <b>HTML email</b> body") // email 

/*
	Attachments if required
*/
m.Attach("/path_to_local_file_on_server")

// Dial address and send email
if err := d.DialAndSend(m); err != nil { // finally send email
	panic(err)
}