Skip to content

Commit cb68edd

Browse files
committed
Email: separated inline and attachment
Signed-off-by: Vishal Rana <[email protected]>
1 parent 21f6ede commit cb68edd

File tree

3 files changed

+29
-25
lines changed

3 files changed

+29
-25
lines changed

client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (c *Client) Log() (log *Log) {
6767
BatchSize: 60,
6868
DispatchInterval: 60,
6969
}
70-
log.resetLogs()
70+
log.resetLogEntries()
7171
return
7272
}
7373

email.go

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,6 @@ type (
4343
}
4444
)
4545

46-
func emailFileFromPath(path string) (*emailFile, error) {
47-
data, err := ioutil.ReadFile(path)
48-
if err != nil {
49-
return nil, err
50-
}
51-
return &emailFile{
52-
Name: filepath.Base(path),
53-
Content: base64.StdEncoding.EncodeToString(data),
54-
}, nil
55-
}
56-
5746
func NewEmailMessage(to, from, subject string) *EmailMessage {
5847
return &EmailMessage{
5948
To: to,
@@ -62,20 +51,30 @@ func NewEmailMessage(to, from, subject string) *EmailMessage {
6251
}
6352
}
6453

65-
func (m *EmailMessage) addFiles() error {
66-
for _, path := range m.inlines {
67-
file, err := emailFileFromPath(path)
54+
func (m *EmailMessage) addInlines() error {
55+
for _, inline := range m.inlines {
56+
data, err := ioutil.ReadFile(inline)
6857
if err != nil {
6958
return err
7059
}
71-
m.Inlines = append(m.Inlines, file)
60+
m.Inlines = append(m.Inlines, &emailFile{
61+
Name: filepath.Base(inline),
62+
Content: base64.StdEncoding.EncodeToString(data),
63+
})
7264
}
73-
for _, path := range m.attachments {
74-
file, err := emailFileFromPath(path)
65+
return nil
66+
}
67+
68+
func (m *EmailMessage) addAttachments() error {
69+
for _, attachment := range m.attachments {
70+
data, err := ioutil.ReadFile(attachment)
7571
if err != nil {
7672
return err
7773
}
78-
m.Attachments = append(m.Attachments, file)
74+
m.Inlines = append(m.Attachments, &emailFile{
75+
Name: filepath.Base(attachment),
76+
Content: base64.StdEncoding.EncodeToString(data),
77+
})
7978
}
8079
return nil
8180
}
@@ -90,7 +89,10 @@ func (m *EmailMessage) AddAttachment(path string) {
9089

9190
// Send sends the email message.
9291
func (e *Email) Send(m *EmailMessage) (*EmailMessage, error) {
93-
if err := m.addFiles(); err != nil {
92+
if err := m.addInlines(); err != nil {
93+
return nil, err
94+
}
95+
if err := m.addAttachments(); err != nil {
9496
return nil, err
9597
}
9698
em := new(EmailMessage)

log.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,19 @@ var levels = map[Level]string{
6363
LevelFatal: "FATAL",
6464
}
6565

66-
func (l *Log) resetLogs() {
66+
func (l *Log) resetLogEntries() {
6767
l.mutex.Lock()
6868
defer l.mutex.Unlock()
6969
l.logs = make([]*logEntry, 0, l.BatchSize)
7070
}
7171

72-
func (l *Log) appendLog(lm *logEntry) {
72+
func (l *Log) appendLogEntry(lm *logEntry) {
7373
l.mutex.Lock()
7474
defer l.mutex.Unlock()
7575
l.logs = append(l.logs, lm)
7676
}
7777

78-
func (l *Log) listLogs() []*logEntry {
78+
func (l *Log) listLogEntries() []*logEntry {
7979
l.mutex.Lock()
8080
defer l.mutex.Unlock()
8181
logs := make([]*logEntry, len(l.logs))
@@ -96,8 +96,10 @@ func (l *Log) dispatch() error {
9696
return nil
9797
}
9898

99+
defer l.resetLogEntries()
100+
99101
le := new(LogError)
100-
_, err := l.sling.Post("").BodyJSON(l.listLogs()).Receive(nil, le)
102+
_, err := l.sling.Post("").BodyJSON(l.listLogEntries()).Receive(nil, le)
101103
if err != nil {
102104
return err
103105
}
@@ -159,7 +161,7 @@ func (l *Log) Log(level Level, format string, args ...interface{}) {
159161
Level: levels[level],
160162
Message: message,
161163
}
162-
l.appendLog(lm)
164+
l.appendLogEntry(lm)
163165

164166
// Dispatch batch
165167
if l.logsLength() >= l.BatchSize {

0 commit comments

Comments
 (0)