-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmail.go
103 lines (88 loc) · 2.27 KB
/
mail.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/google/uuid"
"math/rand"
"net/http"
"strings"
"time"
)
const (
BASE_URL = "https://www.1secmail.com/api/v1/"
CHECK_EMAIL = BASE_URL + "?action=getMessages&login=%s&domain=%s"
READ_EMAIL = BASE_URL + "?action=readMessage&login=%s&domain=%s&id=%d"
)
type Mail struct {
Id int `json:"id"`
From string `json:"from"`
Subject string `json:"subject"`
Date string `json:"date"`
}
type Attachment struct {
Filename string `json:"filename"`
Size int `json:"size"`
Type string `json:"contentType"`
}
type MailBody struct {
Mail
Attachments []Attachment `json:"attachments"`
Body string `json:"body"`
TextBody string `json:"textBody"`
HtmlBody string `json:"htmlBody"`
}
var Domains = []string{
"1secmail.com",
"1secmail.net",
"1secmail.org",
"kzcvv.com",
"qiott.com",
"wuuvo.com",
"icznn.com",
"ezztt.com",
}
func GetEmail() string {
return uuid.New().String() + "@" + Domains[rand.Intn(len(Domains))]
}
func GetMessages(email string) []*Mail {
var mails []*Mail
req, err := http.NewRequest("GET", fmt.Sprintf(CHECK_EMAIL, strings.Split(email, "@")[0], strings.Split(email, "@")[1]), nil)
if err != nil {
return mails
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return mails
}
err = json.NewDecoder(resp.Body).Decode(&mails)
if err != nil {
return mails
}
return mails
}
// ReadMessage will read the first message that matches the given function
// If no messages match the function, it will wait a second and try again
// This mean that this function will block until a message is found
func ReadMessage(ctx context.Context, t time.Duration, email string, fn func(*Mail) bool) *MailBody {
timeout, cancel := context.WithTimeout(ctx, t)
defer cancel()
for {
select {
case <-timeout.Done():
return nil
default:
mails := GetMessages(email)
for _, mail := range mails {
if fn(mail) {
req, _ := http.NewRequest("GET", fmt.Sprintf(READ_EMAIL, strings.Split(email, "@")[0], strings.Split(email, "@")[1], mail.Id), nil)
resp, _ := http.DefaultClient.Do(req)
var mailBody *MailBody
json.NewDecoder(resp.Body).Decode(&mailBody)
return mailBody
}
}
time.Sleep(time.Second)
}
}
}