emaildecoder

package module
v0.0.0-...-916674a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 14, 2023 License: MIT Imports: 11 Imported by: 0

README

go-emaildecoder

Purpose

This library handles heavy lifting when decoding an email source.

It uses an io.Reader as input and can be used to parse :

  • EML files
  • Input stream from an SMTP server

Features

  • Finds text/plain and text/html
  • Exposes mail.Header
  • Allows for attachments parsing and callback
  • Support decoding charsets to UTF-8

Example

package main

import (
	"fmt"
	"github.com/LeakIX/go-emaildecoder"
	"io"
	"os"
	"path/filepath"
)

func main() {
	// Creates an attachments directory
	if err := os.MkdirAll("attachments", 0750); err != nil && err != os.ErrExist {
		panic(err)
	}
	// Creates a new decoder from stdin, with an attachment handler, can be nil if not needed
	decoder := emaildecoder.NewDecoder(os.Stdin, handleAttachment)
	// Parse the email
	email, err := decoder.Decode()
	if err != nil {
		panic(err)
	}
	// Display results
	fmt.Printf("From: %s\nSubject: %s\n", email.Headers.Get("From"), email.Headers.Get("Subject"))
	fmt.Println(string(email.PlainText))
}

// Handle attachments
func handleAttachment(attachment emaildecoder.Attachment) {
	fmt.Printf("Saving %s...\n", attachment.Filename)
	if file, err := os.OpenFile(filepath.Join("attachments", attachment.Filename), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0640); err != nil {
		panic(err)
	} else if _, err = io.Copy(file, attachment); err != nil {
		panic(err)
	} else if err = file.Close(); err != nil {
		panic(err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Attachment

type Attachment struct {
	io.Reader
	Filename    string
	ContentType string
}

type AttachmentCallback

type AttachmentCallback func(attachment Attachment)

type Decoder

type Decoder struct {
	// contains filtered or unexported fields
}

func NewDecoder

func NewDecoder(reader io.Reader, attachmentCallback AttachmentCallback) *Decoder

func (*Decoder) Decode

func (d *Decoder) Decode() (*EmailContent, error)

type EmailContent

type EmailContent struct {
	HTML      []byte
	PlainText []byte
	Headers   mail.Header
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL