-
-
Save sector84/8ef4fce83a501a8dcdead2ed16b5ccfa to your computer and use it in GitHub Desktop.
Sendmail Using Golang without SMTP- Example
This file contains hidden or 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 ( | |
| "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