Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement