Script GoLang SMTP without TLS, used to test Postal Mail Server
package main
import (
"crypto/tls"
"errors"
"log"
"net/smtp"
"gopkg.in/gomail.v2"
)
const CONFIG_SMTP_HOST = "<host>"
const CONFIG_SMTP_PORT = 587
const CONFIG_SENDER_NAME = "<[email protected]"
const CONFIG_AUTH_EMAIL = "<tokenname>"
const CONFIG_AUTH_PASSWORD = "<tokenkey>"
type loginAuth struct {
username, password string
}
func LoginAuth(username, password string) smtp.Auth {
return &loginAuth{username, password}
}
func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
return "LOGIN", []byte{}, nil
}
func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
if more {
switch string(fromServer) {
case "Username:":
return []byte(a.username), nil
case "Password:":
return []byte(a.password), nil
default:
return nil, errors.New("Unkown fromServer")
}
}
return nil, nil
}
func main() {
mailer := gomail.NewMessage()
mailer.SetHeader("From", CONFIG_SENDER_NAME)
mailer.SetHeader("To", "[email protected]")
mailer.SetHeader("Subject", "Test mail")
mailer.SetBody("text/html", "Hello, <b>have a nice day</b>")
dialer := gomail.NewDialer(
CONFIG_SMTP_HOST,
CONFIG_SMTP_PORT,
CONFIG_AUTH_EMAIL,
CONFIG_AUTH_PASSWORD,
)
dialer.TLSConfig = &tls.Config{InsecureSkipVerify: false}
// tweak tls
auth := LoginAuth(CONFIG_AUTH_EMAIL, CONFIG_AUTH_PASSWORD)
dialer.Auth = auth
err := dialer.DialAndSend(mailer)
if err != nil {
log.Fatal(err.Error())
}
log.Println("Mail sent!")
}