Skip to content

Instantly share code, notes, and snippets.

@alok87
Created October 21, 2015 10:10
Show Gist options
  • 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 [email protected] [email protected]
// Useful Links: https://gobyexample.com/spawning-processes
func main() {
fromEmail := "[email protected]"
toEmail := "[email protected]"
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