Skip to content

Instantly share code, notes, and snippets.

@defp
Created September 15, 2014 16:39
Show Gist options
  • Save defp/af7725bd1dd7392b31b2 to your computer and use it in GitHub Desktop.
Save defp/af7725bd1dd7392b31b2 to your computer and use it in GitHub Desktop.
zabbix smtp send email
package main
import (
"encoding/base64"
"fmt"
"net/mail"
"net/smtp"
"os"
"strings"
)
func encodeRFC2047(String string) string {
// use mail's rfc2047 to encode any string
addr := mail.Address{String, ""}
return strings.Trim(addr.String(), " <>")
}
func main() {
from := mail.Address{"监控邮件", "[email protected]"}
to := mail.Address{"收件人", os.Args[1]}
subject := os.Args[2]
body := os.Args[3]
header := make(map[string]string)
header["From"] = from.String()
header["To"] = to.String()
header["Subject"] = encodeRFC2047(subject)
header["MIME-Version"] = "1.0"
header["Content-Type"] = "text/plain; charset=\"utf-8\""
header["Content-Transfer-Encoding"] = "base64"
message := ""
for k, v := range header {
message += fmt.Sprintf("%s: %s\r\n", k, v)
}
message += "\r\n" + base64.StdEncoding.EncodeToString([]byte(body))
addr := "smtp.mst365.cn:25"
auth := smtp.PlainAuth("", "[email protected]", "456", "smtp.mst365.cn")
err := smtp.SendMail(addr, auth, from.Address, []string{to.Address}, []byte(message))
if err != nil {
panic(err)
}
}
@zgordan-vv
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment