-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
121 lines (100 loc) · 3.19 KB
/
main.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"bufio"
"bytes"
"encoding/base64"
"fmt"
"mime/multipart"
"net/http"
"net/smtp"
"os"
"path/filepath"
"strings"
)
var (
host = "smtp.gmail.com"
username = ""
password = ""
portNumber = "587"
filePath = ""
)
type Message struct {
To []string
Subject string
Body string
Attachments map[string][]byte
}
func NewMessage(s, b string) *Message {
return &Message{Subject: s, Body: b, Attachments: make(map[string][]byte)}
}
func (m *Message) AttachFile(src string) error {
b, err := os.ReadFile(src)
if err != nil {
return err
}
_, fileName := filepath.Split(src)
m.Attachments[fileName] = b
return nil
}
func (m *Message) ToBytes() []byte {
buf := bytes.NewBuffer(nil)
withAttachments := len(m.Attachments) > 0
buf.WriteString(fmt.Sprintf("Subject: %s\n", m.Subject))
buf.WriteString(fmt.Sprintf("To: %s\n", strings.Join(m.To, ",")))
buf.WriteString("MIME-Version: 1.0\n")
writer := multipart.NewWriter(buf)
boundary := writer.Boundary()
if withAttachments {
buf.WriteString(fmt.Sprintf("Content-Type: multipart/mixed; boundary=%s\n", boundary))
buf.WriteString(fmt.Sprintf("--%s\n", boundary))
} else {
buf.WriteString("Content-Type: text/plain; charset=utf-8\n")
}
buf.WriteString(m.Body)
if withAttachments {
for k, v := range m.Attachments {
buf.WriteString(fmt.Sprintf("\n\n--%s\n", boundary))
buf.WriteString(fmt.Sprintf("Content-Type: %s\n", http.DetectContentType(v)))
buf.WriteString("Content-Transfer-Encoding: base64\n")
buf.WriteString(fmt.Sprintf("Content-Disposition: attachment; filename=%s\n", k))
b := make([]byte, base64.StdEncoding.EncodedLen(len(v)))
base64.StdEncoding.Encode(b, v)
buf.Write(b)
buf.WriteString(fmt.Sprintf("\n--%s", boundary))
}
buf.WriteString("--")
}
return buf.Bytes()
}
func main() {
reader := bufio.NewReader(os.Stdin)
for {
// Get email address
fmt.Print("Enter email address (or 'exit'): ")
email, _ := reader.ReadString('\n')
email = strings.TrimSpace(email)
// Check if the user wants to exit
if strings.ToLower(email) == "exit" {
fmt.Println("Exiting program.")
break
}
// Create a new message
m := NewMessage("Application for Java Developer Position", "Hello,\n\nI recently came across your LinkedIn post regarding the Java developer position and am very interested in this opportunity. Attached, please find my CV for your review. I believe my skills and experience make me a strong candidate for this role.\n\nThank you for considering my application. I look forward to the possibility of discussing how I can contribute to your team.\n\nBest regards,\nBart")
m.To = []string{email}
// Attach the file and handle errors
err := m.AttachFile(filePath)
if err != nil {
fmt.Printf("Failed to attach file: %v\n", err)
continue // Skip sending the email if file attachment fails
}
// Set up the SMTP authentication
auth := smtp.PlainAuth("", username, password, host)
// Send the email and handle errors
err = smtp.SendMail(fmt.Sprintf("%s:%s", host, portNumber), auth, username, m.To, m.ToBytes())
if err != nil {
fmt.Printf("Failed to send email: %v\n", err)
} else {
fmt.Println("Email sent successfully!")
}
}
}