Skip to content

Instantly share code, notes, and snippets.

@alok87
Created October 21, 2015 10:10
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save alok87/56aaecb6c2e102bcf625 to your computer and use it in GitHub Desktop.
Save alok87/56aaecb6c2e102bcf625 to your computer and use it in GitHub Desktop.
Sendmail Using Golang without SMTP- Example
package main
import (
"io/ioutil"
"os/exec"
"fmt"
)
// EXAMPLE: echo "Subject: TestnHello" | sendmail -f you@domain.com you@domain.com
// Useful Links: https://gobyexample.com/spawning-processes
func main() {
fromEmail := "you@domain.com"
toEmail := "you@domain.com"
msg := "Subject: Sendmail Using Go"
sendmail := exec.Command("/usr/sbin/sendmail", "-f", fromEmail, toEmail)
stdin, err := sendmail.StdinPipe()
if err != nil {
panic(err)
}
stdout, err := sendmail.StdoutPipe()
if err != nil {
panic(err)
}
sendmail.Start()
stdin.Write([]byte(msg))
stdin.Close()
sentBytes, _ := ioutil.ReadAll(stdout)
sendmail.Wait()
fmt.Println("Send Command Output\n")
fmt.Println(string(sentBytes))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment