-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailer.go
81 lines (68 loc) · 1.98 KB
/
mailer.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
package main
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"time"
"github.com/gosuri/uilive"
"github.com/mailgun/mailgun-go/v4"
"golang.org/x/net/context"
)
func main() {
// Load subject and body from files
subject, err := ioutil.ReadFile("subject.txt")
if err != nil {
log.Fatalf("Failed to read subject.txt: %v", err)
}
body, err := ioutil.ReadFile("body.html")
if err != nil {
log.Fatalf("Failed to read body.html: %v", err)
}
// Open the recipients list
file, err := os.Open("recipients.txt")
if err != nil {
log.Fatalf("Failed to open recipients.txt: %v", err)
}
defer file.Close()
// Initialize Mailgun
domain := "your-mailgun-domain.com"
apiKey := "your-api-key-here"
mg := mailgun.NewMailgun(domain, apiKey)
// Set up a live writer to update the terminal UI
writer := uilive.New()
writer.Start()
scanner := bufio.NewScanner(file)
sentCount := 0
startTime := time.Now()
for scanner.Scan() {
email := strings.TrimSpace(scanner.Text())
message := mg.NewMessage(
"[email protected]", // Sender
string(subject), // Subject
"", // Plaintext body
email, // Recipient
)
message.SetHtml(string(body))
// Send the email
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
_, _, err := mg.Send(ctx, message)
if err != nil {
fmt.Fprintf(writer, "failed to send to %s: %v\n", email, err)
} else {
sentCount++
timestamp := time.Now().Format("15:04:05")
fmt.Fprintf(writer, "[%s] sent to: %s | total sent: %d\n", timestamp, email, sentCount)
}
time.Sleep(2100 * time.Millisecond) // Sleeps for 2.1 seconds
}
if err := scanner.Err(); err != nil {
log.Fatalf("Failed to read recipients list: %v", err)
}
writer.Stop() // Stop the live writer
elapsedTime := time.Since(startTime)
fmt.Printf("Finished sending emails. Total sent: %d. Time elapsed: %s\n", sentCount, elapsedTime)
}