-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e5d5628
commit a697f75
Showing
12 changed files
with
287 additions
and
305 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,55 @@ | ||
# mailtm | ||
|
||
The `mailtm` module wraps the [Mail.tm API](https://api.mail.tm) and provides full functionality. | ||
The `mailtm` module wraps the [Mail.tm API](https://api.mail.tm) and provides full functionality. | ||
|
||
This is the seconds version. It solves the login problem from version 1 and adds a login with existing credentials. | ||
|
||
If you like it, please consider giving it a :star: | ||
|
||
###### Install | ||
### Install | ||
|
||
`go get github.com/felixstrobel/mailtm` | ||
|
||
###### Documentation | ||
### Documentation | ||
|
||
Firstly, create an `MailClient` object. This is the object that communicates with the API and contains all important | ||
information. | ||
Firstly, create an `MailClient` object. This is the object that communicates with the API. | ||
```go | ||
import "github.com/felixstrobel/mailtm" | ||
|
||
func main() { | ||
client, err := mailtm.NewMailClient() | ||
client, err := mailtm.New() | ||
} | ||
``` | ||
|
||
After that you can either create an account: | ||
After that you can directly create an account: | ||
```go | ||
client.GetDomains() | ||
client.CreateAccount() | ||
// With random password | ||
client.NewAccount() | ||
|
||
// With custom password | ||
client.NewAccountWithPassword("password") | ||
``` | ||
You have to fetch the available domains before creating an account (see above). The created account can be accessed through `client.Account`. The account's address and password are randomized. | ||
|
||
If you already have an account and know the address and password, you can skip this step and go on with fetching the JWT token. | ||
If you already have an account and know the address and password, you can simply sign back in: | ||
```go | ||
// if you don't have valid credentials: | ||
client.GetAuthToken() | ||
|
||
// if you have valid credentials: | ||
client.GetAuthTokenCredentials(yourAddress, yourPassword) | ||
client.RetrieveAccount("your@email.com", "your_password") | ||
``` | ||
--- | ||
You are authenticated now! You can begin... | ||
|
||
...fetching your messages: | ||
##### Fetching your messages of the first page (around 30 messages): | ||
```go | ||
messages, err := client.GetMessages() | ||
client.GetMessages(&account, 1) | ||
``` | ||
...get details of a message: | ||
##### Get detailed message: | ||
```go | ||
message, err := client.GetMessageByID(messages[0].ID) | ||
client.GetMessageByID(&account, "the_id_of_the_message") | ||
``` | ||
...delete one: | ||
#### Delete a message: | ||
```go | ||
client.DeleteMessageByID(message.ID) | ||
client.DeleteMessageByID(&account, "the_id_of_the_message") | ||
``` | ||
...or update the seen status to true: | ||
#### Mark a message as seen: | ||
```go | ||
client.SeenMessageByID(message.ID) | ||
client.SeenMessageByID(&account, "the_id_of_the_message") | ||
``` | ||
--- | ||
If you decide to delete an account, you can use the `DeleteAccountByID` function to do so: | ||
If you decide to delete an account, you can use the `DeleteAccount` function to do so: | ||
```go | ||
client.DeleteAccountByID(c.Account.ID) | ||
client.DeleteAccount(&account) | ||
``` |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package mailtm | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
type Domain struct { | ||
ID string `json:"id"` | ||
TLD string `json:"domain"` | ||
IsActive bool `json:"isActive"` | ||
IsPrivate bool `json:"isPrivate"` | ||
CreatedAt time.Time `json:"createdAt"` | ||
UpdatedAt time.Time `json:"updatedAt"` | ||
} | ||
|
||
func (c *MailClient) GetDomains() ([]Domain, error) { | ||
var response []Domain | ||
|
||
req, err := http.NewRequest("GET", string(c.service)+"/domains", nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
req.Header.Add("Accept", "application/json") | ||
res, err := c.http.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
body, err := io.ReadAll(res.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = json.Unmarshal(body, &response) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = res.Body.Close() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return response, nil | ||
} | ||
|
||
func (c *MailClient) GetDomainByID(id string) (*Domain, error) { | ||
var response Domain | ||
|
||
req, err := http.NewRequest("GET", string(c.service)+"/domains/"+id, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
req.Header.Add("Accept", "application/json") | ||
res, err := c.http.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
body, err := io.ReadAll(res.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = json.Unmarshal(body, &response) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = res.Body.Close() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &response, nil | ||
} |
Oops, something went wrong.