Skip to content

Instantly share code, notes, and snippets.

@tanelmae
Created April 26, 2022 18:13
Show Gist options
  • Save tanelmae/2480295319f189bcfbe0ce64960afd9f to your computer and use it in GitHub Desktop.
Save tanelmae/2480295319f189bcfbe0ce64960afd9f to your computer and use it in GitHub Desktop.
Send email with Go
package main
import (
"flag"
"fmt"
"log"
"net/smtp"
)
func main() {
username := flag.String("username", "", "Username in the smtp server")
password := flag.String("password", "", "Password in the smtp server")
smtpHost := flag.String("host", "", "SMTP server host")
smtpPort := flag.Int("port", 587, "SMTP server port")
toAddr := flag.String("to", "", "Receiver address")
from := flag.String("from", "", "Sender address")
subject := flag.String("subject", "", "Email subject")
messageText := flag.String("message", "", "Email content")
flag.Parse()
msg := []byte("From: " + *from + "\r\n" +
"To: " + *toAddr + "\r\n" +
"Subject: " + *subject + "\r\n\r\n" +
*messageText + "\r\n")
auth := smtp.PlainAuth(*from, *username, *password, *smtpHost)
err := smtp.SendMail(
fmt.Sprintf("%s:%d", *smtpHost, *smtpPort), auth, *from, []string{*toAddr}, msg)
if err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment