Skip to content

Commit 85052d3

Browse files
author
frederic.leist
committed
#7 added docblock
1 parent 477a1c4 commit 85052d3

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

mail.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ type mailer struct {
4444
config *smtpConfig
4545
}
4646

47+
// New returns a new Mailer instance
48+
//
49+
// Returns an error, if opts could not be validated
4750
func New(opts MailerOpts) (*mailer, error) {
4851
if err := validateMailerOpts(opts); err != nil {
4952
return nil, err
@@ -65,6 +68,11 @@ func New(opts MailerOpts) (*mailer, error) {
6568
return m, nil
6669
}
6770

71+
// validateMailerOpts returns an error if
72+
//
73+
// - [MailerOpts.User] is empty
74+
// - [MailerOpts.Password] is empty
75+
// - [MailerOpts.Host] is empty
6876
func validateMailerOpts(opts MailerOpts) error {
6977
if opts.User == "" {
7078
return fmt.Errorf("MailerOpts.User is empty")
@@ -78,24 +86,29 @@ func validateMailerOpts(opts MailerOpts) error {
7886
return nil
7987
}
8088

89+
// Send sends the message with [net/smtp.SendMail]
8190
func (m *mailer) Send() error {
8291
return smtp.SendMail(m.config.addr, m.config.auth, m.config.user, m.message.To(), m.writeMessage())
8392
}
8493

94+
// SetMessage sets the message
8595
func (m *mailer) SetMessage(msg Message) *mailer {
8696
m.message = msg
8797
return m
8898
}
8999

100+
// SetBoundary sets the boundary string
90101
func (m *mailer) SetBoundary(boundary string) *mailer {
91102
m.boundary = boundary
92103
return m
93104
}
94105

106+
// Boundary returns the boundary string
95107
func (m *mailer) Boundary() string {
96108
return m.boundary
97109
}
98110

111+
// Config returns the SMTP Config
99112
func (m *mailer) Config() *smtpConfig {
100113
return m.config
101114
}
@@ -123,6 +136,7 @@ func (m *mailer) chunkString(s string) string {
123136
return strings.Join(chunks, "\n")
124137
}
125138

139+
// writeMessage writes the message
126140
func (m *mailer) writeMessage() []byte {
127141
msg := m.message
128142
buf := bytes.NewBuffer(nil)

message.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,45 +37,59 @@ type message struct {
3737
attachments map[string][]byte
3838
}
3939

40+
// SetFrom sets the sender email.
4041
func (m *message) SetFrom(from string) {
4142
m.from = from
4243
}
44+
45+
// From returns the sender email.
4346
func (m *message) From() string {
4447
return m.from
4548
}
4649

50+
// SetTo sets the receiver addresses.
4751
func (m *message) SetTo(to ...string) {
4852
m.to = to
4953
}
5054

55+
// To returns receivers.
5156
func (m *message) To() []string {
5257
return m.to
5358
}
5459

60+
// SetCC sets the CC recipients.
5561
func (m *message) SetCC(cc ...string) {
5662
m.cc = cc
5763
}
5864

65+
// CC returns the CC recipients.
5966
func (m *message) CC() []string {
6067
return m.cc
6168
}
6269

70+
// SetBCC sets the BCC recipients.
6371
func (m *message) SetBCC(bcc ...string) {
6472
m.bcc = bcc
6573
}
6674

75+
// BCC returns the BCC recipients.
6776
func (m *message) BCC() []string {
6877
return m.bcc
6978
}
7079

80+
// SetSubject sets the subject.
7181
func (m *message) SetSubject(subject string) {
7282
m.subject = subject
7383
}
7484

85+
// Subject returns the Subject.
7586
func (m *message) Subject() string {
7687
return m.subject
7788
}
7889

90+
// Attach attaches files
91+
//
92+
// Returns an error if one of files could not be read.
7993
func (m *message) Attach(files ...string) error {
8094
for _, file := range files {
8195
b, err := os.ReadFile(file)
@@ -89,36 +103,46 @@ func (m *message) Attach(files ...string) error {
89103
return nil
90104
}
91105

106+
// Attachments returns the attachments.
92107
func (m *message) Attachments() map[string][]byte {
93108
return m.attachments
94109
}
95110

111+
// Body returns the body.
96112
func (m *message) Body() string {
97113
return m.body
98114
}
99115

116+
// SetNormalPriority sets the email priority to 'normal'.
100117
func (m *message) SetNormalPriority() {
101118
m.priority = "normal"
102119
}
103120

121+
// SetUrgentPriority sets the email priority to 'urgent'.
104122
func (m *message) SetUrgentPriority() {
105123
m.priority = "urgent"
106124
}
107125

126+
// SetNonUrgentPriority sets the email priority to 'non-urgent'.
108127
func (m *message) SetNonUrgentPriority() {
109128
m.priority = "non-urgent"
110129
}
111130

131+
// Priority returns the priority.
112132
func (m *message) Priority() string {
113133
return m.priority
114134
}
115135

136+
// FromString creates a new message with content from given string.
116137
func FromString(str string) *message {
117138
m := new()
118139
m.body = str
119140
return m
120141
}
121142

143+
// FromTemplateString creates a new message with content from parsed template string.
144+
//
145+
// Returns an error if the template string could not be parsed.
122146
func FromTemplateString(data any, tpl string) (*message, error) {
123147
buff := bytes.Buffer{}
124148
template, _ := template.New("tinymail").Parse(tpl)
@@ -131,6 +155,9 @@ func FromTemplateString(data any, tpl string) (*message, error) {
131155
return m, nil
132156
}
133157

158+
// FromTemplateFile creates a new message with content from parsed template file.
159+
//
160+
// Returns an error if the template file could not be parsed.
134161
func FromTemplateFile(data any, filenames ...string) (*message, error) {
135162
buff := bytes.Buffer{}
136163
template, _ := template.ParseFiles(filenames...)
@@ -143,6 +170,7 @@ func FromTemplateFile(data any, filenames ...string) (*message, error) {
143170
return m, nil
144171
}
145172

173+
// new creates a new empty message.
146174
func new() *message {
147175
return &message{
148176
from: "",

0 commit comments

Comments
 (0)