Skip to content

Commit

Permalink
Merge pull request #11 from StevenMaude/nada
Browse files Browse the repository at this point in the history
Switch service from Mailinator to nada
  • Loading branch information
StevenMaude authored Mar 25, 2018
2 parents b9836fb + 17437bd commit f4fb7d2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 97 deletions.
30 changes: 14 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,34 @@
# 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

`go build`

## 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.
`[email protected]`).
3. Run `go-mailin8 <local-part of email address>`

## 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 <email address>`

## 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).
124 changes: 43 additions & 81 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <local-part>")
fmt.Println("Usage: mailin8 <address>")
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)
Expand Down

0 comments on commit f4fb7d2

Please sign in to comment.