Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/js/libs/smtp/msg.go
2070 views
1
package smtp
2
3
import (
4
"bufio"
5
"bytes"
6
"net/textproto"
7
"strings"
8
)
9
10
type (
11
// SMTPMessage is a message to be sent over SMTP
12
// @example
13
// ```javascript
14
// const smtp = require('nuclei/smtp');
15
// const message = new smtp.SMTPMessage();
16
// message.From('[email protected]');
17
// ```
18
SMTPMessage struct {
19
from string
20
to []string
21
sub string
22
msg []byte
23
user string
24
pass string
25
}
26
)
27
28
// From adds the from field to the message
29
// @example
30
// ```javascript
31
// const smtp = require('nuclei/smtp');
32
// const message = new smtp.SMTPMessage();
33
// message.From('[email protected]');
34
// ```
35
func (s *SMTPMessage) From(email string) *SMTPMessage {
36
s.from = email
37
return s
38
}
39
40
// To adds the to field to the message
41
// @example
42
// ```javascript
43
// const smtp = require('nuclei/smtp');
44
// const message = new smtp.SMTPMessage();
45
// message.To('[email protected]');
46
// ```
47
func (s *SMTPMessage) To(email string) *SMTPMessage {
48
s.to = append(s.to, email)
49
return s
50
}
51
52
// Subject adds the subject field to the message
53
// @example
54
// ```javascript
55
// const smtp = require('nuclei/smtp');
56
// const message = new smtp.SMTPMessage();
57
// message.Subject('hello');
58
// ```
59
func (s *SMTPMessage) Subject(sub string) *SMTPMessage {
60
s.sub = sub
61
return s
62
}
63
64
// Body adds the message body to the message
65
// @example
66
// ```javascript
67
// const smtp = require('nuclei/smtp');
68
// const message = new smtp.SMTPMessage();
69
// message.Body('hello');
70
// ```
71
func (s *SMTPMessage) Body(msg []byte) *SMTPMessage {
72
s.msg = msg
73
return s
74
}
75
76
// Auth when called authenticates using username and password before sending the message
77
// @example
78
// ```javascript
79
// const smtp = require('nuclei/smtp');
80
// const message = new smtp.SMTPMessage();
81
// message.Auth('username', 'password');
82
// ```
83
func (s *SMTPMessage) Auth(username, password string) *SMTPMessage {
84
s.user = username
85
s.pass = password
86
return s
87
}
88
89
// String returns the string representation of the message
90
// @example
91
// ```javascript
92
// const smtp = require('nuclei/smtp');
93
// const message = new smtp.SMTPMessage();
94
// message.From('[email protected]');
95
// message.To('[email protected]');
96
// message.Subject('hello');
97
// message.Body('hello');
98
// log(message.String());
99
// ```
100
func (s *SMTPMessage) String() string {
101
var buff bytes.Buffer
102
tw := textproto.NewWriter(bufio.NewWriter(&buff))
103
_ = tw.PrintfLine("To: %s", strings.Join(s.to, ","))
104
if s.sub != "" {
105
_ = tw.PrintfLine("Subject: %s", s.sub)
106
}
107
_ = tw.PrintfLine("\r\n%s", s.msg)
108
return buff.String()
109
}
110
111