Skip to content

Instantly share code, notes, and snippets.

@fujiwara
Created December 11, 2017 04:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fujiwara/223f2bf9c21f3be04ef647cc14f891b1 to your computer and use it in GitHub Desktop.
Save fujiwara/223f2bf9c21f3be04ef647cc14f891b1 to your computer and use it in GitHub Desktop.
mail2line
package main
import (
"fmt"
"mime"
"net/http"
"net/mail"
"net/url"
"os"
"strings"
)
func main() {
text := ""
msg, err := mail.ReadMessage(os.Stdin)
if err != nil {
panic(err)
}
addrs, err := msg.Header.AddressList("From")
if err != nil {
panic(err)
}
if len(addrs) >= 0 {
text += addrs[0].Name + " " + addrs[0].Address + " から"
}
subject := msg.Header.Get("Subject")
if subject != "" {
dec := new(mime.WordDecoder)
s, err := dec.DecodeHeader(subject)
if err != nil {
panic(err)
}
text += s + " が届きました"
}
v := url.Values{"message": {text}}
req, err := http.NewRequest(
"POST",
"https://notify-api.line.me/api/notify",
strings.NewReader(v.Encode()),
)
if err != nil {
panic(err)
}
req.Header.Set("Authorization", "Bearer "+os.Getenv("TOKEN"))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
fmt.Println(resp.Status)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment