xxxxxxxxxx
// 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")
}
xxxxxxxxxx
func SmtpEmail(to []string, subject, template string) error {
smtp_host := viper.GetString("SMTP_HOST")
smtp_port, _ := strconv.Atoi(viper.GetString("SMTP_PORT"))
smtp_username := viper.GetString("SMTP_USERNAME")
smtp_password := viper.GetString("SMTP_PASSWORD")
smtpAuth := smtp.PlainAuth("", smtp_username, smtp_password, smtp_host)
smtpAddress := fmt.Sprintf("%s:%d", smtp_host, smtp_port)
smtpFromEmail := viper.GetString("SMTP_EMAIL")
smtpEmailError := smtp.SendMail(smtpAddress, smtpAuth, smtpFromEmail, to, smtpEmailMetadata(subject, template))
if smtpEmailError != nil {
logrus.Errorf("Sending email using SMTP error: %v", smtpEmailError)
}
return smtpEmailError
}
func smtpEmailMetadata(from string, to []string, subject string, template string) []byte {
mimeType := "Content-Type: text/html; \n"
fromEmail := "From: " + from + "\n"
toEmail := "To: " + strings.Join(to, ",") + "\n"
subjecEmail := "Subject: " + subject + "\n"
bodyEmail := []byte(fromEmail + toEmail + subjecEmail + mimeType + "\n" + template)
return bodyEmail
}