net/smtp is a built-in go package and implements SMTP protocol. It provides a simple way for sending mail through smtp servers. This package implements the Simple Mail Transfer Protocol.
Steps to send Email:
1. Get authentication from the Host server and establish a TLS connection to the host server with the PlainAuth function.
func PlainAuth(identity, username, password, host string) Auth
PlainAuth accepts four arguments of string type identity(It should be an empty string to act as username), the username(sender mail address), password (sender mail password), and port of SMTP server. PlainAuth returns an Auth, an implementation of an SMTP authentication mechanism. To authenticate to the host, the returned Auth uses the given username and password and acts as an identity.
2. Use the Auth obtained to send a mail with the SendMail function
func SendMail(addr string, a Auth, from string, to []string, msg []byte) error
SendMail function accepts five arguments. addr is of type string and contains an address and port number of the server (eg: “smtp.gmail.com:587”), a is Auth that we got from the PlainAuth function, from is of type string and contains sender mail address, to is a slice of string that contains receivers mail address and msg is a slice of the byte that contains the body of the mail.
Here we used the Gmail server to send mails. You can use a mail address with any domain, just change the host accordingly.
// Sending Email Using Smtp in Golang package main
import (
"fmt"
"net/smtp"
"os"
) // Main function func main() {
// from is senders email address
// we used environment variables to load the
// email address and the password from the shell
// you can also directly assign the email address
// and the password
from := os.Getenv( "MAIL" )
password := os.Getenv( "PASSWD" )
// toList is list of email address that email is to be sent.
toList := [] string { "example@gmail.com" }
// host is address of server that the
// sender's email address belongs,
// in this case its gmail.
// For e.g if your are using yahoo
// mail change the address as smtp.mail.yahoo.com
host := "smtp.gmail.com"
// Its the default port of smtp server
port := "587"
// This is the message to send in the mail
msg := "Hello geeks!!!"
// We can't send strings directly in mail,
// strings need to be converted into slice bytes
body := [] byte (msg)
// PlainAuth uses the given username and password to
// authenticate to host and act as identity.
// Usually identity should be the empty string,
// to act as username.
auth := smtp.PlainAuth( "" , from, password, host)
// SendMail uses TLS connection to send the mail
// The email is sent to all address in the toList,
// the body should be of type bytes, not strings
// This returns error if any occurred.
err := smtp.SendMail(host+ ":" +port, auth, from, toList, body)
// handling the errors
if err != nil {
fmt.Println(err)
os.Exit( 1 )
}
fmt.Println( "Successfully sent mail to all user in toList" )
} |
Output:
Successfully sent mail to all user in toList