Skip to content

Instantly share code, notes, and snippets.

@darookee
Created April 22, 2016 09:34
Show Gist options
  • Select an option

  • Save darookee/a8f8ff6545d76b507bb650d6c674fa3a to your computer and use it in GitHub Desktop.

Select an option

Save darookee/a8f8ff6545d76b507bb650d6c674fa3a to your computer and use it in GitHub Desktop.
mail.go - test you mailserver sender restrictions (and other things) without the need of configuring a mail client, and without the hassle of a telnet-transaction...
package main
import (
"log"
"net/smtp"
"flag"
"fmt"
"os"
)
func main() {
// ${0} [flags] <sender> <recipient>
// flags and arguments
server := flag.String("server", "mail.example.com", "The SMTP server to test.")
serverPort := flag.Int("port", 25, "The SMTP port to use")
username := flag.String("user", "", "The SMTP username")
password := flag.String("password", "", "The SMTP user's password")
flag.Parse()
args := flag.Args()
if len(args) < 2 {
fmt.Fprint(os.Stderr, "Usage: mail [flags] <sender> <recipient>")
os.Exit(1)
}
// Set up authentication information.
auth := smtp.PlainAuth(
"",
*username,
*password,
*server,
)
// Connect to the server, authenticate, set the sender and recipient,
// and send the email all in one step.
err := smtp.SendMail(
fmt.Sprint(*server, ":", *serverPort),
auth,
args[0],
[]string{args[1]},
[]byte("This is a test mail."),
)
if err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment