Skip to content

Instantly share code, notes, and snippets.

@Goodnessuc
Last active June 27, 2022 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Goodnessuc/5f78b9f523a8c9cae57887ae16f83e1e to your computer and use it in GitHub Desktop.
Save Goodnessuc/5f78b9f523a8c9cae57887ae16f83e1e to your computer and use it in GitHub Desktop.
Sending Emails in Go using any Mail Service
package main
import (
"crypto/tls"
"fmt"
"log"
"net/smtp"
)
func main() {
fmt.Print(SendMail())
}
func SendMail() string {
password := "your app password"
from := "your email"
to := "any email whatsoever"
host := "smtp.mail.yahoo.com"
port := "465"
subject := "Hey, I'm Just Checking On You."
body := "Hope you're doing okay!\n How are you doing today. "
headers := make(map[string]string)
headers["From"] = from
headers["To"] = to
headers["Subject"] = subject
message := ""
for k, v := range headers {
message += fmt.Sprintf("%s: %s\r\n", k, v)
}
message += "\r\n" + body
serverAddress := host + ":" + port
authenticate := smtp.PlainAuth("", from, password, host)
// TLS config
tlsConfig := &tls.Config{
InsecureSkipVerify: true,
ServerName: host,
}
conn, err := tls.Dial("tcp", serverAddress, tlsConfig)
if err != nil {
log.Panic(err)
}
c, err := smtp.NewClient(conn, host)
if err != nil {
log.Panic(err)
}
// Auth
if err = c.Auth(authenticate); err != nil {
log.Panic(err)
}
// To && From
if err = c.Mail(from); err != nil {
log.Panic(err)
}
if err = c.Rcpt(headers["To"]); err != nil {
log.Panic(err)
}
// Data
writer, err := c.Data()
if err != nil {
log.Panic(err)
}
_, err = writer.Write([]byte(message))
if err != nil {
log.Panic(err)
}
err = writer.Close()
if err != nil {
log.Panic(err)
}
err = c.Quit()
if err != nil {
return ""
}
return "Successful, the mail was sent!"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment