diff --git a/README.md b/README.md index f170ad5..24bfe5c 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,22 @@ # go-mailin8 -Display the latest mail from a temporary Mailinator email address, at -the command line. +Display the latest mail from a temporary [nada](https://getnada.com) +email address, at the command line. Useful for getting confirmation emails for throwaway purposes, or for testing. -This is a version of a [bash script I +## History (or "why is this called go-mailin8?") + +Originally, this used Mailinator's service to retrieve email; however, +they rebuilt their site and this became a WebSocket powered application, +making it more difficult to work with. + +This was a version of a [bash script I made](https://gist.github.com/StevenMaude/914e9187c09027866fe88958798acb7e) that uses [jq](https://stedolan.github.io/jq/). However, this Go version -requires no other dependencies. +requires no other dependencies, and now uses a different disposable +email service. ## Build @@ -17,20 +24,11 @@ requires no other dependencies. ## Usage -1. Send, or get an email sent to a Mailinator email address of your - choosing. -2. Note the local-part (the part before @, e.g. - `somefakeemail1234@mailinator.com`). -3. Run `go-mailin8 ` - -## Notes - -NB: you may get unpredictable results from the server if you run this -too often in a short time. See TODO. +1. Send, or get an email sent to a [nada](https://getnada.com) email + address of your choosing. +2. Run `go-mailin8 ` ## TODO: -* Consider introducing slight pauses between requests to try and - improve reliability. * Possibly consider selecting other than latest message (though outside of my original use case). diff --git a/main.go b/main.go index eb25b6b..e7c9d05 100644 --- a/main.go +++ b/main.go @@ -8,127 +8,89 @@ import ( ) type mail struct { - Data struct { - Subject string `json:"subject"` - Parts []struct { - Body string `json:"body"` - } `json:"parts"` - Headers struct { - From string `json:"from"` - } `json:"headers"` - } `json:"data"` + From string `json:"f"` + Subject string `json:"s"` + HTML string `json:"html"` + Text string `json:"text"` } -type publicMsg struct { - ID string `json:"id"` - To string `json:"to"` +type msg struct { + UID string `json:"uid"` } -type mailboxDetails struct { - PublicMsgs []publicMsg `json:"messages"` +type inbox struct { + Msgs []msg `json:"msgs"` } -func getMailboxDetails(localPart string) (mailboxDetails, error) { - webInboxURL := "https://www.mailinator.com/fetch_inbox?zone=public&to=" + localPart - fmt.Println("Retrieving URL:", webInboxURL) +func getMail(latestMsg msg) error { + msgURL := "https://getnada.com/api/v1/messages/" + latestMsg.UID + fmt.Println("Retrieving URL:", msgURL) - mbxDetails := mailboxDetails{} - resp, err := http.Get(webInboxURL) + resp, err := http.Get(msgURL) if err != nil { - return mbxDetails, err + return err } + // TODO: move out display of mail from getting mail. defer resp.Body.Close() - err = json.NewDecoder(resp.Body).Decode(&mbxDetails) - // No need for error check here as we return mbxDetails and err whether - // we have an error or not. - return mbxDetails, err -} - -func getCookies(latestMsg publicMsg) ([]*http.Cookie, error) { - // This request is for nothing but getting required cookies. - // Otherwise, the subsequent request fails. - inboxURL := "https://www.mailinator.com/inbox2.jsp?to=" + latestMsg.To - fmt.Println("Retrieving URL:", inboxURL) - - inboxResp, err := http.Get(inboxURL) - defer inboxResp.Body.Close() - if err != nil { - return nil, err - } - - cookies := inboxResp.Cookies() - return cookies, err -} -func getMail(latestMsg publicMsg, cookies []*http.Cookie) error { - msgURL := "https://www.mailinator.com/fetch_email?zone=public&msgid=" + latestMsg.ID - fmt.Println("Retrieving URL:", msgURL) - req, err := http.NewRequest("GET", msgURL, nil) + mailMessage := mail{} + err = json.NewDecoder(resp.Body).Decode(&mailMessage) if err != nil { return err } - for _, c := range cookies { - req.AddCookie(c) - } - - client := &http.Client{} + fmt.Println("\nFrom :", mailMessage.From) + fmt.Println("Subject:", mailMessage.Subject) + fmt.Println("Plain text:") + fmt.Println(mailMessage.Text) - mailResp, err := client.Do(req) - defer mailResp.Body.Close() - if err != nil { - return err - } + fmt.Println("HTML:") + fmt.Println(mailMessage.HTML) - mailMessage := mail{} - err = json.NewDecoder(mailResp.Body).Decode(&mailMessage) - if err != nil { - return err - } + return nil +} - fmt.Println("\nFrom :", mailMessage.Data.Headers.From) - fmt.Println("Subject:", mailMessage.Data.Subject) - fmt.Println("Plain text:") - fmt.Println(mailMessage.Data.Parts[0].Body) +func getInbox(address string) (inbox, error) { + webInboxURL := "https://getnada.com/api/v1/inboxes/" + address + fmt.Println("Retrieving URL:", webInboxURL) - if len(mailMessage.Data.Parts) == 2 { - fmt.Println("HTML:") - fmt.Println(mailMessage.Data.Parts[1].Body) + addressInbox := inbox{} + resp, err := http.Get(webInboxURL) + if err != nil { + return addressInbox, err } - return nil + defer resp.Body.Close() + err = json.NewDecoder(resp.Body).Decode(&addressInbox) + // No need for error check here as we return mbxDetails and err whether + // we have an error or not. + return addressInbox, err } func main() { // TODO: consider allow to retrieve more than one message. if len(os.Args) != 2 { - fmt.Println("Usage: mailin8 ") + fmt.Println("Usage: mailin8
") os.Exit(1) } - localPart := os.Args[1] - mbxDetails, err := getMailboxDetails(localPart) + address := os.Args[1] + addressInbox, err := getInbox(address) if err != nil { fmt.Println("failed to get message ID:", err) os.Exit(1) } - numberMsgs := len(mbxDetails.PublicMsgs) + numberMsgs := len(addressInbox.Msgs) if numberMsgs == 0 { fmt.Println("no messages in inbox") os.Exit(0) } + fmt.Println("Found", numberMsgs, "messages") - latestMsg := mbxDetails.PublicMsgs[numberMsgs-1] - - cookies, err := getCookies(latestMsg) - if err != nil { - fmt.Println("failed to get cookies:", err) - os.Exit(1) - } - - err = getMail(latestMsg, cookies) + latestMsg := addressInbox.Msgs[0] + err = getMail(latestMsg) if err != nil { fmt.Println("failed to get mail:", err) os.Exit(1)