30 lines
547 B
Go
30 lines
547 B
Go
package mailparse
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
|
|
"github.com/jhillyerd/enmime"
|
|
)
|
|
|
|
func getBody(envelope *enmime.Envelope) (string, error) {
|
|
if envelope.Text != "" {
|
|
return envelope.Text, nil
|
|
}
|
|
if envelope.HTML != "" {
|
|
// TODO: jaytaylor.com/html2text
|
|
return envelope.HTML, nil
|
|
}
|
|
return "", errors.New("Can't find any text part inside the email")
|
|
}
|
|
|
|
func GetText(r io.Reader) (string, error) {
|
|
envelope, err := enmime.ReadEnvelope(r)
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
os.Exit(1)
|
|
}
|
|
return getBody(envelope)
|
|
}
|