Created
April 12, 2018 11:59
-
-
Save jhinrichsen/d23c1641253a93e0b3404b5a97b1a1c8 to your computer and use it in GitHub Desktop.
Minimal email commandline client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"fmt" | |
"log" | |
"net/smtp" | |
"os" | |
"os/user" | |
) | |
func die(err error) { | |
if err != nil { | |
log.Fatal(err) | |
} | |
} | |
func main() { | |
user, err := user.Current() | |
die(err) | |
hostname, err := os.Hostname() | |
die(err) | |
// Connect to the remote SMTP server. | |
c, err := smtp.Dial("<your SMTP server in here>:25") | |
die(err) | |
defer c.Close() | |
// Set the sender and recipient. | |
recipient := fmt.Sprintf("%s@%s", user.Username, hostname) | |
if err := c.Mail(recipient); err != nil { | |
log.Fatal(err) | |
} | |
if err := c.Rcpt("<recipient in here>"); err != nil { | |
log.Fatal(err) | |
} | |
// Send the email body. | |
wc, err := c.Data() | |
die(err) | |
defer wc.Close() | |
buf := bytes.NewBufferString("This is the email body.") | |
if _, err = buf.WriteTo(wc); err != nil { | |
log.Fatal(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment