Created
September 15, 2014 16:39
-
-
Save defp/af7725bd1dd7392b31b2 to your computer and use it in GitHub Desktop.
zabbix smtp send email
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 ( | |
"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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I tried that but getting bellow error:
panic: x509: certificate has expired or is not yet valid
goroutine 1 [running]:
main.main()
/home/mujibur/zabbix_smtp.go:44 +0x9b4
goroutine 17 [syscall, locked to thread]:
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:2232 +0x1
exit status 2