forked from matthewljsmith/go-email
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_delivery_status.go
53 lines (48 loc) · 1.75 KB
/
message_delivery_status.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package email
import (
"bufio"
"bytes"
"errors"
"io"
"net/textproto"
)
// HasDeliveryStatusMessage returns true if this Message has a
// content type of "message/delivery-status" and has a non-nil SubMessage
// containing the delivery status information.
func (m *Message) HasDeliveryStatusMessage() bool {
contentType, _, err := m.Header.ContentType()
if err != nil {
return false
}
return contentType == "message/delivery-status" && m.SubMessage != nil
}
// DeliveryStatusMessageDNS returns the message DNS information,
// or an error if HasDeliveryStatusMessage would return false.
func (m *Message) DeliveryStatusMessageDNS() (Header, error) {
if !m.HasDeliveryStatusMessage() {
return Header{}, errors.New("Message does not have media content of type message/delivery-status")
}
return m.SubMessage.Header, nil
}
// DeliveryStatusRecipientDNS returns the message recipients' DNS information,
// or an error if HasDeliveryStatusMessage would return false.
func (m *Message) DeliveryStatusRecipientDNS() ([]Header, error) {
recipientDNS := make([]Header, 0, 1)
if !m.HasDeliveryStatusMessage() {
return recipientDNS, errors.New("Message does not have media content of type message/delivery-status")
}
var err error
var recipientHeaders textproto.MIMEHeader
tp := textproto.NewReader(bufio.NewReader(bytes.NewReader(m.SubMessage.Body)))
for err != io.EOF {
recipientHeaders, err = tp.ReadMIMEHeader()
if err != nil && err != io.EOF {
return nil, err
}
recipientDNS = append(recipientDNS, Header(recipientHeaders))
}
return recipientDNS, nil
}