Send email

Send email using gomail library

Gomail library

This example uses the popular gomail.v2 library available @

Source Code

The full source code is available @

Code is pretty simple as shown below

// Dial to required smtp server
d := gomail.NewDialer("", 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

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