...

Package quotedprintable

import "gopkg.in/alexcesaro/quotedprintable.v3"
Overview
Index
Examples

Overview ▾

Package quotedprintable implements quoted-printable encoding as specified by RFC 2045.

Constants

const (
    // BEncoding represents Base64 encoding scheme as defined by RFC 2045.
    BEncoding = WordEncoder('b')
    // QEncoding represents the Q-encoding scheme as defined by RFC 2047.
    QEncoding = WordEncoder('q')
)

type Reader

Reader is a quoted-printable decoder.

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

func NewReader

func NewReader(r io.Reader) *Reader

NewReader returns a quoted-printable reader, decoding from r.

func (*Reader) Read

func (r *Reader) Read(p []byte) (n int, err error)

Read reads and decodes quoted-printable data from the underlying reader.

type WordDecoder

A WordDecoder decodes MIME headers containing RFC 2047 encoded-words.

type WordDecoder struct {
    // CharsetReader, if non-nil, defines a function to generate
    // charset-conversion readers, converting from the provided
    // charset into UTF-8.
    // Charsets are always lower-case. utf-8, iso-8859-1 and us-ascii charsets
    // are handled by default.
    // One of the the CharsetReader's result values must be non-nil.
    CharsetReader func(charset string, input io.Reader) (io.Reader, error)
}

func (*WordDecoder) Decode

func (d *WordDecoder) Decode(word string) (string, error)

Decode decodes an encoded-word. If word is not a valid RFC 2047 encoded-word, word is returned unchanged.

Example

Code:

dec := new(WordDecoder)
header, err := dec.Decode("=?utf-8?q?=C2=A1Hola,_se=C3=B1or!?=")
if err != nil {
    panic(err)
}
fmt.Println(header)

dec.CharsetReader = func(charset string, input io.Reader) (io.Reader, error) {
    switch charset {
    case "x-case":
        // Fake character set for example.
        // Real use would integrate with packages such
        // as code.google.com/p/go-charset
        content, err := ioutil.ReadAll(input)
        if err != nil {
            return nil, err
        }
        return bytes.NewReader(bytes.ToUpper(content)), nil
    }
    return nil, fmt.Errorf("unhandled charset %q", charset)
}
header, err = dec.Decode("=?x-case?q?hello!?=")
if err != nil {
    panic(err)
}
fmt.Println(header)

Output:

¡Hola, señor!
HELLO!

func (*WordDecoder) DecodeHeader

func (d *WordDecoder) DecodeHeader(header string) (string, error)

DecodeHeader decodes all encoded-words of the given string. It returns an error if and only if CharsetReader of d returns an error.

Example

Code:

dec := new(WordDecoder)
header, err := dec.DecodeHeader("=?utf-8?q?=C3=89ric?= <eric@example.org>, =?utf-8?q?Ana=C3=AFs?= <anais@example.org>")
if err != nil {
    panic(err)
}
fmt.Println(header)

header, err = dec.DecodeHeader("=?utf-8?q?=C2=A1Hola,?= =?utf-8?q?_se=C3=B1or!?=")
if err != nil {
    panic(err)
}
fmt.Println(header)

dec.CharsetReader = func(charset string, input io.Reader) (io.Reader, error) {
    switch charset {
    case "x-case":
        // Fake character set for example.
        // Real use would integrate with packages such
        // as code.google.com/p/go-charset
        content, err := ioutil.ReadAll(input)
        if err != nil {
            return nil, err
        }
        return bytes.NewReader(bytes.ToUpper(content)), nil
    }
    return nil, fmt.Errorf("unhandled charset %q", charset)
}
header, err = dec.DecodeHeader("=?x-case?q?hello_?= =?x-case?q?world!?=")
if err != nil {
    panic(err)
}
fmt.Println(header)

Output:

Éric <eric@example.org>, Anaïs <anais@example.org>
¡Hola, señor!
HELLO WORLD!

type WordEncoder

A WordEncoder is a RFC 2047 encoded-word encoder.

type WordEncoder byte

func (WordEncoder) Encode

func (e WordEncoder) Encode(charset, s string) string

Encode returns the encoded-word form of s. If s is ASCII without special characters, it is returned unchanged. The provided charset is the IANA charset name of s. It is case insensitive.

Example

Code:

fmt.Println(QEncoding.Encode("utf-8", "¡Hola, señor!"))
fmt.Println(QEncoding.Encode("utf-8", "Hello!"))
fmt.Println(BEncoding.Encode("UTF-8", "¡Hola, señor!"))
fmt.Println(QEncoding.Encode("ISO-8859-1", "Caf\xE9"))

Output:

=?utf-8?q?=C2=A1Hola,_se=C3=B1or!?=
Hello!
=?UTF-8?b?wqFIb2xhLCBzZcOxb3Ih?=
=?ISO-8859-1?q?Caf=E9?=

type Writer

A Writer is a quoted-printable writer that implements io.WriteCloser.

type Writer struct {
    // Binary mode treats the writer's input as pure binary and processes end of
    // line bytes as binary data.
    Binary bool
    // contains filtered or unexported fields
}

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter returns a new Writer that writes to w.

func (*Writer) Close

func (w *Writer) Close() error

Close closes the Writer, flushing any unwritten data to the underlying io.Writer, but does not close the underlying io.Writer.

func (*Writer) Write

func (w *Writer) Write(p []byte) (n int, err error)

Write encodes p using quoted-printable encoding and writes it to the underlying io.Writer. It limits line length to 76 characters. The encoded bytes are not necessarily flushed until the Writer is closed.