Skip to content

flashmob/mbox

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mbox library for Go (Golang)

This library implements the popular mbox format for storing email messages.

It implements the io.Reader and io.Writer, as well as io.Closer ready to plug in to your project.

The library has been written with streaming in mind. (i.e. data is read/written in chunks). The writer does not use any internal buffers (other than the slice provided). Both the reader and writer work on chunks of any size, especially handy if you use io.CopyBuffer or use in TCP streams and so on.

Unit tests coverage at > 90%, and include some edge cases. No external dependencies.

Did you know?

mbox has many variants! This library implements the common mboxrd variant, popularized by qmail.

For a description of the format, see http://qmail.omnis.ch/man/man5/mbox.html

Using

import "github.com/flashmob/mbox"

As a Writer

Example: read a message message.eml, encode and append to the mbox file.

// buf is our working buffer, 
// in this example 4096 bytes is the usual block size of a SSD
buf := make([]byte, 4096)
// input
fin, err := os.Open("./message.eml")
// output (mbox files are append-only)
fout, err := os.OpenFile("./mbox", os.O_APPEND|os.O_WRONLY, 0640 )
mbox := mbox.NewWriter(fout)
err = mbox.Open("test@example.com", time.Now())
if err != nil {
   return err
}
// do the encoding
_, err = io.CopyBuffer(fout, fin, buf)
if err != nil {
   return err
}
err = mbox.Close()
if err != nil {
  return err
}

fin.Close()
fout.Close()

As a Reader

// buf is our working buffer, 
// in this example 4096 bytes is the usual block size of a SSD
buf := make([]byte, 4096)
// output
fout, err := os.OpenFile("./message.eml", os.O_WRONLY, 0600)
// input (just read the first message)
fin, err := os.Open("./mbox")
mbox := mbox.NewReader(fin)
// do the decoding
_, err = io.CopyBuffer(fout, fin, buf)
if err != nil {
    return err
}
// Note: 
// we could read another message here by using io.CopyBuffer with a new destination
// the mbox reader (fin) will automatically read in the next message sequentially
err = mbox.Close()
if err != nil {
    return err
}
fin.Close()
fout.Close()

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages