-
Notifications
You must be signed in to change notification settings - Fork 3
/
mail.go
44 lines (40 loc) · 1.08 KB
/
mail.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
// Copyright (c) 2016 Mute Communications Ltd.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package mail implements email input messages in Mute.
package mail
import (
"io"
"io/ioutil"
"net/mail"
"github.com/mutecomm/mute/log"
)
// Parse parses a MIME encoded email for sending with Mute. It returns the
// intended recipient (parsed from mandatory the 'To' field) and the actual
// message (combined from the optional 'Subject field' plus the message body).
// It cannot handle attachments and/or multi-part messages.
func Parse(r io.Reader) (
recipient string,
message string,
err error,
) {
// read message
msg, err := mail.ReadMessage(r)
if err != nil {
return "", "", log.Error(err)
}
// parse 'To'
recipient = msg.Header.Get("To")
if recipient == "" {
return "", "", log.Error("mail: 'To' not defined")
}
// parse 'Subject'
subject := msg.Header.Get("Subject")
// read body
body, err := ioutil.ReadAll(msg.Body)
if err != nil {
return "", "", log.Error(err)
}
message = subject + "\n" + string(body)
return
}